Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way to encode a digest directly? #25

Closed
leavehouse opened this issue Feb 6, 2018 · 1 comment
Closed

No way to encode a digest directly? #25

leavehouse opened this issue Feb 6, 2018 · 1 comment

Comments

@leavehouse
Copy link

Problem

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.

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.

Closes multiformats#25
@vmx
Copy link
Member

vmx commented Feb 26, 2020

There is now a function called multihash::wrap() where you can encode a multihash from an existing digest.

@vmx vmx closed this as completed Feb 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants