summaryrefslogtreecommitdiffstats
path: root/src/hasher.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/hasher.rs')
-rw-r--r--src/hasher.rs14
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);