1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use argon2::{
password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
Argon2,
};
pub fn generate_hash(password: &str) -> Vec<u8> {
let salt = SaltString::generate(&mut OsRng);
let argon2 = Argon2::default();
argon2
.hash_password(password.as_bytes(), &salt)
.expect("Cannot generate hash from string")
.to_string()
.as_bytes()
.to_vec()
}
pub fn check_hash(password: &str, hash: &str) -> bool {
let parsed_hash = PasswordHash::new(hash).expect("Cannot parse password hash");
Argon2::default()
.verify_password(password.as_bytes(), &parsed_hash)
.is_ok()
}