hashes: Add hash_byte_chunks function to modules

Add a standalone `hash_byte_chunks` function that is a drop in
 replacement for `GeneralHash::hash_byte_chunks`. Do not add it to
 `hmac` - this is in parity with the current code because `Hmac` does
 not implement `GeneralHash::hash_byte_chunks`.
This commit is contained in:
Tobin C. Harding 2025-02-20 13:53:47 +11:00
parent d3846895d7
commit 2b6ef31469
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
2 changed files with 32 additions and 1 deletions

View File

@ -80,6 +80,21 @@ macro_rules! general_hash_type {
engine.finalize()
}
/// Hashes all the byte slices retrieved from the iterator together.
pub fn hash_byte_chunks<B, I>(byte_slices: I) -> Hash
where
B: AsRef<[u8]>,
I: IntoIterator<Item = B>,
{
use crate::HashEngine as _;
let mut engine = Hash::engine();
for slice in byte_slices {
engine.input(slice.as_ref());
}
engine.finalize()
}
$crate::internal_macros::hash_type_no_default!($bits, $reverse, $doc);
impl Hash {
@ -99,7 +114,7 @@ macro_rules! general_hash_type {
B: AsRef<[u8]>,
I: IntoIterator<Item = B>,
{
<Self as $crate::GeneralHash>::hash_byte_chunks(byte_slices)
hash_byte_chunks(byte_slices)
}
}
};

View File

@ -21,6 +21,22 @@ where
engine.finalize()
}
/// Hashes all the byte slices retrieved from the iterator together.
pub fn hash_byte_chunks<B, I, T>(byte_slices: I) -> Hash<T>
where
B: AsRef<[u8]>,
I: IntoIterator<Item = B>,
T: Tag,
{
use crate::HashEngine as _;
let mut engine = HashEngine::default();
for slice in byte_slices {
engine.input(slice.as_ref());
}
engine.finalize()
}
/// Trait representing a tag that can be used as a context for SHA256t hashes.
pub trait Tag {
/// The [`Midstate`] after pre-tagging the hash engine.