use argon2::Params;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum MemoryTier {
Nano,
#[default]
Standard,
Sovereign,
}
impl MemoryTier {
pub fn argon2_params(self, output_len: usize) -> Params {
let (m_cost, t_cost, p_cost) = match self {
MemoryTier::Nano => (8 * 1024, 2, 1), MemoryTier::Standard => (64 * 1024, 3, 2), MemoryTier::Sovereign => (256 * 1024, 5, 4), };
Params::new(m_cost, t_cost, p_cost, Some(output_len)).expect("valid Argon2 params")
}
pub fn label(self) -> &'static str {
match self {
MemoryTier::Nano => "nano",
MemoryTier::Standard => "standard",
MemoryTier::Sovereign => "sovereign",
}
}
pub fn buffer_size(&self) -> usize {
match self {
MemoryTier::Nano => 1024,
MemoryTier::Standard => 4096,
MemoryTier::Sovereign => 4096,
}
}
pub fn should_lock(&self) -> bool {
matches!(self, MemoryTier::Sovereign)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn nano_params_valid() {
let p = MemoryTier::Nano.argon2_params(32);
assert_eq!(p.m_cost(), 8 * 1024);
assert_eq!(p.t_cost(), 2);
assert_eq!(p.p_cost(), 1);
}
#[test]
fn standard_params_valid() {
let p = MemoryTier::Standard.argon2_params(32);
assert_eq!(p.m_cost(), 64 * 1024);
}
#[test]
fn sovereign_params_valid() {
let p = MemoryTier::Sovereign.argon2_params(32);
assert_eq!(p.m_cost(), 256 * 1024);
assert_eq!(p.t_cost(), 5);
assert_eq!(p.p_cost(), 4);
}
}