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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
extern crate rand;
use rand::prelude::*;
// Function to determine passphrase difficulty:
// easy, medium, hard, random, errror
pub fn difficulty(mode: &str) -> i32 {
if mode == "easy" {
thread_rng().gen_range(1, 3)
} else if mode == "medium" {
thread_rng().gen_range(3, 6)
} else if mode == "hard" {
thread_rng().gen_range(6, 9)
} else if mode == "random" {
thread_rng().gen_range(1, 9)
} else {
println!("Not a valid mode. Please set mode to easy, medium, or hard");
0
}
}
// Function to join all padding types into one vector
pub fn join_padding_types<'a>(numbers: Vec<&'a str>,
letters: Vec<&'a str>,
symbols: Vec<&'a str>,
other: Vec<&'a str>) -> Vec<&'a str> {
let mut v: Vec<&str> = Vec::new();
v.append(&mut numbers.clone());
v.append(&mut letters.clone());
v.append(&mut symbols.clone());
v.append(&mut other.clone());
v
}
// Function to create a padding string from the padding vector
pub fn padding(stuff: Vec<&str>,
len: i32) -> Vec<&str> {
let mut v: Vec<&str> = Vec::new();
let l = thread_rng().gen_range(1, len);
let s = thread_rng().gen_range(1, stuff.len());
for _i in 0..l {
v.push(stuff[s]);
}
v
}
// Function to generate a passphrase with padding
pub fn passphrase(words: Vec<&str>,
padding_chars: Vec<&str>,
mode: &str) -> String {
// init variables
let mut v: Vec<&str> = Vec::new();
let d = difficulty(mode);
let p = padding(padding_chars, d);
// add words and padding to vector
for _i in 0..d {
let w = thread_rng().gen_range(1, words.len());
v.push(words[w]);
v.append(&mut p.clone());
}
// convert vector to string
let mut passphrase = String::new();
for _i in v {
passphrase.push_str(_i);
}
// return passphrase string
passphrase
}
// Function to create a random password to compare with
// a random passphrase
pub fn password(stuff: Vec<&str>,
len: i32) -> String {
// init variables
let mut v: Vec<&str> = Vec::new();
// add random values to a vector
for _i in 0..len {
let s = thread_rng().gen_range(1, stuff.len());
v.push(stuff[s])
}
// convert vector to string
let mut password = String::new();
for _i in v {
password.push_str(_i);
}
// return password string
password
}
// Create Alphabets
// - https://users.rust-lang.org/t/storing-strings-in-a-crate-library/23130/13
/* original try
pub 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"];
pub static NUMS: &'static [&'static str] = &["0","1","2","3","4","5","6","7","8","9"];
pub static SYMBOLS: &'static [&'static str] = &["`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "=", "+", "[", "{", "]", "}", "?", ".", ">", ",", "<", ";", "|", ":", "/"];
pub 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"];
pub static OTHER: &'static [&'static str] = &[""];
*/
/* as fixed arrays
pub 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"];
pub static NUMS: [&'static str; 10] = ["0","1","2","3","4","5","6","7","8","9"];
pub static SYMBOLS: [&'static str; 29] = ["`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "=", "+", "[", "{", "]", "}", "?", ".", ">", ",", "<", ";", "|", ":", "/"];
pub 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"];
pub static OTHER: [&'static str; 1] = [""];
*/
// this works with `cargo run`
pub 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"];
pub static NUMS: [&str; 10] = ["0","1","2","3","4","5","6","7","8","9"];
pub static SYMBOLS: [&str; 29] = ["`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "=", "+", "[", "{", "]", "}", "?", ".", ">", ",", "<", ";", "|", ":", "/"];
pub 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"];
pub static OTHER: [&str; 1] = [""];