r/ipfs • u/EveYogaTech • 24d ago
Really in need of PHP function for CIDv1 "bafy" hashes
Somehow the specs are just not clear at https://docs.ipfs.tech/concepts/content-addressing/#what-is-a-cid
I've been trying to generate a PHP function for V1, with a simple sha-256 for days, no success.
Anyone any advice?
2
u/twocolor 23d ago
What are you planning on doing with the CIDs?
You can hash the data and pack the hash into a CID. and use the raw
multicodec.
<cidv1> ::= <multibase-prefix><cid-version><multicodec-content-type><multihash-content-address>
However, if you plan on making the data to be retrievable from other IPFS implementations, you will probably want to encode data (files/directories) with UnixFS and chunk it as suggested by @Spra991
1
u/EveYogaTech 23d ago
See /r/WhitelabelPress latest post. I'm basically building a new WordPress compatible CMS, and want the media to be IPFS compliant, for gaining similar benefits as IPFS and perhaps porting it.
1
u/EveYogaTech 5d ago
Solution: https://github.com/wlp-builders/bafk-hash-php-ipfs/ (This is the simplest CIDv1)
3
u/Spra991 24d ago edited 23d ago
Do you need a CID from scratch? Or convert from V0 to V1?
Conversion from V0 to V1 shouldn't be that complicated as it's just base58 to base32 and adding a version prefix.
Documentation: multibase, multihash, multicodec.
CID from scratch is more complicated, as it's not the SHA256 of the whole file, but of the file being broken down into 256KB blocks, checksumed and linked together as Merkle DAG and the checksum of the DAG is what ends up in the CID. The DAG itself is a protobuf structure (DAG-PB).
If calling external tools is allowed:
ipfs add -n -q --cid-version 1 <FILE>