Take iterator in calculate_hash()

Avoids unnecessary allocations.
This commit is contained in:
Lambda 2024-08-26 19:16:51 +00:00
parent cce83beedb
commit f1642c92d1
5 changed files with 24 additions and 33 deletions

View file

@ -106,9 +106,18 @@ where
}
#[tracing::instrument(skip(keys))]
pub(crate) fn calculate_hash(keys: &[&[u8]]) -> Vec<u8> {
// We only hash the pdu's event ids, not the whole pdu
let bytes = keys.join(&0xFF);
pub(crate) fn calculate_hash<'a, I, T>(keys: I) -> Vec<u8>
where
I: IntoIterator<Item = T>,
T: AsRef<[u8]>,
{
let mut bytes = Vec::new();
for (i, key) in keys.into_iter().enumerate() {
if i != 0 {
bytes.push(0xFF);
}
bytes.extend_from_slice(key.as_ref());
}
let hash = digest::digest(&digest::SHA256, &bytes);
hash.as_ref().to_owned()
}