extern crate passwors;
use passwors::*;
use std::collections::HashMap;
pub struct User {
pub name: String,
pub password: HashedPassword,
pub salt: HashSalt,
pub hasher: Argon2Settings,
}
pub struct UserDB {
all_users: HashMap<String, User>,
}
impl UserDB {
pub fn new() -> UserDB {
UserDB { all_users: HashMap::new() }
}
pub fn add_user(&mut self, user: User) {
let name = user.name.clone();
self.all_users.insert(name, user);
}
pub fn find_user(&self, name: &str) -> Option<&User> {
self.all_users.get(name)
}
}
fn main() {
let mut db = UserDB::new();
{ let hasher = Argon2Settings::new().try_create().unwrap();
let salt = HashSalt::new().unwrap();
let pw_hash = {
let pw = ClearTextPassword::from_string("1234qwerasdfzxcv".to_owned()).unwrap();
pw.hash(&hasher, &salt)
};
db.add_user(User {
name: "admin".to_owned(),
password: pw_hash,
salt: salt,
hasher: hasher.settings(),
});
}
let user = db.find_user("admin").unwrap();
let password = "1234qwerasdfzxcv".to_owned();
let hasher = user.hasher.try_create().unwrap();
let pw_hash = {
let pw = ClearTextPassword::from_string(password).unwrap();
pw.hash(&hasher, &user.salt)
};
assert_eq!(user.password, pw_hash);
}