From 035b663023e49606c2d26411a22b222fb64e27eb Mon Sep 17 00:00:00 2001 From: parazyd Date: Mon, 28 Aug 2023 12:53:51 +0200 Subject: [PATCH] Add common hash sizes From impls blake2s: * From<[u8; 16]> blake2b: * From<[u8; 16]> * From<[u8; 32]> Both introduce an intermediate buffer in the implementation, but this is a small tradeoff that allows usage of some common hash sizes more idiomatically for the users of this library. --- blake2b/src/lib.rs | 34 ++++++++++++++++++++++++++++++++++ blake2b/src/test.rs | 20 ++++++++++++++++++++ blake2s/src/lib.rs | 17 +++++++++++++++++ blake2s/src/test.rs | 10 ++++++++++ 4 files changed, 81 insertions(+) diff --git a/blake2b/src/lib.rs b/blake2b/src/lib.rs index 6c4f92f..38ac13d 100644 --- a/blake2b/src/lib.rs +++ b/blake2b/src/lib.rs @@ -616,6 +616,40 @@ fn bytes_to_hex(bytes: &[u8]) -> HexString { s } +impl From<[u8; 16]> for Hash { + fn from(bytes: [u8; 16]) -> Self { + let mut buf = [0u8; OUTBYTES]; + buf[0..16].copy_from_slice(&bytes); + Self { + bytes: buf, + len: 16 as u8, + } + } +} + +impl From<&[u8; 16]> for Hash { + fn from(bytes: &[u8; 16]) -> Self { + Self::from(*bytes) + } +} + +impl From<[u8; 32]> for Hash { + fn from(bytes: [u8; 32]) -> Self { + let mut buf = [0u8; OUTBYTES]; + buf[0..32].copy_from_slice(&bytes); + Self { + bytes: buf, + len: 32 as u8, + } + } +} + +impl From<&[u8; 32]> for Hash { + fn from(bytes: &[u8; 32]) -> Self { + Self::from(*bytes) + } +} + impl From<[u8; OUTBYTES]> for Hash { fn from(bytes: [u8; OUTBYTES]) -> Self { Self { diff --git a/blake2b/src/test.rs b/blake2b/src/test.rs index 0c7e5c7..00d9a7d 100644 --- a/blake2b/src/test.rs +++ b/blake2b/src/test.rs @@ -205,4 +205,24 @@ fn test_hash_from() { let h = blake2b(b"foo"); assert_eq!(h, Hash::from(h.as_array())); assert_eq!(h, Hash::from(*h.as_array())); + + let h = crate::Params::new() + .hash_length(16) + .to_state() + .update(b"foo") + .finalize(); + let mut h_s = [0u8; 16]; + h_s[0..16].copy_from_slice(h.as_bytes()); + assert_eq!(h, Hash::from(&h_s)); + assert_eq!(h, Hash::from(h_s)); + + let h = crate::Params::new() + .hash_length(32) + .to_state() + .update(b"foo") + .finalize(); + let mut h_s = [0u8; 32]; + h_s[0..32].copy_from_slice(h.as_bytes()); + assert_eq!(h, Hash::from(&h_s)); + assert_eq!(h, Hash::from(h_s)); } diff --git a/blake2s/src/lib.rs b/blake2s/src/lib.rs index f845e96..82a8929 100644 --- a/blake2s/src/lib.rs +++ b/blake2s/src/lib.rs @@ -608,6 +608,23 @@ fn bytes_to_hex(bytes: &[u8]) -> HexString { s } +impl From<[u8; 16]> for Hash { + fn from(bytes: [u8; 16]) -> Self { + let mut buf = [0u8; OUTBYTES]; + buf[0..16].copy_from_slice(&bytes); + Self { + bytes: buf, + len: 16 as u8, + } + } +} + +impl From<&[u8; 16]> for Hash { + fn from(bytes: &[u8; 16]) -> Self { + Self::from(*bytes) + } +} + impl From<[u8; OUTBYTES]> for Hash { fn from(bytes: [u8; OUTBYTES]) -> Self { Self { diff --git a/blake2s/src/test.rs b/blake2s/src/test.rs index 651cb28..897eddb 100644 --- a/blake2s/src/test.rs +++ b/blake2s/src/test.rs @@ -212,4 +212,14 @@ fn test_hash_from() { let h = blake2s(b"foo"); assert_eq!(h, Hash::from(h.as_array())); assert_eq!(h, Hash::from(*h.as_array())); + + let h = crate::Params::new() + .hash_length(16) + .to_state() + .update(b"foo") + .finalize(); + let mut h_s = [0u8; 16]; + h_s[0..16].copy_from_slice(h.as_bytes()); + assert_eq!(h, Hash::from(&h_s)); + assert_eq!(h, Hash::from(h_s)); }