fix: innaccurate hash

This commit is contained in:
Erica Marigold 2023-10-14 23:32:56 -07:00
parent 3dda0889f2
commit 40cfc59f75
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1

View file

@ -70,13 +70,28 @@ impl FromLua<'_> for EncodingKind {
} }
impl CryptoAlgo { impl CryptoAlgo {
pub fn update(&self, content: impl AsRef<[u8]>) { pub fn update(&mut self, content: impl AsRef<[u8]>) {
// TODO: Replace boilerplate using a macro // TODO: Replace boilerplate using a macro
match self { match self {
CryptoAlgo::Sha1(hasher) => sha1::Digest::update(&mut (**hasher).clone(), content), CryptoAlgo::Sha1(hasher) => {
CryptoAlgo::Sha256(hasher) => sha2::Digest::update(&mut (**hasher).clone(), content), let mut new_hasher = (**hasher).clone();
CryptoAlgo::Sha512(hasher) => sha2::Digest::update(&mut (**hasher).clone(), content), sha1::Digest::update(&mut new_hasher, content);
*self = CryptoAlgo::Sha1(Box::new(new_hasher))
}
CryptoAlgo::Sha256(hasher) => {
let mut new_hasher = (**hasher).clone();
sha2::Digest::update(&mut new_hasher, content);
*self = CryptoAlgo::Sha256(Box::new(new_hasher))
}
CryptoAlgo::Sha512(hasher) => {
let mut new_hasher = (**hasher).clone();
sha2::Digest::update(&mut new_hasher, content);
*self = CryptoAlgo::Sha512(Box::new(new_hasher))
}
}; };
} }
@ -99,10 +114,12 @@ impl CryptoAlgo {
} }
}; };
println!("{:#?}", computed);
match encoding { match encoding {
EncodingKind::Utf8 => String::from_utf8(computed).map_err(anyhow::Error::from), EncodingKind::Utf8 => String::from_utf8(computed).map_err(anyhow::Error::from),
EncodingKind::Base64 => Ok(Base64::STANDARD.encode(computed)), EncodingKind::Base64 => Ok(Base64::STANDARD.encode(computed)),
EncodingKind::Hex => Ok(hex::encode::<&[u8]>(&computed)), EncodingKind::Hex => Ok(hex::encode(&computed)),
} }
} }
} }