cas_lib/hashers/
blake2.rs

1use std::sync::mpsc;
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    
45    /// Hashes data using the Blake2b-512 algorithm on the threadpool.
46    /// Returns the hash as a vector of bytes.
47    fn hash_512_threadpool(data_to_hash: Vec<u8>) -> Vec<u8> {
48        let (sender, receiver) = mpsc::channel();
49        rayon::spawn(move || {
50            let result = <CASBlake2 as CASHasher>::hash_512(data_to_hash);
51            sender.send(result).unwrap();
52        });
53        let result = receiver.recv().unwrap();
54        result
55    }
56    
57    /// Verifies a hash using the Blake2b-512 algorithm on the threadpool.
58    /// Returns true if the hash matches the data, false otherwise.
59    fn verify_512_threadpool(hash_to_verify: Vec<u8>, data_to_verify: Vec<u8>) -> bool {
60        let (sender, receiver) = mpsc::channel();
61        rayon::spawn(move || {
62            let result = <CASBlake2 as CASHasher>::verify_512(hash_to_verify, data_to_verify);
63            sender.send(result).unwrap();
64        });
65        let result = receiver.recv().unwrap();
66        result
67    }
68    
69    /// Hashes data using the Blake2s-256 algorithm on the threadpool.
70    /// Returns the hash as a vector of bytes.
71    fn hash_256_threadpool(data_to_hash: Vec<u8>) -> Vec<u8> {
72        let (sender, receiver) = mpsc::channel();
73        rayon::spawn(move || {
74            let result = <CASBlake2 as CASHasher>::hash_256(data_to_hash);
75            sender.send(result).unwrap();
76        });
77        let result = receiver.recv().unwrap();
78        result
79    }
80    
81    /// Verifies a hash using the Blake2s-256 algorithm on the threadpool.
82    /// Returns true if the hash matches the data, false otherwise.
83    fn verify_256_threadpool(hash_to_verify: Vec<u8>, data_to_verify: Vec<u8>) -> bool {
84        let (sender, receiver) = mpsc::channel();
85        rayon::spawn(move || {
86            let result = <CASBlake2 as CASHasher>::verify_256(hash_to_verify, data_to_verify);
87            sender.send(result).unwrap();
88        });
89        let result = receiver.recv().unwrap();
90        result
91    }
92}