wrapper_bcrypt/
wrapper.rs

1use bcrypt::BcryptError;
2use darth_rust::DarthRust;
3type BcryptHashResult = Result<String, BcryptError>;
4type BcryptVerifyResult = Result<bool, BcryptError>;
5
6#[derive(Debug, DarthRust)]
7pub struct WrapperBcrypt {
8    pub password: String,
9    pub cost: u32,
10    pub hash: Option<String>,
11}
12
13pub trait BcryptTrait {
14    fn encode(&self) -> BcryptHashResult;
15    fn verify(&self) -> BcryptVerifyResult;
16}
17
18impl BcryptTrait for WrapperBcrypt {
19    fn encode(&self) -> BcryptHashResult {
20        let password = &self.password;
21        let cost = self.cost;
22        bcrypt::hash(password, cost)
23    }
24    fn verify(&self) -> BcryptVerifyResult {
25        let password = &self.password;
26        let hash = self.hash.as_ref().expect("hash must be provided");
27        bcrypt::verify(password, hash)
28    }
29}