quasor 6.1.6

A high-security AEAD based on a Duplex Sponge construction with SHAKE256, Argon2id, and BLAKE3.
Documentation
// crates/quasor/src/builder.rs

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,
        }
    }

    /// Sets the memory cost (m_cost) for Argon2.
    pub fn memory_cost(mut self, m_cost: u32) -> Self {
        self.m_cost = m_cost;
        self
    }

    /// Sets the time cost (t_cost) for Argon2.
    pub fn time_cost(mut self, t_cost: u32) -> Self {
        self.t_cost = t_cost;
        self
    }

    /// Sets the parallelism cost (p_cost) for Argon2.
    pub fn parallelism_cost(mut self, p_cost: u32) -> Self {
        self.p_cost = p_cost;
        self
    }

    /// Builds the Quasor instance with the configured parameters.
    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))
    }
}