-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpassword.rs
54 lines (46 loc) · 1.37 KB
/
password.rs
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
use crate::misc;
pub fn generate(upper: bool, numeric: bool, special: bool, mut num: i8) -> String {
if num < 5 {
num = 5;
}
letter(upper, numeric, special, num)
}
fn letter(upper: bool, numeric: bool, special: bool, num: i8) -> String {
let mut pw = String::from("");
let mut opts = vec![1i8];
if upper {
opts.push(2);
}
if numeric {
opts.push(3);
}
if special {
opts.push(4);
}
let lower_str = b"abcdefghijklmnopqrstuvwxyz";
let upper_str = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let numeric_str = b"0123456789";
let special_str = b"!@#$%&*+-=?";
match opts[misc::random_data_index(opts.as_slice())] {
1 => pw.push(misc::random_char_from_string(lower_str)),
2 => pw.push(misc::random_char_from_string(upper_str)),
3 => pw.push(misc::random_char_from_string(numeric_str)),
4 => pw.push(misc::random_char_from_string(special_str)),
_ => pw.push(misc::random_char_from_string(lower_str)),
}
match num {
0 => format!("{}", pw),
_ => format!("{}{}", pw, letter(upper, numeric, special, num - 1)),
}
}
#[cfg(test)]
mod tests {
use crate::password;
use crate::testify::exec_mes;
#[test]
fn password_generate() {
exec_mes("password::generate", || {
password::generate(true, true, true, 26)
});
}
}