Skip to content

Commit 2ae186d

Browse files
committed
Add helper functions to Hmac
As we do for other hash types (by way of the `GeneralHash` trait) add two helper functions to the `Hmac` type. Note we need a key and it has the same type as the data being hashed - possibly confusing.
1 parent b8f9ce6 commit 2ae186d

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

hashes/src/hmac/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,30 @@ impl<T: GeneralHash> Hmac<T> {
3535
{
3636
HmacEngine::new(key)
3737
}
38+
39+
/// Hashes some bytes.
40+
pub fn hash(key: &[u8], data: &[u8]) -> Self
41+
where
42+
<T as GeneralHash>::Engine: Default,
43+
{
44+
let mut engine = HmacEngine::new(key);
45+
engine.input(data);
46+
Self::from_engine(engine)
47+
}
48+
49+
/// Hashes all the byte slices retrieved from the iterator together.
50+
pub fn hash_byte_chunks<B, I>(key: &[u8], byte_slices: I) -> Self
51+
where
52+
<T as GeneralHash>::Engine: Default,
53+
B: AsRef<[u8]>,
54+
I: IntoIterator<Item = B>,
55+
{
56+
let mut engine = HmacEngine::new(key);
57+
for slice in byte_slices {
58+
engine.input(slice.as_ref());
59+
}
60+
Self::from_engine(engine)
61+
}
3862
}
3963

4064
impl<T: GeneralHash + str::FromStr> str::FromStr for Hmac<T> {

0 commit comments

Comments
 (0)