diff options
-rw-r--r-- | LICENSE | 21 | ||||
-rw-r--r-- | README.md | 16 | ||||
-rw-r--r-- | src/main.rs | 52 |
3 files changed, 66 insertions, 23 deletions
@@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 Michaël Ball + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..5bf31bd --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# rust-passphrasegen +A passphrase generator written in Rust. + +## Usage +Build by running `cargo build --release`. + +Generate a passphrase by running `passphrasegen`. + +Required arguments: +* Path to words file (eg. /usr/share/dict/words) + +### Example + +Generate a passphrase of 5 words using the american english wordlist: + + $ passphrasegen -w 5 /usr/share/dict/american-english diff --git a/src/main.rs b/src/main.rs index c78441e..c8a1e01 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,17 +4,19 @@ extern crate rand; use getopts::Options; use rand::{thread_rng, Rng}; use std::env; -use std::vec::Vec; use std::fs::File; use std::io::{self, BufRead, BufReader}; use std::path::Path; +use std::vec::Vec; fn lines_from_file<P>(filename: &P) -> Result<Vec<String>, io::Error> - where P: AsRef<Path> +where + P: AsRef<Path>, { let file = try!(File::open(filename)); let buf = BufReader::new(file); - Ok(buf.lines() + Ok(buf + .lines() .map(|l| l.expect("Could not parse line")) .collect()) } @@ -40,11 +42,13 @@ fn random_words_from_dictionary(dictionary: &Vec<String>, words: &usize) -> Vec< #[test] fn test_random_words_from_dictionary() { - let test_dict = vec!["This".to_string(), - "is".to_string(), - "a".to_string(), - "test".to_string(), - "vector".to_string()]; + let test_dict = vec![ + "This".to_string(), + "is".to_string(), + "a".to_string(), + "test".to_string(), + "vector".to_string(), + ]; let num_words = 3; let new_vec = random_words_from_dictionary(&test_dict, &num_words); @@ -56,10 +60,12 @@ fn main() { let program = args[0].clone(); let mut opts = Options::new(); - opts.optopt("w", - "", - "number of words to use in passphrase, default is 4", - "WORDS"); + opts.optopt( + "w", + "", + "number of words to use in passphrase, default is 4", + "WORDS", + ); let matches = match opts.parse(&args[1..]) { Ok(m) => m, @@ -77,15 +83,13 @@ fn main() { let num_words; match words_opt { - Some(x) => { - match x.parse::<usize>() { - Ok(n) => num_words = n, - Err(_e) => { - println!("Please enter an integer for the -w flag"); - return; - } + Some(x) => match x.parse::<usize>() { + Ok(n) => num_words = n, + Err(_e) => { + println!("Please enter an integer for the -w flag"); + return; } - } + }, None => num_words = 4, } @@ -94,9 +98,11 @@ fn main() { match lines_from_file(&input) { Ok(n) => lines = n, Err(_e) => { - println!("Cannot read file {}. Please ensure it exists and you have permission to \ - read it.", - input); + println!( + "Cannot read file {}. Please ensure it exists and you have permission to \ + read it.", + input + ); return; } } |