Skip to content

Instantly share code, notes, and snippets.

@gooh
Created July 27, 2015 12:37
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 gooh/dd6407813651aa040f22 to your computer and use it in GitHub Desktop.
Save gooh/dd6407813651aa040f22 to your computer and use it in GitHub Desktop.
A simple secure password generator
extern crate rand;
extern crate docopt;
extern crate rustc_serialize;
use rand::{OsRng, Rng};
use docopt::Docopt;
static USAGE: &'static str = "
Usage: rpgen [options]
rpgen --help
Options:
-h, --help Shows this message.
-l, --length <chars> Generates a password with this many chars [default: 10].
-a, --amount <amount> Generates this many passwords [default: 1].
";
#[derive(RustcDecodable, Debug)]
struct Args {
flag_length: usize,
flag_amount: usize
}
fn main() {
let args: Args = Docopt::new(USAGE)
.and_then(|d| d.decode())
.unwrap_or_else(|e| e.exit());
let choices:Vec<char> = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890+*~#!§$%&/()=?[]{}'-_.,;:<>|\\\"".chars().collect();
let length = args.flag_length;
let mut amount = args.flag_amount;
while amount > 0 {
println!("{}", gen_pw(&choices, length));
amount -= 1;
}
}
fn gen_pw(choices: &Vec<char>, length: usize) -> String {
let mut rng = OsRng::new().unwrap();
let mut new_password = String::new();
while new_password.len() < length {
let new_char = rng.choose(&choices).unwrap().to_string();
new_password = new_password + &new_char;
}
return new_password;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment