use argon2::{
    password_hash::{rand_core::OsRng, PasswordHasher, SaltString},
    Argon2,
};

pub fn generate_salt_and_hash_plain_password(plain: &str) -> anyhow::Result<(String, String)> {
    let salt = SaltString::generate(&mut OsRng);

    // Argon2 with default params (Argon2id v19)
    let hash = Argon2::default()
        .hash_password(plain.as_bytes(), &salt)?
        .to_string();

    Ok((hash, salt.to_string()))
}

pub fn hash_plain_password_with_salt(plain: &str, salt_string: &str) -> Result<String, argon2::password_hash::Error> {
    let salt = SaltString::from_b64(salt_string)?;

    let hash = Argon2::default()
        .hash_password(plain.as_bytes(), &salt)?
        .to_string();

    Ok(hash)
}
