How actually is an IPFS hash (ipfs add) is generated?
I'm looking for step by step operations done on input(bytes/string) to get output(in this case IPFSHash)
blockchaingo-ethereumipfs
I'm looking for step by step operations done on input(bytes/string) to get output(in this case IPFSHash)
Best Answer
IPFS uses multihash where the format is the following:
The list of hash function codes can be found in this table.
Here's some pseudocode of the process using SHA2-256 as the hashing function.
Concatenating those three items will produce
Which then you encode it to base58
Here's an example of how to implement multihash in JavaScript:
There's a CLI you can use to generate multihashes:
A file in IPFS is "transformed" into a Unixfs "file", which is a representation of files in a DAG. So when you use
add
to upload a file to IPFS, the data has metadata wrapper which will give you a different result when you multihash it.For example:
Here's an example in Node.js of how to generate the exact same multihash as
ipfs add
:Hope this helps