feat: update makepassword-rs

This commit is contained in:
2024-12-29 13:05:27 +08:00
parent 6d11b30ec9
commit 795265ef97
4 changed files with 294 additions and 5 deletions

64
makepassword-rs/src/main.rs Normal file → Executable file
View File

@@ -3,21 +3,81 @@
//! ```cargo
//! [dependencies]
//! rand = "0.8.5"
//! clap = { version = "4.1.8", features = ["derive"] }
//! ```
use clap::{arg, Parser};
use rand::RngCore;
use std::process::exit;
const ENGLISH_LOWER: &'static str = "abcdefghijklmnopqrstuvwxyz";
const ENGLISH_UPPER: &'static str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const NUMBER: &'static str = "0123456789";
const SPECIAL: &'static str = ".,/<>;:[]{}-=_+!@#$%^&*()~";
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None, bin_name = "makepassword.rs")]
struct Args {
/// Password length
#[arg(short, long)]
length: Option<u32>,
/// Chars (e.g. a - lower case alphabet, A - UPPER CASE ALPHABET, n|N|# - number, * - special)
#[arg(short, long)]
chars: Option<String>,
}
fn main() {
let str = ENGLISH_LOWER.to_string() + ENGLISH_UPPER + NUMBER;
let args: Args = Args::parse();
let mut a = false;
let mut A = false;
let mut n = false;
let mut s = false;
match args.chars {
None => {
a = true;
A = true;
n = true;
}
Some(chars) => {
for c in chars.chars() {
match c {
'a' => a = true,
'A' => A = true,
'n' | 'N' | '#' => n = true,
'*' => s = true,
_ => {}
}
}
}
}
if !a && !A && !n && !s {
eprintln!("No chars selected.");
exit(1);
}
let mut str = "".to_string();
if a {
str += ENGLISH_LOWER;
}
if A {
str += ENGLISH_UPPER;
}
if n {
str += NUMBER;
}
if s {
str += SPECIAL;
}
let chars = str.chars().collect::<Vec<_>>();
let mut rng = rand::thread_rng();
let mut pass = String::new();
for _ in 0..18 {
let len = args.length.unwrap_or(18);
for _ in 0..len {
pass.push(chars[rng.next_u32() as usize % chars.len()]);
}