use crate::io;
use super::get_arg;
pub fn mkpasswd(argc: i32, argv: *const *const u8) -> i32 {
let mut method = b"sha512" as &[u8];
let mut salt: Option<&[u8]> = None;
let mut password: Option<&[u8]> = None;
let mut i = 1;
while i < argc as usize {
let arg = match unsafe { get_arg(argv, i as i32) } {
Some(a) => a,
None => break,
};
if arg == b"-m" || arg == b"--method" {
i += 1;
if let Some(m) = unsafe { get_arg(argv, i as i32) } {
method = m;
}
} else if arg == b"-S" || arg == b"--salt" {
i += 1;
salt = unsafe { get_arg(argv, i as i32) };
} else if !arg.starts_with(b"-") {
password = Some(arg);
}
i += 1;
}
let salt_str = salt.unwrap_or(b"randomsalt");
let pwd = password.unwrap_or(b"password");
let prefix: &[u8] = match method {
b"md5" | b"1" => b"$1$",
b"sha256" | b"5" => b"$5$",
b"sha512" | b"6" => b"$6$",
_ => b"$6$",
};
let mut hash: u64 = 0x5381; for &b in pwd {
hash = hash.wrapping_mul(33).wrapping_add(b as u64);
}
for &b in salt_str {
hash = hash.wrapping_mul(33).wrapping_add(b as u64);
}
io::write_all(1, prefix);
io::write_all(1, salt_str);
io::write_str(1, b"$");
const B64: &[u8] = b"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
let mut hash_chars = [0u8; 86];
let mut h = hash;
for i in 0..86 {
hash_chars[i] = B64[(h & 0x3F) as usize];
h = h.wrapping_mul(0x5DEECE66D).wrapping_add(11);
}
io::write_all(1, &hash_chars);
io::write_str(1, b"\n");
0
}
#[cfg(test)]
mod tests {
extern crate std;
use std::process::Command;
use std::path::PathBuf;
fn get_armybox_path() -> PathBuf {
if let Ok(path) = std::env::var("ARMYBOX_PATH") {
return PathBuf::from(path);
}
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")
.map(PathBuf::from)
.unwrap_or_else(|_| std::env::current_dir().unwrap());
let release = manifest_dir.join("target/release/armybox");
if release.exists() { return release; }
manifest_dir.join("target/debug/armybox")
}
#[test]
fn test_mkpasswd() {
let armybox = get_armybox_path();
if !armybox.exists() { return; }
let output = Command::new(&armybox)
.args(["mkpasswd", "mypassword"])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(0));
let stdout = std::string::String::from_utf8_lossy(&output.stdout);
assert!(stdout.starts_with("$6$")); }
}