use md5;
use rand::Rng;
pub fn rand_s(length: usize) -> String {
const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyz\
0123456789)(*&^%$#@!~";
let mut rng = rand::rng();
let rand_string: String = (0..length)
.map(|_| {
let idx = rng.random_range(0..CHARSET.len());
CHARSET[idx] as char
})
.collect();
rand_string
}
fn bytes_to_hex(bytes: &[u8]) -> String {
use std::fmt::Write;
let mut result = String::new();
for byte in bytes {
write!(result, "{:02x}", byte).expect("Writing to String should not fail");
}
result
}
pub fn encrypt_password_salt(password: &str, salt: &str) -> String {
if salt.len() == 0 {
return encrypt_password(password);
}
let combined = format!("{}$X7h{}", password, salt);
let digest = md5::compute(combined);
bytes_to_hex(&digest[..])
}
pub fn encrypt_password(password: &str) -> String {
let digest = md5::compute(password);
bytes_to_hex(&digest[..])
}