This repo tests IPFS CID generation using a IPFS daemon and compares this to a IPFS CID converted from a raw sha256 hash of a file for a range of file sizes.
When using the IPFS daemon (see ./ipfs.js), the raw-leaves flag is set and the chunker is set
to 1024KB (the maximum, its default setting is 256KB).
The experiment shows that IPFS CIDs created using the daemon are only equivalent to CIDv1 representations of the raw file hash when the size of the file is less than or equal to the chunker.
- First install [IPFS(https://docs.ipfs.tech/install/command-line/#install-official-binary-distributions)
- Start ipfs daemon:
ipfs daemon - Install packages:
npm install - Generate dummy files:
node generateDummyFiles.js - Test CID generation:
node testCIDGeneration.js
The result should show something similar to the following output:
File size: 1024 bytes
IPFS Raw Leaf CID: bafkreidylmdvd7bmkpobjjgohwaa42ppttqqbhvte7gpiwfp4cocilbgze
Raw file hash (hex): 785b0751fc2c53dc14a4ce3d800e69ef9ce1009eb327ccf458afe09c242c26c9
Raw file hash CID: bafkreidylmdvd7bmkpobjjgohwaa42ppttqqbhvte7gpiwfp4cocilbgze
✅ CID match for file size: 1 KB
cid: bafkreidylmdvd7bmkpobjjgohwaa42ppttqqbhvte7gpiwfp4cocilbgze
rawHashCid: bafkreidylmdvd7bmkpobjjgohwaa42ppttqqbhvte7gpiwfp4cocilbgze
File size: 10240 bytes
IPFS Raw Leaf CID: bafkreihjm5qkq53iof545ph5exo4pvdljw6jljfqafg66cambbjz67mq2a
Raw file hash (hex): e96760a87768717bcebcfd25ddc7d46b4dbc95a4b0014def080c08539f7d90d0
Raw file hash CID: bafkreihjm5qkq53iof545ph5exo4pvdljw6jljfqafg66cambbjz67mq2a
✅ CID match for file size: 10 KB
cid: bafkreihjm5qkq53iof545ph5exo4pvdljw6jljfqafg66cambbjz67mq2a
rawHashCid: bafkreihjm5qkq53iof545ph5exo4pvdljw6jljfqafg66cambbjz67mq2a
File size: 262144 bytes
IPFS Raw Leaf CID: bafkreibdci4uxwmvixm54ey4etx3paphmwwbv3beh4xnsndvs6tzhjav5e
Raw file hash (hex): 2312394bd99545d9de131c24efb781e765ac1aec243f2ed9347597a793a415e9
Raw file hash CID: bafkreibdci4uxwmvixm54ey4etx3paphmwwbv3beh4xnsndvs6tzhjav5e
✅ CID match for file size: 256 KB
cid: bafkreibdci4uxwmvixm54ey4etx3paphmwwbv3beh4xnsndvs6tzhjav5e
rawHashCid: bafkreibdci4uxwmvixm54ey4etx3paphmwwbv3beh4xnsndvs6tzhjav5e
File size: 1048576 bytes
IPFS Raw Leaf CID: bafkreih3xkzit57zjmsxg3cyxzdktfgeih6qevjmyybcguxd3bws7k34qm
Raw file hash (hex): fbbab289f7f94b25736c58be46a994c441fd02552cc6022352e3d86d2fab7c83
Raw file hash CID: bafkreih3xkzit57zjmsxg3cyxzdktfgeih6qevjmyybcguxd3bws7k34qm
✅ CID match for file size: 1024 KB
cid: bafkreih3xkzit57zjmsxg3cyxzdktfgeih6qevjmyybcguxd3bws7k34qm
rawHashCid: bafkreih3xkzit57zjmsxg3cyxzdktfgeih6qevjmyybcguxd3bws7k34qm
File size: 1049600 bytes
IPFS Raw Leaf CID: bafybeiekkzawgc5a7twqkmiaacqr77cx3ya4apsrdebauoaj7lvwfpzt4a
Raw file hash (hex): 5458c23abbce8d80a1b5b46d0555aa348c762d344e86b9f833d54ce982f3832f
Raw file hash CID: bafkreiculdbdvo6orwakdnnunucvlkrurr3c2ncoq247qm6vjtuyf44df4
❌ CID mismatch for file size: 1025 KB
cid: bafybeiekkzawgc5a7twqkmiaacqr77cx3ya4apsrdebauoaj7lvwfpzt4a
rawHashCid: bafkreiculdbdvo6orwakdnnunucvlkrurr3c2ncoq247qm6vjtuyf44df4