diff options
author | Andreas Grois <andi@grois.info> | 2023-01-20 00:16:57 +0100 |
---|---|---|
committer | Andreas Grois <andi@grois.info> | 2023-01-20 00:16:57 +0100 |
commit | ad1c6728521a2a89ee790cd12baaea73aa6041a2 (patch) | |
tree | 78078875d0168f69aa51e596bcd073c5b93b943f /src/passwordmaker/hmac.rs | |
parent | 26b9bc316d8db76b7840f5598877590310a9f49f (diff) |
Add benchmark for HMAC code.
Also, simplify the function a bit. It's faster this way, even though
there's an additional unconditional collect() call now.
Diffstat (limited to 'src/passwordmaker/hmac.rs')
-rw-r--r-- | src/passwordmaker/hmac.rs | 42 |
1 files changed, 5 insertions, 37 deletions
diff --git a/src/passwordmaker/hmac.rs b/src/passwordmaker/hmac.rs index 4c9d6aa..62b77fd 100644 --- a/src/passwordmaker/hmac.rs +++ b/src/passwordmaker/hmac.rs @@ -3,15 +3,13 @@ use crate::Hasher; pub(super) fn hmac<T, K, M>(key : K, data : M) -> T::Output where T : Hasher, T::Output : AsRef<[u8]>, - K : Iterator<Item=u8> + Clone, + K : Iterator<Item=u8>, M : Iterator<Item=u8>, { - let key_len = key.clone().count(); - let key = if key_len > 64 { - KeyOrHash::from_hash(T::hash(&key.collect::<Vec<_>>())) - } else { - KeyOrHash::from_key(key) - }; + let key = key.collect::<Vec<_>>(); + let key_hash = if key.len() > 64 { Some(T::hash(&key)) } else { None }; + let key = key_hash.as_ref().map(T::Output::as_ref).map(<&[u8]>::into_iter).unwrap_or_else(|| (&key).into_iter()).copied(); + let key = key.chain(std::iter::repeat(0)); //if key[i] does not exist, use 0 instead. let mut inner_pad = [0u8;64]; @@ -25,34 +23,4 @@ pub(super) fn hmac<T, K, M>(key : K, data : M) -> T::Output let hash = T::hash(&inner_pad.iter().copied().chain(data).collect::<Vec<_>>()); T::hash(&outer_pad.iter().chain(hash.as_ref().iter()).copied().collect::<Vec<_>>()) -} - -enum KeyOrHash<K: Iterator<Item=u8>, H: AsRef<[u8]>> { - Key(K), - Hash{ - hash : H, - idx : usize - } -} - -impl<K: Iterator<Item=u8>, H: AsRef<[u8]>> KeyOrHash<K, H>{ - fn from_key(key : K) -> Self { - Self::Key(key) - } - fn from_hash(hash : H) -> Self { - Self::Hash { hash, idx: 0 } - } -} - -impl<K: Iterator<Item=u8>, H: AsRef<[u8]>> Iterator for KeyOrHash<K, H>{ - type Item = u8; - fn next(&mut self) -> Option<Self::Item> { - match self { - KeyOrHash::Key(k) => k.next(), - KeyOrHash::Hash { hash: owned, idx } => { - *idx += 1; - owned.as_ref().get(*idx-1).copied() - }, - } - } }
\ No newline at end of file |