Compare commits

..

No commits in common. "04f1e485646ff69b733a940dd4019819c05670f4" and "052723bfdb064c0a8b9f722c1637c3b905951ae5" have entirely different histories.

View File

@ -1,8 +1,5 @@
use std::collections::HashSet;
use std::env; use std::env;
use rand::RngCore;
fn main() { fn main() {
// Take in two command line arguments, one i32 and one optional String // Take in two command line arguments, one i32 and one optional String
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
@ -21,17 +18,15 @@ fn main() {
// Disallowed_chars is an optional argument // Disallowed_chars is an optional argument
let disallowed_chars: Option<&String> = args.get(2); let disallowed_chars: Option<&String> = args.get(2);
let allowed_chars: String = String::from("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-=`~!@#$%^&*()_+[]\\{}|;':,./<>?"); let mut allowed_chars: String = String::from("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-=`~!@#$%^&*()_+[]\\{}|;':,./<>?");
let mut allowed_chars_set: HashSet<char> = allowed_chars.chars().collect();
// Remove disallowed characters from allowed_characters if disallowed_chars is specified // Remove disallowed characters from allowed_characters if disallowed_chars is specified
if let Some(disallowed_chars) = disallowed_chars { if let Some(disallowed_chars) = disallowed_chars {
for disallowed_char in disallowed_chars.chars() { for disallowed_char in disallowed_chars.chars() {
allowed_chars_set.remove(&disallowed_char); allowed_chars = allowed_chars.replace(disallowed_char, "");
} }
if allowed_chars_set.len() == 0 { // Make sure there are still characters left to use if allowed_chars.len() == 0 { // Make sure there are still characters left to use
println!("Error: You have disallowed all characters"); println!("Error: You have disallowed all characters");
println!("The allowed character set includes: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-=`~!@#$%^&*()_+[]\\{{}}|;':,./<>?"); println!("The allowed character set includes: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-=`~!@#$%^&*()_+[]\\{{}}|;':,./<>?");
return; return;
@ -39,14 +34,13 @@ fn main() {
} }
if let Some(new_password_length) = new_password_length { // Unwrap Option in i32 if let Some(new_password_length) = new_password_length { // Unwrap Option in i32
let mut rng = rand::thread_rng();
// Generate a random string of length n // Generate a random string of length n
let mut new_password = String::new(); let mut new_password = String::new();
for _ in 0..new_password_length { for _ in 0..new_password_length {
new_password.push( new_password.push(
*allowed_chars_set.iter() allowed_chars.chars()
.nth(rng.next_u32() as usize % allowed_chars_set.len()) .nth(rand::random::<usize>() % allowed_chars.len())
.expect("Error: Invalid index") .unwrap()
); );
} }