From eadcd95bdaaa38f18ec21e8687fc76da7d35d25e Mon Sep 17 00:00:00 2001 From: mriise Date: Wed, 18 May 2022 15:43:57 -0700 Subject: [PATCH 1/4] Use blak3's XOF function for output sizes other than 32 fixes #213 --- src/hasher_impl.rs | 23 ++++++++++++++++++----- tests/lib.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/hasher_impl.rs b/src/hasher_impl.rs index 925e47c6..260376df 100644 --- a/src/hasher_impl.rs +++ b/src/hasher_impl.rs @@ -102,6 +102,14 @@ pub mod blake3 { digest: [u8; S], } + impl Blake3Hasher { + /// blake3's XOF function, fills the given slice with hash information + pub fn finalize_xof_fill(&mut self, digest_out: &mut [u8]) { + let mut digest = self.hasher.finalize_xof(); + digest.fill(digest_out) + } + } + impl Default for Blake3Hasher { fn default() -> Self { let hasher = ::blake3::Hasher::new(); @@ -119,11 +127,16 @@ pub mod blake3 { } fn finalize(&mut self) -> &[u8] { - let digest = self.hasher.finalize(); //default is 32 bytes anyway - let digest_bytes = digest.as_bytes(); - let digest_out = &mut self.digest[..digest_bytes.len().max(S)]; - digest_out.copy_from_slice(digest_bytes); - digest_out + // let digest = self.hasher.finalize(); //default is 32 bytes anyway + // let digest_bytes = digest.as_bytes(); + // let size = digest_bytes.len().min(S); + // let digest_out = &mut self.digest[..digest_bytes.len().max(S)]; + // digest_out.copy_from_slice(digest_bytes); + let Blake3Hasher { hasher, digest } = self; + let mut output = hasher.finalize_xof(); + output.fill(digest); + + digest } fn reset(&mut self) { diff --git a/tests/lib.rs b/tests/lib.rs index 3fdcba3b..f8416932 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -335,3 +335,40 @@ fn multihash_errors() { "Should error on wrong hash length" ); } + +#[test] +fn smaller_than_default_digest() { + use multihash::Blake3Hasher; + const DIGEST_SIZE: usize = 16; + pub struct ContentHasher(Blake3Hasher); + + pub struct ContentHash([u8; DIGEST_SIZE]); + + impl ContentHasher { + fn new() -> ContentHasher { + ContentHasher(Blake3Hasher::default()) + } + + fn write(&mut self, input: &[u8]) { + self.0.update(input); + } + + fn finish(&mut self) -> ContentHash { + let hash = multihash::Code::Blake3_256.wrap(self.0.finalize()).unwrap(); + let resized_hash = hash.resize::().unwrap(); + + let mut content = ContentHash([0u8; DIGEST_SIZE]); + content.0.copy_from_slice(resized_hash.digest()); + content + } + + fn reset(&mut self) { + self.0.reset(); + } + } + + let mut hasher = ContentHasher::new(); + hasher.write("foobar".as_bytes()); + let _content_hash = hasher.finish(); + hasher.reset(); +} From 18a40109e7fa297dfb350f7720e752df2554efc3 Mon Sep 17 00:00:00 2001 From: mriise Date: Wed, 18 May 2022 15:46:08 -0700 Subject: [PATCH 2/4] cleanup comments --- src/hasher_impl.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/hasher_impl.rs b/src/hasher_impl.rs index 260376df..8e594d74 100644 --- a/src/hasher_impl.rs +++ b/src/hasher_impl.rs @@ -103,7 +103,7 @@ pub mod blake3 { } impl Blake3Hasher { - /// blake3's XOF function, fills the given slice with hash information + /// using blake3's XOF function, fills the given slice with hash output pub fn finalize_xof_fill(&mut self, digest_out: &mut [u8]) { let mut digest = self.hasher.finalize_xof(); digest.fill(digest_out) @@ -127,15 +127,9 @@ pub mod blake3 { } fn finalize(&mut self) -> &[u8] { - // let digest = self.hasher.finalize(); //default is 32 bytes anyway - // let digest_bytes = digest.as_bytes(); - // let size = digest_bytes.len().min(S); - // let digest_out = &mut self.digest[..digest_bytes.len().max(S)]; - // digest_out.copy_from_slice(digest_bytes); let Blake3Hasher { hasher, digest } = self; let mut output = hasher.finalize_xof(); output.fill(digest); - digest } From acff6f101c8197c65da9e6745fa9d64bc15c337d Mon Sep 17 00:00:00 2001 From: mriise Date: Thu, 19 May 2022 09:08:09 -0700 Subject: [PATCH 3/4] minor fixes - use expected result & rename new blake3 test - use easier to read code for filling inner digest with blake3 --- src/hasher_impl.rs | 7 +++---- tests/lib.rs | 7 +++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/hasher_impl.rs b/src/hasher_impl.rs index 8e594d74..24a04aa2 100644 --- a/src/hasher_impl.rs +++ b/src/hasher_impl.rs @@ -127,10 +127,9 @@ pub mod blake3 { } fn finalize(&mut self) -> &[u8] { - let Blake3Hasher { hasher, digest } = self; - let mut output = hasher.finalize_xof(); - output.fill(digest); - digest + let mut output = self.hasher.finalize_xof(); + output.fill(&mut self.digest); + &self.digest } fn reset(&mut self) { diff --git a/tests/lib.rs b/tests/lib.rs index f8416932..17e6f770 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -337,7 +337,7 @@ fn multihash_errors() { } #[test] -fn smaller_than_default_digest() { +fn blak3_non_default_digest() { use multihash::Blake3Hasher; const DIGEST_SIZE: usize = 16; pub struct ContentHasher(Blake3Hasher); @@ -369,6 +369,9 @@ fn smaller_than_default_digest() { let mut hasher = ContentHasher::new(); hasher.write("foobar".as_bytes()); - let _content_hash = hasher.finish(); + let content_hash = hasher.finish(); hasher.reset(); + + let expected = hex::decode("aa51dcd43d5c6c5203ee16906fd6b35d").unwrap(); + assert_eq!(&content_hash.0, expected.as_slice()) } From a9a2a71c52c901c6138f83b8bdacac746789855e Mon Sep 17 00:00:00 2001 From: mriise Date: Thu, 19 May 2022 09:11:00 -0700 Subject: [PATCH 4/4] run cargo fmt --- tests/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/lib.rs b/tests/lib.rs index 17e6f770..cd475d16 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -371,7 +371,7 @@ fn blak3_non_default_digest() { hasher.write("foobar".as_bytes()); let content_hash = hasher.finish(); hasher.reset(); - + let expected = hex::decode("aa51dcd43d5c6c5203ee16906fd6b35d").unwrap(); assert_eq!(&content_hash.0, expected.as_slice()) }