cas_lib/hashers/
sha.rs

1use std::sync::mpsc;
2
3use super::cas_hasher::CASHasher;
4use sha3::{Digest, Sha3_256, Sha3_512};
5pub struct CASSHA;
6
7impl CASHasher for CASSHA {
8    /// Hashes data using the SHA-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 = Sha3_512::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 SHA-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 = Sha3_512::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 SHA-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 = Sha3_256::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 SHA-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 = Sha3_256::new();
39        hasher.update(data_to_verify);
40        let result = hasher.finalize();
41        return hash_to_verify.eq(&result.to_vec());
42    }
43    
44    /// Hashes data using the SHA-512 algorithm on the threadpool.
45    /// Returns the hash as a vector of bytes.
46    fn hash_512_threadpool(data_to_hash: Vec<u8>) -> Vec<u8> {
47        let (sender, receiver) = mpsc::channel();
48        rayon::spawn(move || {
49            let result = <CASSHA as CASHasher>::hash_512(data_to_hash);
50            sender.send(result).unwrap();
51        });
52        let result = receiver.recv().unwrap();
53        result
54    }
55    
56    /// Verifies a hash using the SHA-512 algorithm on the threadpool.
57    /// Returns true if the hash matches the data, false otherwise.
58    fn verify_512_threadpool(hash_to_verify: Vec<u8>, data_to_verify: Vec<u8>) -> bool {
59        let (sender, receiver) = mpsc::channel();
60        rayon::spawn(move || {
61            let result = <CASSHA as CASHasher>::verify_512(hash_to_verify, data_to_verify);
62            sender.send(result).unwrap();
63        });
64        let result = receiver.recv().unwrap();
65        result
66    }
67    
68    /// Hashes data using the SHA-256 algorithm on the threadpool.
69    /// Returns the hash as a vector of bytes.
70    fn hash_256_threadpool(data_to_hash: Vec<u8>) -> Vec<u8> {
71        let (sender, receiver) = mpsc::channel();
72        rayon::spawn(move || {
73            let result = <CASSHA as CASHasher>::hash_256(data_to_hash);
74            sender.send(result).unwrap();
75        });
76        let result = receiver.recv().unwrap();
77        result
78    }
79    
80    /// Verifies a hash using the SHA-256 algorithm on the threadpool.
81    /// Returns true if the hash matches the data, false otherwise.
82    fn verify_256_threadpool(hash_to_verify: Vec<u8>, data_to_verify: Vec<u8>) -> bool {
83        let (sender, receiver) = mpsc::channel();
84        rayon::spawn(move || {
85            let result = <CASSHA as CASHasher>::verify_256(hash_to_verify, data_to_verify);
86            sender.send(result).unwrap();
87        });
88        let result = receiver.recv().unwrap();
89        result
90    }
91}