cas_lib/password_hashers/
bcrypt.rs

1
2
3use bcrypt::{hash, verify, DEFAULT_COST};
4
5pub struct CASBCrypt;
6
7impl CASBCrypt {
8    /// Hashes a password using bcrypt with a customized cost.
9    /// Parameters:
10    /// - password_to_hash: The password to be hashed.
11    /// - cost: The cost parameter for bcrypt (default is 12 and max is 31).
12    /// Returns the hashed password as a string.
13    pub fn hash_password_customized(password_to_hash: String, cost: u32) -> String {
14        return hash(password_to_hash, cost).unwrap();
15    }
16
17    /// Hashes a password using bcrypt.
18    /// Returns the hashed password as a string.
19    pub fn hash_password(password_to_hash: String) -> String {
20        return hash(password_to_hash, DEFAULT_COST).unwrap();
21    }
22
23    /// Verifies a password against a hashed password using bcrypt.
24    /// Returns true if the password matches the hashed password, false otherwise.
25    pub fn verify_password(hashed_password: String, password_to_verify: String) -> bool {
26        return verify(password_to_verify, &hashed_password).unwrap();
27    }
28}