You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you have a hash digest digest: &[u8] and you want to encode that into multihash bytes, you're out of luck if you're using rust-multihash currently. This is because encode expects un-hashed data as input. Instead, you must do something like this manually:
let hash_alg = multihash::Hash::SHA1;
let mut mh = Vec::with_capacity(digest.len() + 2);
mh.push(hash_alg.code());
mh.push(hash_alg.size());
mh.extend_from_slice(digest);
In contrast, go-multihash's Encode function does not hash your data for you, so you can directly pass in a digest.
Potential solutions
This could be remedied with a new encoding function that expects a hash digest, maybe something named encode_digest? However, perhaps we should consider changing the way encode works. It would be a breaking change, but it would bring it in-line with how go-multihash works.
The text was updated successfully, but these errors were encountered:
leavehouse
added a commit
to leavehouse/rust-multihash
that referenced
this issue
Feb 21, 2018
In go-multihash, the `Sum` function takes un-hashed data and a
specification of a hash function and produces multihash bytes
by hashing the input data. The `Encode` function, on the other hand,
produces multihash bytes from an existing hash digest.
This commit adds an equivalent of the `Encode` function from
go-multihash. Since there is already a function named "encode",
this commit also renames it to `sum` to make it similar to the go
version of this library. This is a breaking change.
Closesmultiformats#25
Problem
If you have a hash digest
digest: &[u8]
and you want toencode
that into multihash bytes, you're out of luck if you're usingrust-multihash
currently. This is becauseencode
expects un-hashed data as input. Instead, you must do something like this manually:In contrast,
go-multihash
'sEncode
function does not hash your data for you, so you can directly pass in a digest.Potential solutions
This could be remedied with a new encoding function that expects a hash digest, maybe something named
encode_digest
? However, perhaps we should consider changing the wayencode
works. It would be a breaking change, but it would bring it in-line with how go-multihash works.The text was updated successfully, but these errors were encountered: