diff --git a/hashes/src/hash160/mod.rs b/hashes/src/hash160/mod.rs index 282d653d7..328ad9513 100644 --- a/hashes/src/hash160/mod.rs +++ b/hashes/src/hash160/mod.rs @@ -38,9 +38,12 @@ impl Default for HashEngine { } impl crate::HashEngine for HashEngine { + type Hash = Hash; const BLOCK_SIZE: usize = 64; // Same as sha256::HashEngine::BLOCK_SIZE; + fn input(&mut self, data: &[u8]) { self.0.input(data) } fn n_bytes_hashed(&self) -> u64 { self.0.n_bytes_hashed() } + fn finalize(self) -> Self::Hash { Hash::from_engine(self) } } #[cfg(test)] diff --git a/hashes/src/hmac/mod.rs b/hashes/src/hmac/mod.rs index 8215e48a7..fa50389db 100644 --- a/hashes/src/hmac/mod.rs +++ b/hashes/src/hmac/mod.rs @@ -92,11 +92,12 @@ impl HmacEngine { } impl HashEngine for HmacEngine { + type Hash = Hmac; const BLOCK_SIZE: usize = T::Engine::BLOCK_SIZE; fn n_bytes_hashed(&self) -> u64 { self.iengine.n_bytes_hashed() } - fn input(&mut self, buf: &[u8]) { self.iengine.input(buf) } + fn finalize(self) -> Self::Hash { Hmac::from_engine(self) } } impl fmt::Debug for Hmac { diff --git a/hashes/src/lib.rs b/hashes/src/lib.rs index 06cfbcda3..329273b94 100644 --- a/hashes/src/lib.rs +++ b/hashes/src/lib.rs @@ -186,6 +186,9 @@ pub type HkdfSha512 = Hkdf; /// A hashing engine which bytes can be serialized into. pub trait HashEngine: Clone { + /// The `Hash` type returned when finalizing this engine. + type Hash: Hash; + /// Length of the hash's internal block size, in bytes. const BLOCK_SIZE: usize; @@ -194,6 +197,9 @@ pub trait HashEngine: Clone { /// Return the number of bytes already input into the engine. fn n_bytes_hashed(&self) -> u64; + + /// Finalizes this engine. + fn finalize(self) -> Self::Hash; } /// Trait describing hash digests which can be constructed by hashing arbitrary data. diff --git a/hashes/src/ripemd160/mod.rs b/hashes/src/ripemd160/mod.rs index e6be17537..c91dc8b68 100644 --- a/hashes/src/ripemd160/mod.rs +++ b/hashes/src/ripemd160/mod.rs @@ -87,9 +87,10 @@ impl Default for HashEngine { } impl crate::HashEngine for HashEngine { + type Hash = Hash; const BLOCK_SIZE: usize = 64; fn n_bytes_hashed(&self) -> u64 { self.bytes_hashed } - crate::internal_macros::engine_input_impl!(); + fn finalize(self) -> Self::Hash { Hash::from_engine(self) } } diff --git a/hashes/src/sha1/mod.rs b/hashes/src/sha1/mod.rs index dc32d31dd..6abbf0a9c 100644 --- a/hashes/src/sha1/mod.rs +++ b/hashes/src/sha1/mod.rs @@ -79,9 +79,12 @@ impl Default for HashEngine { } impl crate::HashEngine for HashEngine { + type Hash = Hash; const BLOCK_SIZE: usize = 64; fn n_bytes_hashed(&self) -> u64 { self.bytes_hashed } crate::internal_macros::engine_input_impl!(); + + fn finalize(self) -> Self::Hash { Hash::from_engine(self) } } diff --git a/hashes/src/sha256/mod.rs b/hashes/src/sha256/mod.rs index 6ae83b879..fd40401bd 100644 --- a/hashes/src/sha256/mod.rs +++ b/hashes/src/sha256/mod.rs @@ -128,11 +128,12 @@ impl Default for HashEngine { } impl crate::HashEngine for HashEngine { + type Hash = Hash; const BLOCK_SIZE: usize = 64; fn n_bytes_hashed(&self) -> u64 { self.bytes_hashed } - crate::internal_macros::engine_input_impl!(); + fn finalize(self) -> Self::Hash { Hash::from_engine(self) } } impl Hash { diff --git a/hashes/src/sha256d/mod.rs b/hashes/src/sha256d/mod.rs index adb2ffa71..313760852 100644 --- a/hashes/src/sha256d/mod.rs +++ b/hashes/src/sha256d/mod.rs @@ -33,9 +33,12 @@ impl Default for HashEngine { } impl crate::HashEngine for HashEngine { + type Hash = Hash; const BLOCK_SIZE: usize = 64; // Same as sha256::HashEngine::BLOCK_SIZE; + fn input(&mut self, data: &[u8]) { self.0.input(data) } fn n_bytes_hashed(&self) -> u64 { self.0.n_bytes_hashed() } + fn finalize(self) -> Self::Hash { Hash::from_engine(self) } } #[cfg(test)] diff --git a/hashes/src/sha256t/mod.rs b/hashes/src/sha256t/mod.rs index 19a6ef768..7b58bc991 100644 --- a/hashes/src/sha256t/mod.rs +++ b/hashes/src/sha256t/mod.rs @@ -133,9 +133,12 @@ impl Clone for HashEngine { } impl crate::HashEngine for HashEngine { + type Hash = Hash; const BLOCK_SIZE: usize = 64; // Same as sha256::HashEngine::BLOCK_SIZE; + fn input(&mut self, data: &[u8]) { self.0.input(data) } fn n_bytes_hashed(&self) -> u64 { self.0.n_bytes_hashed() } + fn finalize(self) -> Self::Hash { Hash::from_engine(self) } } crate::internal_macros::impl_write!( diff --git a/hashes/src/sha384/mod.rs b/hashes/src/sha384/mod.rs index c20b356dc..9918afa2f 100644 --- a/hashes/src/sha384/mod.rs +++ b/hashes/src/sha384/mod.rs @@ -30,11 +30,12 @@ impl Default for HashEngine { } impl crate::HashEngine for HashEngine { + type Hash = Hash; const BLOCK_SIZE: usize = sha512::BLOCK_SIZE; fn n_bytes_hashed(&self) -> u64 { self.0.n_bytes_hashed() } - fn input(&mut self, inp: &[u8]) { self.0.input(inp); } + fn finalize(self) -> Self::Hash { Hash::from_engine(self) } } #[cfg(test)] diff --git a/hashes/src/sha512/mod.rs b/hashes/src/sha512/mod.rs index 65bf75c18..617f34cd8 100644 --- a/hashes/src/sha512/mod.rs +++ b/hashes/src/sha512/mod.rs @@ -120,9 +120,10 @@ impl HashEngine { } impl crate::HashEngine for HashEngine { + type Hash = Hash; const BLOCK_SIZE: usize = 128; fn n_bytes_hashed(&self) -> u64 { self.bytes_hashed } - crate::internal_macros::engine_input_impl!(); + fn finalize(self) -> Self::Hash { Hash::from_engine(self) } } diff --git a/hashes/src/sha512_256/mod.rs b/hashes/src/sha512_256/mod.rs index 40b4ca651..04e6d9be3 100644 --- a/hashes/src/sha512_256/mod.rs +++ b/hashes/src/sha512_256/mod.rs @@ -40,11 +40,12 @@ impl Default for HashEngine { } impl crate::HashEngine for HashEngine { + type Hash = Hash; const BLOCK_SIZE: usize = sha512::BLOCK_SIZE; fn n_bytes_hashed(&self) -> u64 { self.0.n_bytes_hashed() } - fn input(&mut self, inp: &[u8]) { self.0.input(inp); } + fn finalize(self) -> Self::Hash { Hash::from_engine(self) } } #[cfg(test)] diff --git a/hashes/src/siphash24/mod.rs b/hashes/src/siphash24/mod.rs index 1db747733..4f8de24ff 100644 --- a/hashes/src/siphash24/mod.rs +++ b/hashes/src/siphash24/mod.rs @@ -121,6 +121,7 @@ impl HashEngine { } impl crate::HashEngine for HashEngine { + type Hash = Hash; const BLOCK_SIZE: usize = 8; #[inline] @@ -165,6 +166,8 @@ impl crate::HashEngine for HashEngine { } fn n_bytes_hashed(&self) -> u64 { self.bytes_hashed } + + fn finalize(self) -> Self::Hash { Hash::from_engine(self) } } impl Hash {