airlab_lib/pwd/
mod.rs

1#![allow(clippy::module_name_repetitions)]
2mod error;
3mod hmac_hasher;
4
5pub use self::error::{Error, Result};
6
7use crate::auth_config;
8use crate::pwd::hmac_hasher::hmac_sha512_hash;
9use uuid::Uuid;
10
11pub struct ContentToHash {
12    pub content: String,
13    pub salt: Uuid,
14}
15
16pub fn hash_pwd(to_hash: &ContentToHash) -> Result<String> {
17    let key = &auth_config().PWD_KEY;
18
19    let hashed = hmac_sha512_hash(key, to_hash)?;
20
21    Ok(format!("#01#{hashed}"))
22}
23
24pub fn validate_pwd(enc_content: &ContentToHash, pwd_ref: &str) -> Result<()> {
25    let pwd = hash_pwd(enc_content)?;
26
27    if pwd == pwd_ref {
28        Ok(())
29    } else {
30        Err(Error::NotMatching)
31    }
32}