cas_lib/hashers/
blake2.rs

1
2
3use super::cas_hasher::CASHasher;
4use blake2::{Blake2b512, Blake2s256, Digest};
5
6pub struct CASBlake2;
7
8impl CASHasher for CASBlake2 {
9    /// Hashes data using the Blake2b-512 algorithm.
10    /// Returns the hash as a vector of bytes.
11    fn hash_512(data_to_hash: Vec<u8>) -> Vec<u8> {
12        let mut hasher = Blake2b512::new();
13        hasher.update(data_to_hash);
14        let result = hasher.finalize();
15        return result.to_vec();
16    }
17
18    /// Verifies a hash using the Blake2b-512 algorithm.
19    /// Returns true if the hash matches the data, false otherwise.
20    fn verify_512(hash_to_verify: Vec<u8>, data_to_verify: Vec<u8>) -> bool {
21        let mut hasher = Blake2b512::new();
22        hasher.update(data_to_verify);
23        let result = hasher.finalize();
24        return hash_to_verify.eq(&result.to_vec());
25    }
26
27    /// Hashes data using the Blake2s-256 algorithm.
28    /// Returns the hash as a vector of bytes.
29    fn hash_256(data_to_hash: Vec<u8>) -> Vec<u8> {
30        let mut hasher = Blake2s256::new();
31        hasher.update(data_to_hash);
32        let result = hasher.finalize();
33        return result.to_vec();
34    }
35
36    /// Verifies a hash using the Blake2s-256 algorithm.
37    /// Returns true if the hash matches the data, false otherwise.
38    fn verify_256(hash_to_verify: Vec<u8>, data_to_verify: Vec<u8>) -> bool {
39        let mut hasher = Blake2s256::new();
40        hasher.update(data_to_verify);
41        let result = hasher.finalize();
42        return hash_to_verify.eq(&result.to_vec());
43    }
44}