lib_wordpass/
lib.rs

1use std::fs::File;
2use std::{io, env};
3use std::io::BufRead;
4use std::path::Path;
5use rand::Rng;
6use std::fmt::Write;
7use inflector::Inflector;
8
9/// Generate a password using words selected at random from the list of words.
10/// The resulting string will have at least *min_letter_count* letters (excluding spaces)
11/// and at least *min_word_count* words.
12/// If *initial_capital* is true, the first word will begin with a capital letter
13/// If *use_caps* is true, all words will begin with a capital letter.
14/// If *use_spaces* is true, words will be separated by spaces.
15pub fn generate_password(words:Vec<String>, min_letter_count:u8, min_word_count:u8, initial_capital:bool, use_caps:bool, use_spaces:bool ) ->String{
16
17    let mut char_count = 0;
18    let mut rng = rand::thread_rng();
19    let mut chosen_words = vec![];
20    while char_count < min_letter_count as usize || chosen_words.len()<min_word_count as usize {
21        let n: usize = rng.gen();
22        let pos = n % words.len();
23        chosen_words.push(words.get(pos).unwrap());
24        char_count += chosen_words.last().unwrap().len();
25    }
26    let mut chosen_iter = chosen_words.iter();
27    let mut res = String::new();
28    if initial_capital || use_caps {
29        write!(res, "{}", chosen_iter.next().unwrap().to_title_case()).expect("Unable to write data");
30    } else {
31        write!(res, "{}", chosen_iter.next().unwrap()).expect("Unable to write data");
32    }
33    while let Some(word) = chosen_iter.next() {
34        if use_spaces {
35            write!(res, " ").expect("Unable to write data");
36        }
37        if use_caps {
38            write!(res, "{}", word.to_title_case()).expect("Unable to write data");
39        } else {
40            write!(res, "{}", word).expect("Unable to write data");
41        }
42    }
43    res
44}
45/// Get a *Vec<String>* of words from a file.
46/// If filter is true, words which are not lowercase ascii are filtered. This is helpful to filter
47/// out words with apostrophes, proper nouns or names beginning with capital letters.
48pub fn get_word_list(file:File, filter:bool) -> Vec<String> {
49    let file_reader =  io::BufReader::new(file);
50    let mut words = Vec::with_capacity(10_000);
51
52    for l in file_reader.lines() {
53        let line = l.unwrap();
54        if !filter {
55            words.push(line);
56        } else {
57            if !line.contains(|c:char|!c.is_ascii_lowercase()) {
58                words.push(line);
59            }
60        }
61    }
62    words
63}
64
65///Try to get a reasonable default word list. This will sometimes be effective on modern linux systems
66/// We look first in the user's config folder ($XDG_CONFIG_HOME) to see if there is a folder for wordpass.
67/// If so, and it contains a file words.txt, we use that.
68/// If not, we look for a system dictionary in */usr/share/dict/words*
69/// Returns the filename for a dictionary if found, else None.
70pub fn get_default_filename() -> Option<String> {
71
72    let word_list_in_config =  env::var("XDG_CONFIG_HOME")
73        .unwrap_or(env::var("HOME")
74            .unwrap_or("~/".to_string())+".config")
75        + "wordpass/words.txt";
76
77    if Path::exists(Path::new(&word_list_in_config)) {
78        return Some(word_list_in_config);
79    }
80    let default_dictionary = "/usr/share/dict/words".to_string();
81    if Path::exists(Path::new(&default_dictionary)) {
82        return Some(default_dictionary);
83    }
84    None
85}
86