Add Hash type and finalize method to HashEngine
Add an associated const `Hash` to the `HashEngine` trait. Also add a `finalize` method that converts the engine to the associated hash. For now just use the existent `from_engine` stuff. We can refactor later.
This commit is contained in:
parent
84623ffaf9
commit
ab63b7a0ff
|
@ -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)]
|
||||
|
|
|
@ -92,11 +92,12 @@ impl<T: GeneralHash> HmacEngine<T> {
|
|||
}
|
||||
|
||||
impl<T: GeneralHash> HashEngine for HmacEngine<T> {
|
||||
type Hash = Hmac<T>;
|
||||
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<T: GeneralHash + fmt::Debug> fmt::Debug for Hmac<T> {
|
||||
|
|
|
@ -186,6 +186,9 @@ pub type HkdfSha512 = Hkdf<sha512::Hash>;
|
|||
|
||||
/// 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.
|
||||
|
|
|
@ -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) }
|
||||
}
|
||||
|
|
|
@ -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) }
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -133,9 +133,12 @@ impl<T: Tag> Clone for HashEngine<T> {
|
|||
}
|
||||
|
||||
impl<T: Tag> crate::HashEngine for HashEngine<T> {
|
||||
type Hash = Hash<T>;
|
||||
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!(
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -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) }
|
||||
}
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue