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:
Tobin C. Harding 2025-02-20 10:46:08 +11:00
parent 84623ffaf9
commit ab63b7a0ff
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
12 changed files with 33 additions and 6 deletions

View File

@ -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)]

View File

@ -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> {

View File

@ -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.

View File

@ -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) }
}

View File

@ -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) }
}

View File

@ -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 {

View File

@ -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)]

View File

@ -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!(

View File

@ -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)]

View File

@ -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) }
}

View File

@ -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)]

View File

@ -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 {