From 2b6ef31469edd93d59655afa41f3130f3ab058ba Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 20 Feb 2025 13:53:47 +1100 Subject: [PATCH] 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`. --- hashes/src/internal_macros.rs | 17 ++++++++++++++++- hashes/src/sha256t/mod.rs | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/hashes/src/internal_macros.rs b/hashes/src/internal_macros.rs index 41ffab593..fee359a9e 100644 --- a/hashes/src/internal_macros.rs +++ b/hashes/src/internal_macros.rs @@ -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(byte_slices: I) -> Hash + where + B: AsRef<[u8]>, + I: IntoIterator, + { + 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, { - ::hash_byte_chunks(byte_slices) + hash_byte_chunks(byte_slices) } } }; diff --git a/hashes/src/sha256t/mod.rs b/hashes/src/sha256t/mod.rs index be144f2f2..d5acd5edf 100644 --- a/hashes/src/sha256t/mod.rs +++ b/hashes/src/sha256t/mod.rs @@ -21,6 +21,22 @@ where engine.finalize() } +/// Hashes all the byte slices retrieved from the iterator together. +pub fn hash_byte_chunks(byte_slices: I) -> Hash +where + B: AsRef<[u8]>, + I: IntoIterator, + 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.