Last active
July 21, 2022 10:44
-
-
Save 1franck/24a9eaf1ba0247df9cb3e02025034919 to your computer and use it in GitHub Desktop.
simple password gen in rust as learning exercise
This file contains 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
fn password_generate(len: i32) -> String { | |
let mut rng = rand::thread_rng(); | |
let mut password = String::from(""); | |
let lower_chars = String::from("abcdefghijklmopqrstuvwxyz"); | |
let upper_chars = String::from("QWERTYUIOPASDFGHJKLZXCVBNM"); | |
let number_chars = String::from("0987654321"); | |
let symbols_chars = String::from("$!@?%+,().-[];:><\\~#&|/_*"); | |
let mut chars_coll: Vec<char> = lower_chars.chars().collect(); | |
chars_coll.append(&mut upper_chars.chars().collect()); | |
chars_coll.append(&mut number_chars.chars().collect()); | |
chars_coll.append(&mut symbols_chars.chars().collect()); | |
for _ in 0..len { | |
password.push_str(&chars_coll[rng.gen_range(0..chars_coll.len())].to_string()); | |
} | |
return password; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment