Skip to content
This repository has been archived by the owner on Dec 26, 2024. It is now read-only.

Commit

Permalink
add password generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ojensen5115 committed Jun 18, 2017
1 parent aa2c324 commit 468e3a7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ rustyline = "1.0.0" # for getting user input
clipboard = "0.3.0" # for copying usernames / passwords
serde = "1.0" # for rusqlite
serde_derive = "1.0" # for rusqlite
serde_json = "1.0" # for keybase verification
serde_json = "1.0" # for keybase verification
rand = "0.3.15" # for password generation
2 changes: 1 addition & 1 deletion bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ _pw()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"

commands="add edit delete list show copy"
commands="add edit delete list show copy generate"

case "$prev" in
pw)
Expand Down
51 changes: 42 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ extern crate clipboard;
use clipboard::ClipboardProvider;
use clipboard::ClipboardContext;

use std::env;

use std::path::{PathBuf, Path};
extern crate rand;
use rand::Rng;
use rand::os::OsRng;

use std::env;
use std::io;
use std::io::prelude::*;

use std::path::{PathBuf, Path};
use std::process::Command;

const INI_PATH: &'static str = ".pwrc";
Expand All @@ -44,15 +45,24 @@ Usage:
pw list <category>
pw show <name>
pw copy <name> (u|p)
pw [options]
pw generate [--chars=<num>]
pw -h | --help | --version
pw --comp-name | --comp-sec
Options:
-h --help Show this screen.
--version Show version.
--comp-name List credential names for tab completion
--comp-sec List categories for tab completion
-h --help Show this screen.
--version Show version.
--chars=<num> Number of characters to generate [default: 32]
--comp-name List credential names for tab completion
--comp-sec List categories for tab completion
";

const CHAR_ALPHA: &'static str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const CHAR_NUM: &'static str = "1234567890";
const CHAR_SYMBOL: &'static str = "!@#$%^&*()";

#[derive(Debug, Deserialize)]
struct Args {
cmd_add: bool,
Expand All @@ -62,12 +72,14 @@ struct Args {
cmd_copy: bool,
cmd_edit: bool,
cmd_delete: bool,
cmd_generate: bool,

cmd_u: bool,
cmd_p: bool,

flag_comp_name: bool,
flag_comp_sec: bool,
flag_chars: usize,

arg_name: String,
arg_category: Option<String>
Expand Down Expand Up @@ -115,6 +127,9 @@ fn main() {
else if args.cmd_delete {
delete_credential(&conn, args.arg_name);
}
else if args.cmd_generate {
generate_password(args.flag_chars);
}
else if args.flag_comp_name {
completion_name(&conn);
}
Expand Down Expand Up @@ -291,6 +306,24 @@ fn delete_credential(conn: &rusqlite::Connection, name: String) {
};
}

fn generate_password(num_chars: usize) {
let mut rand = match OsRng::new() {
Ok(g) => g,
Err(e) => panic!("Failed to obtain OS RNG: {}", e)
};
println!("Generating {}-character password:", num_chars);

//let password = rand.gen_ascii_chars().take(num_chars).collect::<String>();
let haystack = CHAR_ALPHA.to_owned() + CHAR_NUM + CHAR_SYMBOL;
let mut pw = Vec::new();
for _ in 1..num_chars {
pw.push(*rand.choose(haystack.as_bytes()).unwrap());
}
let password = String::from_utf8(pw).unwrap();

println!(" {}", password);
}

fn initialize_datastore(data_path: &str) -> rusqlite::Connection {
let path = Path::new(data_path);
let db_exists = path.is_file();
Expand Down

0 comments on commit 468e3a7

Please sign in to comment.