multiformats / cs-multihash

Multihash implementation in C#

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to hash image and other files in Multihash...???and also how to get back same values with decode...?

mosinms7711 opened this issue · comments

How to hash image and other files in Multihash...???

First you'll have to get a byte array of the content you want to hash, then

Multihash mh = Multihash.Sum<SHA1>(bytes);

SHA1 should be replaced by whatever algorithm you want to use.

how to get back same values with decode...?

I'm not exactly sure what you want to do, and I'd be the first to admit the documentation/readme is not that very helpful. I'd suggest you check out multihash to get an idea of what this library actually provide.

Okay, thanks for the reference.
The goal is to use mutlihash nuget to simulate IPFS hash before even sending it to IPFS. To check for integrity of the payload being sent.

Can I use a specific function to get that "Qm" Hash locally, which is supposed to be received when the file is stored in IPFS..

Hi.

  1. IPFS uses the SHA256 algorithm. Use Sum with the byte content of given file.
  2. IPFS uses the Base58btc encoding for human readable hashes.
var bytes = File.ReadAllBytes(filename);
var multihash = Multihash.Sum<SHA256>(bytes);
var hash = multihash.ToString(MultibaseEncoding.Base58Btc);

Something like that will give you the 'Qm'-prefix.

How to wrap data in Merkle Dag with xamarin forms or c#.?I need to add content to ipfs network.Before that i want hash value for the content.Now i can get hash values of raw data but when we add content to ipfs it wrap the data with merkle dag then create hash value.As with above methods i am getting hash with "Qm" prefix but it is not same as IPFS hash

For example if there is image with name funImage.png...when we add it to IPFS then it gives me "QmXExS4BMc1YrH6iWERyryFcDWkvobxryXSwECLrcd7Y1H" and when i am creating hash value with multihash for same image it gives me "QmaZNJC2WexKbjiBaTuSjsGrx3keNJgPZQC9Rbdm24pU8x"...now i want to create same hash value for both...

@mosinms7711 I'm afraid thats out of scope for this library. I'd recommend you check out IPLD.

Note: IPFS may not always produce the same "hash" for a file due to different chunking algorithms, formats, etc. It's produces the same hash consistently unless the default options are changed but relying on users picking the default options won't always work.