cas_lib/hashers/
sha.rs

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