use bcrypt::{hash, verify, DEFAULT_COST};
use doido_core::Result;
pub fn hash_password(password: &str) -> Result<String> {
hash_password_with_cost(password, DEFAULT_COST)
}
pub fn hash_password_with_cost(password: &str, cost: u32) -> Result<String> {
hash(password, cost).map_err(|e| doido_core::anyhow::anyhow!("bcrypt hash failed: {e}"))
}
pub fn verify_password(password: &str, digest: &str) -> bool {
verify(password, digest).unwrap_or(false)
}
pub fn generate_token() -> String {
format!(
"{}{}",
uuid::Uuid::new_v4().simple(),
uuid::Uuid::new_v4().simple()
)
}
pub trait HasSecurePassword {
fn password_digest(&self) -> &str;
fn authenticate(&self, password: &str) -> bool {
verify_password(password, self.password_digest())
}
}