refactor(nix-compat/nixhash): box sha512

Limit the amount of memory consumed on the stack for NixHash. Sha512
isn't used that often, so it's fine if we heap-allocate it.

Change-Id: I4a9eecd20c6184610124dc130c41bfa5d0dc04c5
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9726
Tested-by: BuildkiteCI
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: Connor Brewster <cbrewster@hey.com>
This commit is contained in:
Florian Klink 2023-10-14 21:23:31 +01:00 committed by clbot
parent e3d72cc4cb
commit cb1a14334a

View file

@ -14,7 +14,7 @@ pub enum NixHash {
Md5([u8; 16]),
Sha1([u8; 20]),
Sha256([u8; 32]),
Sha512([u8; 64]),
Sha512(Box<[u8; 64]>),
}
impl NixHash {
@ -34,7 +34,7 @@ impl NixHash {
NixHash::Md5(digest) => digest,
NixHash::Sha1(digest) => digest,
NixHash::Sha256(digest) => digest,
NixHash::Sha512(digest) => digest,
NixHash::Sha512(digest) => digest.as_ref(),
}
}
@ -73,7 +73,7 @@ pub fn from_algo_and_digest(algo: HashAlgo, digest: &[u8]) -> Result<NixHash, Er
HashAlgo::Md5 => NixHash::Md5(digest.try_into().unwrap()),
HashAlgo::Sha1 => NixHash::Sha1(digest.try_into().unwrap()),
HashAlgo::Sha256 => NixHash::Sha256(digest.try_into().unwrap()),
HashAlgo::Sha512 => NixHash::Sha512(digest.try_into().unwrap()),
HashAlgo::Sha512 => NixHash::Sha512(Box::new(digest.try_into().unwrap())),
})
}
@ -307,7 +307,7 @@ mod tests {
/// Test parsing a hash string in various formats, and also when/how the out-of-band algo is needed.
#[test_case(&NixHash::Sha1(DIGEST_SHA1); "sha1")]
#[test_case(&NixHash::Sha256(DIGEST_SHA256); "sha256")]
#[test_case(&NixHash::Sha512(DIGEST_SHA512); "sha512")]
#[test_case(&NixHash::Sha512(Box::new(DIGEST_SHA512)); "sha512")]
#[test_case(&NixHash::Md5(DIGEST_MD5); "md5")]
fn from_str(expected_hash: &NixHash) {
let algo = &expected_hash.algo();