Receive a file with an known SHA256 sum from IPFS

Hello,

I try to find information on how to calculate the CIDv0 or CIDv1 from a known SHA256 sum

A CID is just a SHA-256 hash (or any other hash type), wrapped with some prefixes, so you can perfectly do this, usually programmatically… GitHub - whyrusleeping/elcid: A cid encoder/decoder tool is an example, but it does not have a sha256-hex type, but you can get the idea of how things work (https://github.com/whyrusleeping/elcid/blob/master/main.go#L80).

Anyways, the reason we don’t do this (and therefore there’s no tooling for it) is that IPFS chunks files in 256KB-size chunks (by default). Therefore the SHA256 IPFS hash will usually not correspond to the regular SHA256 of your file.bin (unless that file is less than 256KB and was added as a raw leaf.

Unfortunately, this means for the vast majority of cases there is no way to figure out the IPFS-Cid from the original sha256sum of a file.

2 Likes