summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2025-06-23 07:39:36 +0000
committerSanto Cariotti <santo@dcariotti.me>2025-06-23 07:39:36 +0000
commitbe8ec280a056be688913c1a35c589ef406f72f50 (patch)
tree959996d979443483576c9054ab28824995d18c2b /src
parentb1ca877d924b15404be1d51df4bddb571d69a5be (diff)
Add `Keccak512Hasher`
Diffstat (limited to 'src')
-rw-r--r--src/hasher.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/hasher.rs b/src/hasher.rs
index 088b16a..7692e0f 100644
--- a/src/hasher.rs
+++ b/src/hasher.rs
@@ -1,6 +1,7 @@
//! Provides hashing abstractions and implementations including SHA256 and a default dummy hasher.
use sha2::{Digest, Sha256};
+use sha3::Keccak512;
/// A trait representing a generic hash function.
///
@@ -47,6 +48,30 @@ impl Hasher for SHA256Hasher {
}
}
+#[derive(Clone)]
+/// A hasher implementation using the Keccak512 cryptographic hash function.
+pub struct Keccak512Hasher;
+
+impl Default for Keccak512Hasher {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl Keccak512Hasher {
+ pub fn new() -> Self {
+ Self {}
+ }
+}
+
+impl Hasher for Keccak512Hasher {
+ fn hash(&self, input: &[u8]) -> String {
+ let mut hasher = Keccak512::new();
+ hasher.update(input);
+ hex::encode(hasher.finalize())
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -68,4 +93,22 @@ mod tests {
let actual_hash = hasher.hash(input);
assert_eq!(actual_hash, expected_hash);
}
+
+ #[test]
+ fn test_keccak512_hasher_with_known_input() {
+ let hasher = Keccak512Hasher;
+ let input = "hello".as_bytes();
+ let expected_hash = "52fa80662e64c128f8389c9ea6c73d4c02368004bf4463491900d11aaadca39d47de1b01361f207c512cfa79f0f92c3395c67ff7928e3f5ce3e3c852b392f976";
+ let actual_hash = hasher.hash(input);
+ assert_eq!(actual_hash, expected_hash);
+ }
+
+ #[test]
+ fn test_keccak512_hasher_empty_string() {
+ let hasher = Keccak512Hasher;
+ let input = &[];
+ let expected_hash = "0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e";
+ let actual_hash = hasher.hash(input);
+ assert_eq!(actual_hash, expected_hash);
+ }
}