Skip to content

Commit

Permalink
Merge pull request #258 from dadleyy/prefer-iter
Browse files Browse the repository at this point in the history
perf: avoid `concat` in sha1 checksum impl
  • Loading branch information
dignifiedquire committed Dec 9, 2023
2 parents 63f55a7 + 036943b commit d907163
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/crypto/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ impl Hasher for SimpleChecksum {

/// SHA1 checksum, first 20 octets.
#[inline]
pub fn calculate_sha1(data: &[u8]) -> Vec<u8> {
Sha1::digest(data)[..20].to_vec()
pub fn calculate_sha1<I, T>(data: I) -> Vec<u8>
where
T: AsRef<[u8]>,
I: IntoIterator<Item = T>,
{
let mut digest = Sha1::new();
for chunk in data {
digest.update(chunk.as_ref());
}
digest.finalize()[..20].to_vec()
}
2 changes: 1 addition & 1 deletion src/crypto/sym.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl SymmetricKeyAlgorithm {
let mdc_len = 22;
let (data, mdc) = res.split_at(res.len() - mdc_len);

let sha1 = checksum::calculate_sha1(&[prefix, data, &mdc[0..2]].concat());
let sha1 = checksum::calculate_sha1([prefix, data, &mdc[0..2]]);
if mdc[0] != 0xD3 || // Invalid MDC tag
mdc[1] != 0x14 || // Invalid MDC length
mdc[2..] != sha1[..]
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub mod ser;
pub mod types;

// reexports for easier use
#[allow(unused_imports)]
pub use self::composed::key::*;
pub use self::composed::*;
pub use self::packet::Signature;
2 changes: 1 addition & 1 deletion src/types/params/plain_secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl<'a> PlainSecretParamsRef<'a> {
pub fn checksum_sha1(&self) -> Vec<u8> {
let mut buf = Vec::new();
self.to_writer_raw(&mut buf).expect("known write target");
checksum::calculate_sha1(&buf)
checksum::calculate_sha1([&buf])
}

pub fn as_repr(&self, public_params: &PublicParams) -> Result<SecretKeyRepr> {
Expand Down

0 comments on commit d907163

Please sign in to comment.