password_worker/hasher_impls/
bcrypt.rs

1use crate::{Hasher, PasswordWorker, PasswordWorkerError};
2
3/// Use this type in the generic constructor to use bcrypt
4#[derive(Clone, Copy, Debug)]
5pub enum Bcrypt {}
6
7impl Hasher for Bcrypt {
8    type Config = BcryptConfig;
9    type Error = bcrypt::BcryptError;
10
11    fn hash(data: impl AsRef<[u8]>, config: &Self::Config) -> Result<String, Self::Error> {
12        bcrypt::hash(data, config.cost)
13    }
14
15    fn verify(data: impl AsRef<[u8]>, hash: &str) -> Result<bool, Self::Error> {
16        bcrypt::verify(data, hash)
17    }
18}
19
20/// The configuration attributes needed to perform bcrypt hashing
21#[derive(Clone, Copy)]
22pub struct BcryptConfig {
23    /// Cost for hashing (higher takes longer)
24    pub cost: u32,
25}
26
27impl PasswordWorker<Bcrypt> {
28    /// This constructor creates a new bcrypt instance
29    pub fn new_bcrypt(max_threads: usize) -> Result<Self, PasswordWorkerError<Bcrypt>> {
30        PasswordWorker::<Bcrypt>::new(max_threads)
31    }
32}