diff options
| author | Santo Cariotti <santo@dcariotti.me> | 2025-06-16 09:07:20 +0000 |
|---|---|---|
| committer | Santo Cariotti <santo@dcariotti.me> | 2025-06-16 09:07:20 +0000 |
| commit | bb3eadc0756062e82a6feb73d4bae5bd1cb18917 (patch) | |
| tree | f1e89b1552e3a9e20f7b55a70d83262625362d37 /src/hasher.rs | |
| parent | c470ba5f7b1a85bfd1cd96e5ec14d2f42a25ec55 (diff) | |
Use bytes instead of string for data values
Diffstat (limited to 'src/hasher.rs')
| -rw-r--r-- | src/hasher.rs | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/src/hasher.rs b/src/hasher.rs index a26dd3c..101b23c 100644 --- a/src/hasher.rs +++ b/src/hasher.rs @@ -7,8 +7,8 @@ use sha2::{Digest, Sha256}; /// /// This allows the Merkle tree to use any hash function that conforms to this interface. pub trait Hasher { - /// Hashes an input string and returns the resulting hash as a hexadecimal string. - fn hash(&self, input: &str) -> String; + /// Hashes a sequence of bytes and returns the resulting hash as a hexadecimal string. + fn hash(&self, input: &[u8]) -> String; } /// A dummy hasher used for testing or demonstration purposes. @@ -17,7 +17,7 @@ pub trait Hasher { pub struct DefaultHasher; impl Hasher for DefaultHasher { - fn hash(&self, _input: &str) -> String { + fn hash(&self, _input: &[u8]) -> String { "0xc0ff3".to_string() } } @@ -28,9 +28,9 @@ pub struct SHA256Hasher; #[cfg(feature = "sha256")] impl Hasher for SHA256Hasher { - fn hash(&self, input: &str) -> String { + fn hash(&self, input: &[u8]) -> String { let mut hasher = Sha256::new(); - hasher.update(input.as_bytes()); + hasher.update(input); hex::encode(hasher.finalize()) } } @@ -42,7 +42,7 @@ mod tests { #[test] fn test_sha256_hasher_with_known_input() { let hasher = SHA256Hasher; - let input = "hello"; + let input = "hello".as_bytes(); let expected_hash = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"; let actual_hash = hasher.hash(input); assert_eq!(actual_hash, expected_hash); @@ -51,7 +51,7 @@ mod tests { #[test] fn test_sha256_hasher_empty_string() { let hasher = SHA256Hasher; - let input = ""; + let input = &[]; let expected_hash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; let actual_hash = hasher.hash(input); assert_eq!(actual_hash, expected_hash); |
