aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: c8a1e013c5d4bb5b22e852e34abb9eba90528eb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
extern crate getopts;
extern crate rand;

use getopts::Options;
use rand::{thread_rng, Rng};
use std::env;
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>,
{
    let file = try!(File::open(filename));
    let buf = BufReader::new(file);
    Ok(buf
        .lines()
        .map(|l| l.expect("Could not parse line"))
        .collect())
}

fn print_usage(program: &str, opts: Options) {
    let brief = format!("Usage: {} DICTIONARY_FILE [options]", program);
    print!("{}", opts.usage(&brief));
}

fn random_words_from_dictionary(dictionary: &Vec<String>, words: &usize) -> Vec<String> {
    let mut rng = thread_rng();
    let max_idx = dictionary.len() - 1;

    let mut vec = Vec::with_capacity(*words);
    for _ in 0..*words {
        let n: usize = rng.gen_range(0, max_idx);
        let ref token = dictionary[n];
        vec.push(token.to_string());
    }

    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 num_words = 3;

    let new_vec = random_words_from_dictionary(&test_dict, &num_words);
    assert_eq!(new_vec.len(), 3);
}

fn main() {
    let args: Vec<String> = env::args().collect();
    let program = args[0].clone();

    let mut opts = Options::new();
    opts.optopt(
        "w",
        "",
        "number of words to use in passphrase, default is 4",
        "WORDS",
    );

    let matches = match opts.parse(&args[1..]) {
        Ok(m) => m,
        Err(f) => panic!("{}", f),
    };

    let words_opt = matches.opt_str("w");

    let input = if !matches.free.is_empty() {
        matches.free[0].clone()
    } else {
        print_usage(&program, opts);
        return;
    };

    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;
            }
        },
        None => num_words = 4,
    }

    let lines;

    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
            );
            return;
        }
    }

    let passphrase = random_words_from_dictionary(&lines, &num_words).join(" ");

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