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
use std::fs::File;
use std::{io, env};
use std::io::BufRead;
use std::path::Path;
use rand::Rng;
use std::fmt::Write;
use inflector::Inflector;
pub fn generate_password(words:Vec<String>, min_letter_count:u8, min_word_count:u8, initial_capital:bool, use_caps:bool, use_spaces:bool ) ->String{
let mut char_count = 0;
let mut rng = rand::thread_rng();
let mut chosen_words = vec![];
while char_count < min_letter_count as usize || chosen_words.len()<min_word_count as usize {
let n: usize = rng.gen();
let pos = n % words.len();
chosen_words.push(words.get(pos).unwrap());
char_count += chosen_words.last().unwrap().len();
}
let mut chosen_iter = chosen_words.iter();
let mut res = String::new();
if initial_capital || use_caps {
write!(res, "{}", chosen_iter.next().unwrap().to_title_case()).expect("Unable to write data");
} else {
write!(res, "{}", chosen_iter.next().unwrap()).expect("Unable to write data");
}
while let Some(word) = chosen_iter.next() {
if use_spaces {
write!(res, " ").expect("Unable to write data");
}
if use_caps {
write!(res, "{}", word.to_title_case()).expect("Unable to write data");
} else {
write!(res, "{}", word).expect("Unable to write data");
}
}
res
}
pub fn get_word_list(file:File, filter:bool) -> Vec<String> {
let file_reader = io::BufReader::new(file);
let mut words = Vec::with_capacity(10_000);
for l in file_reader.lines() {
let line = l.unwrap();
if !filter {
words.push(line);
} else {
if !line.contains(|c:char|!c.is_ascii_lowercase()) {
words.push(line);
}
}
}
words
}
pub fn get_default_filename() -> Option<String> {
let word_list_in_config = env::var("XDG_CONFIG_HOME")
.unwrap_or(env::var("HOME")
.unwrap_or("~/".to_string())+".config")
+ "wordpass/words.txt";
if Path::exists(Path::new(&word_list_in_config)) {
return Some(word_list_in_config);
}
let default_dictionary = "/usr/share/dict/words".to_string();
if Path::exists(Path::new(&default_dictionary)) {
return Some(default_dictionary);
}
None
}