refactor: Clean up Argon2 password hashing implementation and remove unused configuration constants
This commit is contained in:
@@ -4,18 +4,13 @@
|
|||||||
//! The hashing parameters are configured for a balance between security and performance.
|
//! The hashing parameters are configured for a balance between security and performance.
|
||||||
|
|
||||||
use argon2::{
|
use argon2::{
|
||||||
Argon2,
|
|
||||||
password_hash::{
|
password_hash::{
|
||||||
Error, PasswordHash, PasswordHasher, PasswordVerifier, SaltString,
|
rand_core::OsRng, Error, PasswordHash, PasswordHasher, PasswordVerifier,
|
||||||
rand_core::OsRng,
|
SaltString,
|
||||||
},
|
},
|
||||||
|
Argon2,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Configuration constants for Argon2 hashing
|
|
||||||
const MEMORY_COST: u32 = 1024; // 1MB
|
|
||||||
const TIME_COST: u32 = 1; // 1 iteration
|
|
||||||
const PARALLELISM: u32 = 1; // 1 thread
|
|
||||||
|
|
||||||
/// Hash a password using Argon2id algorithm.
|
/// Hash a password using Argon2id algorithm.
|
||||||
///
|
///
|
||||||
/// This function generates a cryptographically secure salt and hashes the password
|
/// This function generates a cryptographically secure salt and hashes the password
|
||||||
@@ -38,12 +33,7 @@ const PARALLELISM: u32 = 1; // 1 thread
|
|||||||
/// ```
|
/// ```
|
||||||
pub fn hash_password(password: &str) -> Result<String, Error> {
|
pub fn hash_password(password: &str) -> Result<String, Error> {
|
||||||
let salt = SaltString::generate(&mut OsRng);
|
let salt = SaltString::generate(&mut OsRng);
|
||||||
let argon2 = Argon2::new(
|
let argon2 = Argon2::default();
|
||||||
argon2::Algorithm::Argon2id,
|
|
||||||
argon2::Version::V0x13,
|
|
||||||
argon2::Params::new(MEMORY_COST, TIME_COST, PARALLELISM, None).unwrap(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let password_hash = argon2
|
let password_hash = argon2
|
||||||
.hash_password(password.as_bytes(), &salt)?
|
.hash_password(password.as_bytes(), &salt)?
|
||||||
.to_string();
|
.to_string();
|
||||||
@@ -75,7 +65,6 @@ pub fn hash_password(password: &str) -> Result<String, Error> {
|
|||||||
pub fn verify_password(password: &str, hash: &str) -> Result<bool, Error> {
|
pub fn verify_password(password: &str, hash: &str) -> Result<bool, Error> {
|
||||||
let parsed_hash = PasswordHash::new(hash)?;
|
let parsed_hash = PasswordHash::new(hash)?;
|
||||||
let argon2 = Argon2::default();
|
let argon2 = Argon2::default();
|
||||||
|
|
||||||
match argon2.verify_password(password.as_bytes(), &parsed_hash) {
|
match argon2.verify_password(password.as_bytes(), &parsed_hash) {
|
||||||
Ok(_) => Ok(true),
|
Ok(_) => Ok(true),
|
||||||
Err(_) => Ok(false),
|
Err(_) => Ok(false),
|
||||||
|
|||||||
Reference in New Issue
Block a user