Skip to content

Commit 2b6ef31

Browse files
committed
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`.
1 parent d384689 commit 2b6ef31

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

hashes/src/internal_macros.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,21 @@ macro_rules! general_hash_type {
8080
engine.finalize()
8181
}
8282

83+
/// Hashes all the byte slices retrieved from the iterator together.
84+
pub fn hash_byte_chunks<B, I>(byte_slices: I) -> Hash
85+
where
86+
B: AsRef<[u8]>,
87+
I: IntoIterator<Item = B>,
88+
{
89+
use crate::HashEngine as _;
90+
91+
let mut engine = Hash::engine();
92+
for slice in byte_slices {
93+
engine.input(slice.as_ref());
94+
}
95+
engine.finalize()
96+
}
97+
8398
$crate::internal_macros::hash_type_no_default!($bits, $reverse, $doc);
8499

85100
impl Hash {
@@ -99,7 +114,7 @@ macro_rules! general_hash_type {
99114
B: AsRef<[u8]>,
100115
I: IntoIterator<Item = B>,
101116
{
102-
<Self as $crate::GeneralHash>::hash_byte_chunks(byte_slices)
117+
hash_byte_chunks(byte_slices)
103118
}
104119
}
105120
};

hashes/src/sha256t/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ where
2121
engine.finalize()
2222
}
2323

24+
/// Hashes all the byte slices retrieved from the iterator together.
25+
pub fn hash_byte_chunks<B, I, T>(byte_slices: I) -> Hash<T>
26+
where
27+
B: AsRef<[u8]>,
28+
I: IntoIterator<Item = B>,
29+
T: Tag,
30+
{
31+
use crate::HashEngine as _;
32+
33+
let mut engine = HashEngine::default();
34+
for slice in byte_slices {
35+
engine.input(slice.as_ref());
36+
}
37+
engine.finalize()
38+
}
39+
2440
/// Trait representing a tag that can be used as a context for SHA256t hashes.
2541
pub trait Tag {
2642
/// The [`Midstate`] after pre-tagging the hash engine.

0 commit comments

Comments
 (0)