From 84688c4062a471a8d200727f7444311cf3c1fe8d Mon Sep 17 00:00:00 2001 From: David Senk Date: Mon, 18 Aug 2025 22:35:56 -0400 Subject: [PATCH] working sha3_256 --- src/hashes/sha3_256.rs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/hashes/sha3_256.rs b/src/hashes/sha3_256.rs index 736b1d9..d82b0bc 100644 --- a/src/hashes/sha3_256.rs +++ b/src/hashes/sha3_256.rs @@ -1,20 +1,27 @@ use super::*; +use ::sha3::{Digest, Sha3_256}; pub struct SHA3_256 { hash: BITS256, + hasher: Sha3_256, } impl Hasher for SHA3_256 { fn new() -> Self { - SHA3_256 { hash: [0; 32] } + SHA3_256 { + hash: [0; 32], + hasher: Sha3_256::new(), + } } fn digest(&mut self, bytes: impl AsRef<[u8]>) { - todo!() + self.hasher.update(bytes) } - fn complete(self) -> HashReturn { - todo!() + fn complete(mut self) -> HashReturn { + let res = self.hasher.finalize(); + self.hash.copy_from_slice(&res); + HashReturn::SHA3_256(self.hash) } } @@ -24,6 +31,17 @@ mod test { #[test] fn test_sha3_256_hash() { - todo!(); + let input = "HelloWorld"; + let expected_output = "964b398ecd55793d8ca93e01274efe1377a70c8dc358fdca17cb4e94a9ed7777"; + + let mut sha3_256 = SHA3_256::new(); + + sha3_256.digest(input); + + let result = sha3_256.complete().into_bytes(); + + let hash_out = crate::hex_table::u8_array_to_lower_hex_string(&result).unwrap(); + + assert_eq!(hash_out, expected_output); } }