36 lines
1.1 KiB
Rust
36 lines
1.1 KiB
Rust
use argon2::{
|
|
password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
|
|
Argon2,
|
|
};
|
|
use sha2::{Digest, Sha256};
|
|
|
|
// Hashing password with Argon2id
|
|
pub fn hash_password(password: &str) -> Result<String, argon2::password_hash::Error> {
|
|
let salt = SaltString::generate(&mut OsRng);
|
|
let argon2 = Argon2::default();
|
|
let password_hash = argon2
|
|
.hash_password(password.as_bytes(), &salt)?
|
|
.to_string();
|
|
Ok(password_hash)
|
|
}
|
|
|
|
// Verifying Argon2id password hash
|
|
pub fn verify_password(password: &str, hash: &str) -> bool {
|
|
if let Ok(parsed_hash) = PasswordHash::new(hash) {
|
|
Argon2::default()
|
|
.verify_password(password.as_bytes(), &parsed_hash)
|
|
.is_ok()
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
|
|
// Verifying SHA-256 bootstrap hash
|
|
pub fn verify_sha256(password: &str, expected_hex: &str) -> bool {
|
|
let mut hasher = Sha256::new();
|
|
hasher.update(password.as_bytes());
|
|
let result = hasher.finalize();
|
|
let hex_result = hex::encode(result);
|
|
hex_result.eq_ignore_ascii_case(expected_hex)
|
|
}
|