next_web_dev/security/
password_encoder.rs1use bcrypt::{hash, verify, BcryptError, DEFAULT_COST};
2
3
4#[derive(Clone)]
5pub struct PasswordEncoder {
6 salt: String,
7}
8
9impl PasswordEncoder {
10 pub fn encode(&self, password: &str) -> Result<String, BcryptError> {
11 let pwd = String::from(password) + self.salt.as_str();
12 hash(pwd, DEFAULT_COST)
13 }
14
15 pub fn verify(&self, password: &str, hashed_password: &str) -> bool {
16 let pwd = String::from(password) + self.salt.as_str();
17 verify(pwd, hashed_password).unwrap_or(false)
18 }
19}
20
21impl Default for PasswordEncoder {
22 fn default() -> Self {
23 Self {
24 salt: String::from("next-web-dev-salt"),
25 }
26 }
27}