ramiz_karaeski
ramiz_karaeski

Reputation: 11

CIDv1 base32 encoding

Can anyone help me understand how string version is calculated for CIDv1?

Example: bafykbzacea6h5nvlblvak5jhj7m7ku5sp7u2roo3lbembab3a6tnvmuzgcqas

Is this right hex representation according to CID explorer 0170B220203C7EB6AB0AEA0575274FD9F553B27FE9A8B9DB5848C0803B07A6DAB29930A009

Using this tool, values doesn't match.

Upvotes: 1

Views: 340

Answers (2)

vicnetto
vicnetto

Reputation: 71

I know it’s an old question, but perhaps this can help someone in the future.

The issue lies in your decoding alphabet. In IPFS, the default alphabet used for base32 encoding is [a-z, 2-7], using only lowercase characters. This was proposed and defined in a go-ipfs issue on GitHub (see Kubo issue #4143).

To decode correctly, you need a tool that allows you to specify your alphabet, such as CyberChef (with this recipe).

It’s important to note that the first character of a CIDv1 indicates the encoding algorithm used, according to the multibase protocol. Therefore, before decoding this version of the CID, you must remove the first character.

Upvotes: 0

lidel
lidel

Reputation: 555

The hex displayed by the CID explorer shows the HEX of the hash DIGEST part, not the entire CID. This enables you to extract raw blake2b-256 from the CID.

CIDv1 have a prefix which for your CID is:

  • multibase-indicator: b (base23) (which informs how to decode binary bytes that follow)
  • cid version: 1
  • multicodec-content-type: dag-pb
  • multihash, which itself has three fields
    • code of the hash function used: blake2b-256
    • length of digest: 32
    • DIGEST

Relevant docs:

To inspect CID details like prefix (things before hash digest) with ipfs cid format (from Kubo):

$ ipfs cid format -f "(%b)-(%v)-(%c)-(%h)-(%L)-DIGEST" bafykbzacea6h5nvlblvak5jhj7m7ku5sp7u2roo3lbembab3a6tnvmuzgcqas
(base32)-(cidv1)-(dag-pb)-(blake2b-256)-(32)-DIGEST

See ipfs cid format --help.

Upvotes: 1

Related Questions