use anyhow::{Context, Result};
pub struct Hash;
impl Hash {
pub fn make(password: &str) -> Result<String> {
bcrypt::hash(password, 12).context("Failed to hash password")
}
pub fn check(password: &str, hash: &str) -> Result<bool> {
bcrypt::verify(password, hash).context("Failed to verify password")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hash_and_check() {
let hashed = Hash::make("secret123").unwrap();
assert!(Hash::check("secret123", &hashed).unwrap());
assert!(!Hash::check("wrong", &hashed).unwrap());
}
#[test]
fn test_different_passwords_different_hashes() {
let a = Hash::make("a").unwrap();
let b = Hash::make("b").unwrap();
assert_ne!(a, b);
}
#[test]
fn test_verify_invalid_hash() {
assert!(Hash::check("anything", "not-a-valid-hash").is_err());
}
}