use indexmap::IndexMap;
use php_std::Array;
use php_std::types::KeyRef;
use super::Proof;
use crate::hash::random_int;
use crate::hashes::{Argon2, Bcrypt, Md5, PHPass, Scrypt, ScryptModified, Sha};
use crate::{Error, Hash};
pub const ARGON2: &str = "argon2";
pub const BCRYPT: &str = "bcrypt";
pub const SCRYPT: &str = "scrypt";
pub const SCRYPT_MODIFIED: &str = "scryptMod";
pub const SHA: &str = "sha";
pub const MD5: &str = "md5";
pub const PHPASS: &str = "phpass";
const CHARSET: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+-=[]{}|;:,.<>?";
#[derive(Debug, Clone)]
enum Current {
Registered(String),
Own(Box<dyn Hash>),
}
#[derive(Debug, Clone)]
pub struct Password {
hashes: IndexMap<String, Box<dyn Hash>>,
current: Current,
length: i64,
charset: Vec<u8>,
}
impl Default for Password {
fn default() -> Self {
Self::new()
}
}
impl Password {
pub fn new() -> Self {
Self::with_hashes(IndexMap::new())
}
pub fn with_hashes(hashes: IndexMap<String, Box<dyn Hash>>) -> Self {
let hashes = if hashes.is_empty() {
let mut defaults: IndexMap<String, Box<dyn Hash>> = IndexMap::new();
defaults.insert(ARGON2.into(), Box::new(Argon2::new()));
defaults.insert(BCRYPT.into(), Box::new(Bcrypt::new()));
defaults.insert(SCRYPT.into(), Box::new(Scrypt::new()));
defaults.insert(SCRYPT_MODIFIED.into(), Box::new(ScryptModified::new()));
defaults.insert(SHA.into(), Box::new(Sha::new()));
defaults.insert(MD5.into(), Box::new(Md5::new()));
defaults.insert(PHPASS.into(), Box::new(PHPass::new()));
defaults
} else {
hashes
};
let current = if hashes.contains_key(ARGON2) {
ARGON2.to_owned()
} else {
hashes.keys().next().cloned().unwrap_or_default()
};
Self { hashes, current: Current::Registered(current), length: 16, charset: CHARSET.to_vec() }
}
pub fn add_hash(&mut self, name: &str, hash: Box<dyn Hash>) -> &mut Self {
self.hashes.insert(name.to_owned(), hash);
self
}
pub fn remove_hash(&mut self, name: &str) -> Result<&mut Self, Error> {
if !self.hashes.contains_key(name) {
return Err(Error::Exception(format!("Hash '{name}' not found")));
}
if matches!(&self.current, Current::Registered(n) if n == name) {
return Err(Error::Exception("Cannot remove current hash".into()));
}
self.hashes.shift_remove(name);
Ok(self)
}
pub fn hash_by_name(&self, name: &str) -> Result<&dyn Hash, Error> {
self.hashes.get(name).map(|h| h.as_ref()).ok_or_else(|| Error::Exception(format!("Hash '{name}' not found")))
}
pub fn hash_by_name_mut(&mut self, name: &str) -> Result<&mut dyn Hash, Error> {
match self.hashes.get_mut(name) {
Some(h) => Ok(h.as_mut()),
None => Err(Error::Exception(format!("Hash '{name}' not found"))),
}
}
pub fn use_hash(&mut self, name: &str) -> Result<&mut Self, Error> {
self.hash_by_name(name)?;
self.current = Current::Registered(name.to_owned());
Ok(self)
}
pub fn current_name(&self) -> Option<&str> {
match &self.current {
Current::Registered(name) if self.hashes.contains_key(name) => Some(name),
_ => None,
}
}
pub fn set_length(&mut self, length: i64) -> Result<&mut Self, Error> {
if length < 8 {
return Err(Error::Exception("Password length must be at least 8 characters".into()));
}
self.length = length;
Ok(self)
}
pub fn set_charset(&mut self, charset: &[u8]) -> Result<&mut Self, Error> {
if charset.len() < 10 {
return Err(Error::Exception("Password charset must contain at least 10 characters".into()));
}
self.charset = charset.to_vec();
Ok(self)
}
pub fn create_hash(kind: &str, options: &Array) -> Result<Box<dyn Hash>, Error> {
let mut hash: Box<dyn Hash> = match kind {
ARGON2 => Box::new(Argon2::new()),
BCRYPT => Box::new(Bcrypt::new()),
SCRYPT => Box::new(Scrypt::new()),
SCRYPT_MODIFIED => Box::new(ScryptModified::new()),
SHA => Box::new(Sha::new()),
MD5 => Box::new(Md5::new()),
PHPASS => Box::new(PHPass::new()),
_ => return Err(Error::Exception(format!("Unsupported hash type: {kind}"))),
};
set_options(hash.as_mut(), options)?;
Ok(hash)
}
}
pub fn set_options(hash: &mut dyn Hash, options: &Array) -> Result<(), Error> {
for (key, value) in options.iter() {
match key {
KeyRef::Str(k) => hash.options_mut().set(&String::from_utf8_lossy(k), value.clone()),
KeyRef::Int(_) => {
return Err(Error::Type(
"Utopia\\Auth\\Hash::setOption(): Argument #1 ($key) must be of type string, int given".into(),
));
}
}
}
Ok(())
}
impl Proof for Password {
fn generate(&self) -> Result<Vec<u8>, Error> {
if self.charset.is_empty() {
return Err(Error::Exception("Password charset is empty".into()));
}
let max = self.charset.len() as u64 - 1;
Ok((0..self.length).map(|_| self.charset[random_int(max) as usize]).collect())
}
fn hasher(&self) -> &dyn Hash {
match &self.current {
Current::Own(h) => h.as_ref(),
Current::Registered(name) => self.hashes.get(name).map(|h| h.as_ref()).unwrap_or(&*FALLBACK),
}
}
fn hasher_mut(&mut self) -> &mut dyn Hash {
match &mut self.current {
Current::Own(h) => h.as_mut(),
Current::Registered(name) => {
if !self.hashes.contains_key(name.as_str()) {
self.hashes.insert(name.clone(), Box::new(Argon2::new()));
}
self.hashes.get_mut(name.as_str()).map(|h| h.as_mut()).expect("inserted above")
}
}
}
fn set_hash(&mut self, hash: Box<dyn Hash>) {
self.current = Current::Own(hash);
}
}
static FALLBACK: std::sync::LazyLock<Argon2> = std::sync::LazyLock::new(Argon2::new);