Skip to main content

cas_lib/hashers/
sha2.rs

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