entropy_test/
lib.rs

1extern crate rand;
2use rand::prelude::*;
3
4
5// Function to determine passphrase difficulty: 
6// easy, medium, hard, random, errror
7pub fn difficulty(mode: &str) -> i32 {
8    if mode == "easy" {
9        thread_rng().gen_range(1, 3)
10    } else if mode == "medium" {
11        thread_rng().gen_range(3, 6)
12    } else if mode == "hard" {
13        thread_rng().gen_range(6, 9)
14    } else if mode == "random" {
15        thread_rng().gen_range(1, 9)
16    } else {
17        println!("Not a valid mode. Please set mode to easy, medium, or hard");
18        0
19    }
20}
21
22// Function to join all padding types into one vector
23pub fn join_padding_types<'a>(numbers: Vec<&'a str>,
24                              letters: Vec<&'a str>,
25                              symbols: Vec<&'a str>,
26                              other: Vec<&'a str>) -> Vec<&'a str> {
27    
28    let mut v: Vec<&str> = Vec::new();
29    
30    v.append(&mut numbers.clone());
31    v.append(&mut letters.clone());
32    v.append(&mut symbols.clone());
33    v.append(&mut other.clone());
34    
35    v
36    
37}
38
39// Function to create a padding string from the padding vector
40pub fn padding(stuff: Vec<&str>,
41               len: i32) -> Vec<&str> {
42    
43    let mut v: Vec<&str> = Vec::new();
44    let l = thread_rng().gen_range(1, len);
45    let s = thread_rng().gen_range(1, stuff.len());
46    
47    for _i in 0..l {
48        v.push(stuff[s]);
49    }
50        
51    v
52    
53}
54
55// Function to generate a passphrase with padding
56pub fn passphrase(words: Vec<&str>,
57                  padding_chars: Vec<&str>,
58                  mode: &str) -> String {
59    
60    // init variables
61    let mut v: Vec<&str> = Vec::new();
62    let d = difficulty(mode);
63    let p = padding(padding_chars, d);
64    
65    // add words and padding to vector
66    for _i in 0..d {
67        let w = thread_rng().gen_range(1, words.len());
68        v.push(words[w]);
69        v.append(&mut p.clone());
70    }
71
72    // convert vector to string
73    let mut passphrase = String::new();
74    for _i in v {
75        passphrase.push_str(_i);
76    }
77
78    // return passphrase string
79    passphrase
80    
81}
82
83// Function to create a random password to compare with 
84// a random passphrase
85pub fn password(stuff: Vec<&str>,
86                len: i32) -> String {
87        
88    // init variables
89    let mut v: Vec<&str> = Vec::new();
90    
91    // add random values to a vector
92    for _i in 0..len {
93        let s = thread_rng().gen_range(1, stuff.len());
94        v.push(stuff[s])
95    }
96        
97    // convert vector to string
98    let mut password = String::new();
99    for _i in v {
100        password.push_str(_i);
101    }
102    
103    // return password string
104    password
105}
106
107// Create Alphabets
108// - https://users.rust-lang.org/t/storing-strings-in-a-crate-library/23130/13
109
110/* original try
111pub static WORDS: &'static [&'static str] = &["apple", "bagel", "cat", "dolphin", "elephant", "frog", "gopher", "horse", "iguana", "jackrabbit", "kangaroo", "lizzard", "monkey", "nightengale", "otter", "prayingmantis", "quail", "rooster", "snake", "turtle", "ummm", "velocorapter", "walrus", "x", "yarr", "zebra"];
112pub static NUMS: &'static [&'static str] = &["0","1","2","3","4","5","6","7","8","9"];
113pub static SYMBOLS: &'static [&'static str] = &["`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "=", "+", "[", "{", "]", "}", "?", ".", ">", ",", "<", ";", "|", ":", "/"]; 
114pub static LETTERS: &'static [&'static str] = &["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
115pub static OTHER: &'static [&'static str] = &[""];
116*/
117
118/* as fixed arrays
119pub static WORDS: [&'static str; 26] = ["apple", "bagel", "cat", "dolphin", "elephant", "frog", "gopher", "horse", "iguana", "jackrabbit", "kangaroo", "lizzard", "monkey", "nightengale", "otter", "prayingmantis", "quail", "rooster", "snake", "turtle", "ummm", "velocorapter", "walrus", "x", "yarr", "zebra"];
120pub static NUMS: [&'static str; 10] = ["0","1","2","3","4","5","6","7","8","9"];
121pub static SYMBOLS: [&'static str; 29] = ["`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "=", "+", "[", "{", "]", "}", "?", ".", ">", ",", "<", ";", "|", ":", "/"]; 
122pub static LETTERS: [&'static str; 26] = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
123pub static OTHER: [&'static str; 1] = [""];
124*/
125
126// this works with `cargo run`
127pub static WORDS: [&str; 26] = ["apple", "bagel", "cat", "dolphin", "elephant", "frog", "gopher", "horse", "iguana", "jackrabbit", "kangaroo", "lizzard", "monkey", "nightengale", "otter", "prayingmantis", "quail", "rooster", "snake", "turtle", "ummm", "velocorapter", "walrus", "x", "yarr", "zebra"];
128pub static NUMS: [&str; 10] = ["0","1","2","3","4","5","6","7","8","9"];
129pub static SYMBOLS: [&str; 29] = ["`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "=", "+", "[", "{", "]", "}", "?", ".", ">", ",", "<", ";", "|", ":", "/"]; 
130pub static LETTERS: [&str; 26] = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
131pub static OTHER: [&str; 1] = [""];