cas_lib/password_hashers/
bcrypt.rs

1use std::sync::mpsc;
2
3use bcrypt::{hash, verify, DEFAULT_COST};
4
5use super::cas_password_hasher::CASPasswordHasher;
6
7pub struct CASBCrypt;
8
9impl CASPasswordHasher for CASBCrypt {
10    /// Hashes a password using bcrypt.
11    /// Returns the hashed password as a string.
12    fn hash_password(password_to_hash: String) -> String {
13        return hash(password_to_hash, DEFAULT_COST).unwrap();
14    }
15
16    /// Verifies a password against a hashed password using bcrypt.
17    /// Returns true if the password matches the hashed password, false otherwise.
18    fn verify_password(hashed_password: String, password_to_verify: String) -> bool {
19        return verify(password_to_verify, &hashed_password).unwrap();
20    }
21
22    /// Hashes a password using bcrypt on the threadpool.
23    /// Returns the hashed password as a string.
24    fn hash_password_threadpool(password: String) -> String {
25        let (sender, receiver) = mpsc::channel();
26        rayon::spawn(move || {
27            let hash = Self::hash_password(password);
28            sender.send(hash).unwrap();
29        });
30        let hash = receiver.recv().unwrap();
31        hash
32    }
33
34    /// Verifies a password against a hashed password using bcrypt on the threadpool.
35    /// Returns true if the password matches the hashed password, false otherwise.
36    fn verify_password_threadpool(hashed_password: String, password_to_verify: String) -> bool {
37        let (sender, receiver) = mpsc::channel();
38        rayon::spawn(move || {
39            let hash = Self::verify_password(hashed_password, password_to_verify);
40            sender.send(hash).unwrap();
41        });
42        let hash = receiver.recv().unwrap();
43        hash
44    }
45}