2022-05-14 12:35:08 +00:00
|
|
|
use crate::ctx::Context;
|
2022-01-13 17:37:46 +00:00
|
|
|
use crate::err::Error;
|
|
|
|
use crate::sql::value::Value;
|
|
|
|
use md5::Digest;
|
|
|
|
use md5::Md5;
|
|
|
|
use sha1::Sha1;
|
|
|
|
use sha2::Sha256;
|
|
|
|
use sha2::Sha512;
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn md5(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
let mut hasher = Md5::new();
|
2022-05-05 04:30:32 +00:00
|
|
|
hasher.update(args.remove(0).as_string().as_str());
|
2022-01-13 17:37:46 +00:00
|
|
|
let val = hasher.finalize();
|
|
|
|
let val = format!("{:x}", val);
|
|
|
|
Ok(val.into())
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn sha1(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
let mut hasher = Sha1::new();
|
2022-05-05 04:30:32 +00:00
|
|
|
hasher.update(args.remove(0).as_string().as_str());
|
2022-01-13 17:37:46 +00:00
|
|
|
let val = hasher.finalize();
|
|
|
|
let val = format!("{:x}", val);
|
|
|
|
Ok(val.into())
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn sha256(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
let mut hasher = Sha256::new();
|
2022-05-05 04:30:32 +00:00
|
|
|
hasher.update(args.remove(0).as_string().as_str());
|
2022-01-13 17:37:46 +00:00
|
|
|
let val = hasher.finalize();
|
|
|
|
let val = format!("{:x}", val);
|
|
|
|
Ok(val.into())
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn sha512(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
let mut hasher = Sha512::new();
|
2022-05-05 04:30:32 +00:00
|
|
|
hasher.update(args.remove(0).as_string().as_str());
|
2022-01-13 17:37:46 +00:00
|
|
|
let val = hasher.finalize();
|
|
|
|
let val = format!("{:x}", val);
|
|
|
|
Ok(val.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod argon2 {
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
use crate::ctx::Context;
|
2022-01-13 17:37:46 +00:00
|
|
|
use crate::err::Error;
|
|
|
|
use crate::sql::value::Value;
|
|
|
|
use argon2::{
|
|
|
|
password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
|
|
|
|
Argon2,
|
|
|
|
};
|
|
|
|
use rand::rngs::OsRng;
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn cmp(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
let algo = Argon2::default();
|
2022-05-05 04:30:32 +00:00
|
|
|
let hash = args.remove(0).as_string();
|
|
|
|
let pass = args.remove(0).as_string();
|
2022-01-13 17:37:46 +00:00
|
|
|
let test = PasswordHash::new(&hash).unwrap();
|
|
|
|
Ok(algo.verify_password(pass.as_ref(), &test).is_ok().into())
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn gen(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-01-13 17:37:46 +00:00
|
|
|
let algo = Argon2::default();
|
2022-05-05 04:30:32 +00:00
|
|
|
let pass = args.remove(0).as_string();
|
2022-01-13 17:37:46 +00:00
|
|
|
let salt = SaltString::generate(&mut OsRng);
|
|
|
|
let hash = algo.hash_password(pass.as_ref(), salt.as_ref()).unwrap().to_string();
|
|
|
|
Ok(hash.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod pbkdf2 {
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
use crate::ctx::Context;
|
2022-01-13 17:37:46 +00:00
|
|
|
use crate::err::Error;
|
|
|
|
use crate::sql::value::Value;
|
|
|
|
use pbkdf2::{
|
|
|
|
password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
|
|
|
|
Pbkdf2,
|
|
|
|
};
|
|
|
|
use rand::rngs::OsRng;
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn cmp(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
let hash = args.remove(0).as_string();
|
|
|
|
let pass = args.remove(0).as_string();
|
2022-01-13 17:37:46 +00:00
|
|
|
let test = PasswordHash::new(&hash).unwrap();
|
|
|
|
Ok(Pbkdf2.verify_password(pass.as_ref(), &test).is_ok().into())
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn gen(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
let pass = args.remove(0).as_string();
|
2022-01-13 17:37:46 +00:00
|
|
|
let salt = SaltString::generate(&mut OsRng);
|
|
|
|
let hash = Pbkdf2.hash_password(pass.as_ref(), salt.as_ref()).unwrap().to_string();
|
|
|
|
Ok(hash.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod scrypt {
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
use crate::ctx::Context;
|
2022-01-13 17:37:46 +00:00
|
|
|
use crate::err::Error;
|
|
|
|
use crate::sql::value::Value;
|
|
|
|
use rand::rngs::OsRng;
|
|
|
|
use scrypt::{
|
|
|
|
password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
|
|
|
|
Scrypt,
|
|
|
|
};
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn cmp(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
let hash = args.remove(0).as_string();
|
|
|
|
let pass = args.remove(0).as_string();
|
2022-01-13 17:37:46 +00:00
|
|
|
let test = PasswordHash::new(&hash).unwrap();
|
|
|
|
Ok(Scrypt.verify_password(pass.as_ref(), &test).is_ok().into())
|
|
|
|
}
|
|
|
|
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn gen(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
let pass = args.remove(0).as_string();
|
2022-01-13 17:37:46 +00:00
|
|
|
let salt = SaltString::generate(&mut OsRng);
|
|
|
|
let hash = Scrypt.hash_password(pass.as_ref(), salt.as_ref()).unwrap().to_string();
|
|
|
|
Ok(hash.into())
|
|
|
|
}
|
|
|
|
}
|