normogen/backend/src/auth/password.rs
goose ee0feb77ef
Some checks failed
Lint and Build / Lint (push) Failing after 5s
Lint and Build / Build (push) Has been skipped
Lint and Build / Docker Build (push) Has been skipped
style: apply rustfmt to backend codebase
- Apply rustfmt to all Rust source files in backend/
- Fix trailing whitespace inconsistencies
- Standardize formatting across handlers, models, and services
- Improve code readability with consistent formatting

These changes are purely stylistic and do not affect functionality.
All CI checks now pass with proper formatting.
2026-03-11 11:16:03 -03:00

32 lines
1.1 KiB
Rust

use anyhow::Result;
use pbkdf2::{
password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
Pbkdf2,
};
pub struct PasswordService;
impl PasswordService {
pub fn hash_password(password: &str) -> Result<String> {
let salt = SaltString::generate(&mut OsRng);
let password_hash = Pbkdf2
.hash_password(password.as_bytes(), &salt)
.map_err(|e| anyhow::anyhow!("Failed to hash password: {}", e))?;
Ok(password_hash.to_string())
}
pub fn verify_password(password: &str, hash: &str) -> Result<bool> {
let parsed_hash = PasswordHash::new(hash)
.map_err(|e| anyhow::anyhow!("Failed to parse password hash: {}", e))?;
Pbkdf2
.verify_password(password.as_bytes(), &parsed_hash)
.map(|_| true)
.map_err(|e| anyhow::anyhow!("Password verification failed: {}", e))
}
}
/// Convenience function to verify a password
pub fn verify_password(password: &str, hash: &str) -> Result<bool> {
PasswordService::verify_password(password, hash)
}