summaryrefslogtreecommitdiffstats
path: root/src/hasher.rs
blob: d27a7e54d89600b0c40ff5ea5fd71ae4fe69a7e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//! Provides hashing abstractions and implementations including SHA256 and a default dummy hasher.

/// A trait representing a generic hash function.
///
/// This allows the Merkle tree to use any hash function that conforms to this interface.
pub trait Hasher {
    /// 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.
///
/// Always returns a static hash value.
#[derive(Clone)]
pub struct DummyHasher;

impl Hasher for DummyHasher {
    fn hash(&self, input: &[u8]) -> String {
        let sum: u32 = input.iter().map(|&b| b as u32).sum();
        format!("hash_{:x}", sum)
    }
}

#[cfg(feature = "sha256")]
mod hasher_sha256 {
    use super::*;
    use sha2::{Digest, Sha256};

    #[derive(Clone)]
    /// A hasher implementation using the SHA-256 cryptographic hash function.
    pub struct SHA256Hasher;

    impl SHA256Hasher {
        pub fn new() -> Self {
            Self {}
        }
    }

    impl Hasher for SHA256Hasher {
        fn hash(&self, input: &[u8]) -> String {
            let mut hasher = Sha256::new();
            hasher.update(input);
            hex::encode(hasher.finalize())
        }
    }

    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn test_sha256_hasher_with_known_input() {
            let hasher = SHA256Hasher;
            let input = "hello".as_bytes();
            let expected_hash = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824";
            let actual_hash = hasher.hash(input);
            assert_eq!(actual_hash, expected_hash);
        }

        #[test]
        fn test_sha256_hasher_empty_string() {
            let hasher = SHA256Hasher;
            let input = &[];
            let expected_hash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
            let actual_hash = hasher.hash(input);
            assert_eq!(actual_hash, expected_hash);
        }
    }
}

#[cfg(feature = "sha256")]
pub use hasher_sha256::*;