Created
March 30, 2020 02:23
-
-
Save lsdr/138d40872f42ac34a6a9ac37223f89ae to your computer and use it in GitHub Desktop.
Small password gen in Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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