Skip to content

Instantly share code, notes, and snippets.

@lsdr
Created March 30, 2020 02:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsdr/138d40872f42ac34a6a9ac37223f89ae to your computer and use it in GitHub Desktop.
Save lsdr/138d40872f42ac34a6a9ac37223f89ae to your computer and use it in GitHub Desktop.
Small password gen in Rust
// Original code:
// https://github.com/rodolfoghi/learn-rust/tree/master/raspberrypi-projects/password_generator
//
use rand::Rng;
use std::io;
fn main() {
let chars = "abcdefghijklmnopqrstuvxz1234567890!@#$%&*()-+=?;:.><.\\|{}[]";
println!("Password Generator");
println!("==================");
println!("number of passwords?");
let mut number_of_passwords = String::new();
io::stdin()
.read_line(&mut number_of_passwords)
.expect("Failed to read number of passwords");
let number_of_passwords: u32 = number_of_passwords
.trim()
.parse()
.expect("Please type a number!");
println!("password length?");
let mut password_length = String::new();
io::stdin()
.read_line(&mut password_length)
.expect("Failed to read number of passwords");
let password_length: u32 = password_length
.trim()
.parse()
.expect("Please type a number!");
for _ in 0..number_of_passwords {
let mut new_password = String::new();
for _ in 0..password_length {
let start: usize = rand::thread_rng().gen_range(1, chars.len()) as usize;
let end: usize = start + 1;
new_password.push_str(&chars[start..end]);
}
println!("{}", new_password);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment