use crate::quasor::{Error, Quasor};
use argon2::{self, Algorithm, Argon2, Params, Version};
pub struct QuasorBuilder<'a> {
password: &'a [u8],
salt: &'a [u8],
m_cost: u32,
t_cost: u32,
p_cost: u32,
}
impl<'a> QuasorBuilder<'a> {
pub fn new(password: &'a [u8], salt: &'a [u8]) -> Self {
Self {
password,
salt,
m_cost: Params::DEFAULT_M_COST,
t_cost: Params::DEFAULT_T_COST,
p_cost: Params::DEFAULT_P_COST,
}
}
pub fn memory_cost(mut self, m_cost: u32) -> Self {
self.m_cost = m_cost;
self
}
pub fn time_cost(mut self, t_cost: u32) -> Self {
self.t_cost = t_cost;
self
}
pub fn parallelism_cost(mut self, p_cost: u32) -> Self {
self.p_cost = p_cost;
self
}
pub fn build(self) -> Result<Quasor, Error> {
let params = Params::new(self.m_cost, self.t_cost, self.p_cost, None)
.map_err(|e| Error::Argon2Error(e.to_string()))?;
let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
let mut master_key = [0u8; 32];
argon2
.hash_password_into(self.password, self.salt, &mut master_key)
.map_err(|e| Error::Argon2Error(e.to_string()))?;
Ok(Quasor::new_with_key(master_key))
}
}