use crate::variant::Variant;
use crate::version::Version;
#[derive(Clone, Debug, PartialEq)]
pub struct Config<'a> {
pub ad: &'a [u8],
pub hash_length: u32,
pub lanes: u32,
pub mem_cost: u32,
pub secret: &'a [u8],
pub time_cost: u32,
pub variant: Variant,
pub version: Version,
}
impl<'a> Config<'a> {
pub fn original() -> Config<'a> {
Config {
ad: &[],
hash_length: 32,
lanes: 1,
mem_cost: 4096,
secret: &[],
time_cost: 3,
variant: Variant::Argon2i,
version: Version::Version13,
}
}
pub fn owasp1() -> Config<'a> {
Config {
ad: &[],
hash_length: 32,
lanes: 1,
mem_cost: 47104,
secret: &[],
time_cost: 1,
variant: Variant::Argon2id,
version: Version::Version13,
}
}
pub fn owasp2() -> Config<'a> {
Config {
ad: &[],
hash_length: 32,
lanes: 1,
mem_cost: 19456,
secret: &[],
time_cost: 2,
variant: Variant::Argon2id,
version: Version::Version13,
}
}
pub fn owasp3() -> Config<'a> {
Config {
ad: &[],
hash_length: 32,
lanes: 1,
mem_cost: 12288,
secret: &[],
time_cost: 3,
variant: Variant::Argon2id,
version: Version::Version13,
}
}
pub fn owasp4() -> Config<'a> {
Config {
ad: &[],
hash_length: 32,
lanes: 1,
mem_cost: 9216,
secret: &[],
time_cost: 4,
variant: Variant::Argon2id,
version: Version::Version13,
}
}
pub fn owasp5() -> Config<'a> {
Config {
ad: &[],
hash_length: 32,
lanes: 1,
mem_cost: 7168,
secret: &[],
time_cost: 5,
variant: Variant::Argon2id,
version: Version::Version13,
}
}
pub fn rfc9106() -> Config<'a> {
Config {
ad: &[],
hash_length: 32,
lanes: 1,
mem_cost: 2097152,
secret: &[],
time_cost: 1,
variant: Variant::Argon2id,
version: Version::Version13,
}
}
pub fn rfc9106_low_mem() -> Config<'a> {
Config {
ad: &[],
hash_length: 32,
lanes: 1,
mem_cost: 65536,
secret: &[],
time_cost: 3,
variant: Variant::Argon2id,
version: Version::Version13,
}
}
}
impl<'a> Default for Config<'a> {
fn default() -> Config<'a> {
Config::owasp2()
}
}
#[cfg(test)]
mod tests {
use crate::config::Config;
use crate::variant::Variant;
use crate::version::Version;
#[test]
fn default_returns_correct_instance() {
let config = Config::default();
assert_eq!(config.ad, &[]);
assert_eq!(config.hash_length, 32);
assert_eq!(config.lanes, 1);
assert_eq!(config.mem_cost, 19 * 1024);
assert_eq!(config.secret, &[]);
assert_eq!(config.time_cost, 2);
assert_eq!(config.variant, Variant::Argon2id);
assert_eq!(config.version, Version::Version13);
}
#[test]
fn original_returns_correct_instance() {
let config = Config::original();
assert_eq!(config.ad, &[]);
assert_eq!(config.hash_length, 32);
assert_eq!(config.lanes, 1);
assert_eq!(config.mem_cost, 4096);
assert_eq!(config.secret, &[]);
assert_eq!(config.time_cost, 3);
assert_eq!(config.variant, Variant::Argon2i);
assert_eq!(config.version, Version::Version13);
}
#[test]
fn owasp1_returns_correct_instance() {
let config = Config::owasp1();
assert_eq!(config.ad, &[]);
assert_eq!(config.hash_length, 32);
assert_eq!(config.lanes, 1);
assert_eq!(config.mem_cost, 46 * 1024);
assert_eq!(config.secret, &[]);
assert_eq!(config.time_cost, 1);
assert_eq!(config.variant, Variant::Argon2id);
assert_eq!(config.version, Version::Version13);
}
#[test]
fn owasp2_returns_correct_instance() {
let config = Config::owasp2();
assert_eq!(config.ad, &[]);
assert_eq!(config.hash_length, 32);
assert_eq!(config.lanes, 1);
assert_eq!(config.mem_cost, 19 * 1024);
assert_eq!(config.secret, &[]);
assert_eq!(config.time_cost, 2);
assert_eq!(config.variant, Variant::Argon2id);
assert_eq!(config.version, Version::Version13);
}
#[test]
fn owasp3_returns_correct_instance() {
let config = Config::owasp3();
assert_eq!(config.ad, &[]);
assert_eq!(config.hash_length, 32);
assert_eq!(config.lanes, 1);
assert_eq!(config.mem_cost, 12 * 1024);
assert_eq!(config.secret, &[]);
assert_eq!(config.time_cost, 3);
assert_eq!(config.variant, Variant::Argon2id);
assert_eq!(config.version, Version::Version13);
}
#[test]
fn owasp4_returns_correct_instance() {
let config = Config::owasp4();
assert_eq!(config.ad, &[]);
assert_eq!(config.hash_length, 32);
assert_eq!(config.lanes, 1);
assert_eq!(config.mem_cost, 9 * 1024);
assert_eq!(config.secret, &[]);
assert_eq!(config.time_cost, 4);
assert_eq!(config.variant, Variant::Argon2id);
assert_eq!(config.version, Version::Version13);
}
#[test]
fn owasp5_returns_correct_instance() {
let config = Config::owasp5();
assert_eq!(config.ad, &[]);
assert_eq!(config.hash_length, 32);
assert_eq!(config.lanes, 1);
assert_eq!(config.mem_cost, 7 * 1024);
assert_eq!(config.secret, &[]);
assert_eq!(config.time_cost, 5);
assert_eq!(config.variant, Variant::Argon2id);
assert_eq!(config.version, Version::Version13);
}
#[test]
fn rfc9106_returns_correct_instance() {
let config = Config::rfc9106();
assert_eq!(config.ad, &[]);
assert_eq!(config.hash_length, 32);
assert_eq!(config.lanes, 1);
assert_eq!(config.mem_cost, 2 * 1024 * 1024);
assert_eq!(config.secret, &[]);
assert_eq!(config.time_cost, 1);
assert_eq!(config.variant, Variant::Argon2id);
assert_eq!(config.version, Version::Version13);
}
#[test]
fn rfc9106_low_mem_returns_correct_instance() {
let config = Config::rfc9106_low_mem();
assert_eq!(config.ad, &[]);
assert_eq!(config.hash_length, 32);
assert_eq!(config.lanes, 1);
assert_eq!(config.mem_cost, 64 * 1024);
assert_eq!(config.secret, &[]);
assert_eq!(config.time_cost, 3);
assert_eq!(config.variant, Variant::Argon2id);
assert_eq!(config.version, Version::Version13);
}
}