feat: impl sha3 (256-bit & 512-bit)

This commit is contained in:
Erica Marigold 2023-10-15 04:06:23 -07:00
parent 8d5854af3a
commit f05e1ca765
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1
4 changed files with 36 additions and 2 deletions

20
Cargo.lock generated
View file

@ -1070,6 +1070,15 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "keccak"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f6d5ed8676d904364de097082f4e7d240b571b67989ced0240f08b7f966f940"
dependencies = [
"cpufeatures",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
@ -1171,6 +1180,7 @@ dependencies = [
"serde_yaml",
"sha1 0.10.6",
"sha2",
"sha3",
"thiserror",
"tokio",
"tokio-tungstenite",
@ -2107,6 +2117,16 @@ dependencies = [
"digest",
]
[[package]]
name = "sha3"
version = "0.10.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75872d278a8f37ef87fa0ddbda7802605cb18344497949862c0d4dcb291eba60"
dependencies = [
"digest",
"keccak",
]
[[package]]
name = "sharded-slab"
version = "0.1.7"

View file

@ -107,6 +107,7 @@ digest = "0.10.7"
md-5 = "0.10.6"
sha1 = "0.10.6"
sha2 = "0.10.8"
sha3 = "0.10.8"
blake2 = "0.10.6"

View file

@ -44,7 +44,7 @@ macro_rules! impl_hash_algo {
impl Crypto {
$(
paste::item! {
pub fn [<$algo:lower>]<T: ToString>(content: Option<T>) -> Self {
pub fn [<$algo:snake:lower>]<T: ToString>(content: Option<T>) -> Self {
let constructed = Self {
algo: Arc::new(Mutex::new(CryptoAlgo::$algo(Box::new($Type::new())))),
};
@ -68,7 +68,9 @@ impl_hash_algo! {
Sha512: sha2::Sha512,
Md5: md5::Md5,
Blake2s256: blake2::Blake2s256,
Blake2b512: blake2::Blake2b512
Blake2b512: blake2::Blake2b512,
Sha3_256: sha3::Sha3_256,
Sha3_512: sha3::Sha3_512
}
#[derive(Clone)]

View file

@ -35,6 +35,17 @@ pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {
.with_function("blake2b512", |_, content: Option<String>| {
Ok(Crypto::blake2b512(content))
})?
.with_function("sha3", |_, (variant, content): (String, Option<String>)| {
match variant.to_string().as_str() {
"256" => Ok(Crypto::sha3_256(content)),
"512" => Ok(Crypto::sha3_512(content)),
&_ => Err(LuaError::runtime(format!(
"Expected sha3 variant to be 256-bit or 512-bit, got {}",
variant
))),
}
})?
.build()?,
)?
.build_readonly()