function generateCID
generateCID(content: string | Uint8Array): Promise<string>

Generates a Content Identifier (CID) for the given content.

Creates a CIDv1 using SHA-256 hashing and base32 encoding, compatible with IPFS and other content-addressed storage systems. The resulting CID starts with the "bafkrei" prefix.

Examples

Hashing a string

const cid = await generateCID('Hello, World!');
console.log(cid);
// "bafkreifzjut3te2nhyekklss27nh3k72ysco7y32koao5eei66wof36n5e"

Hashing binary data

const bytes = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f]);
const cid = await generateCID(bytes);

Verifying content integrity

const original = await generateCID(content);
const retrieved = await generateCID(downloadedContent);
if (original === retrieved) {
  console.log('Content verified');
}

Parameters

content: string | Uint8Array
  • The content to hash. Strings are UTF-8 encoded before hashing.

Return Type

Promise<string>

A promise resolving to the CID string (59 characters, starts with "bafkrei")

See