-
Notifications
You must be signed in to change notification settings - Fork 59
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
Panic on size being smaller than default #213
Comments
I don't think it would panic, but I might miss something. So let's say So rust-multihash/src/multihash.rs Lines 102 to 104 in f1d20e9
|
My code looks like that where DIGEST_SIZE is 32 right now. pub struct ContentHasher(Blake3Hasher<DIGEST_SIZE>);
impl ContentHasher {
pub fn new() -> ContentHasher {
ContentHasher(Blake3Hasher::default())
}
pub fn write(&mut self, input: &[u8]) {
self.0.update(input);
}
pub fn finish(&mut self) -> ContentHash {
let hash = multihash::Code::Blake3_256.wrap(self.0.finalize()).unwrap();
hash.resize::<DIGEST_SIZE>().unwrap()
}
pub fn reset(&mut self) {
self.0.reset();
}
} However, if I were to set it to something smaller than the default digest size (32), it will panic. The reason is an out of bounds indexing error due the array rust-multihash/src/hasher_impl.rs Lines 100 to 103 in f1d20e9
It is the other way around. The length will always be S or smaller because Do you consider it a user error to use an S smaller than the default size of the hash? |
Blake3Hasher was originally intended to be flexible like this, but some implementation details had kept me away at the time but has since been fixed. see #130 |
Thanks @mriise for a fix and @hummingly for being patient and explaining things in even more depths. It's funny that I even opened an issue about it and didn't recall. |
* Use blake3's XOF function for output sizes other than 32 fixes #213 * minor fixes - use expected result & rename new blake3 test - use easier to read code for filling inner digest with blake3
quick note for transparency, this was a bit more broken than this issue mentions, since the digest returned by blake3 was only 32 bytes, anything bigger would've been filled with ...fun |
I was looking through the source code and found this for the
Blake3Hasher.finalize()
method.rust-multihash/src/hasher_impl.rs
Line 124 in f1d20e9
This will always panic if
S
is less than 32 and the same line of code appears for other hasher implementations too. The fix would be to compute the minimum length and use that to create the byte slices.The text was updated successfully, but these errors were encountered: