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
use lazy_static::lazy_static;
use regex::Regex;
/// Checks if the string is a strong password.
/// A strong password must be 8 characters long and then contain characters from at least 3 of the following 4 rules:
/// 1. Upper case English letters.
/// 2. Lower case English letters.
/// 3. Numbers(0-9).
/// 4. Non-alpha.
///
/// # Arguments
///
/// * `s` - The string to check.
///
/// # Returns
///
/// Returns true if string is a strong password, false if not.
///
/// # Examples
///
/// ```
/// use rufl::string;
///
/// assert_eq!(true, string::is_strong_password("abc123@ABC"));
///
/// assert_eq!(false, string::is_strong_password("12@abAB"));
///
/// ```
pub fn is_strong_password(s: impl AsRef<str>) -> bool {
if s.as_ref().len() < 8 {
return false;
}
let has_number = NUMBER_REGEX.is_match(s.as_ref()) as i32;
let has_upper = UPPERCA_CASE_REGEX.is_match(s.as_ref()) as i32;
let has_lower = LOWER_CASE_REGEX.is_match(s.as_ref()) as i32;
let has_nonalpha = NONALPHA_REGEX.is_match(s.as_ref()) as i32;
has_number + has_upper + has_lower + has_nonalpha > 3
}
lazy_static! {
static ref UPPERCA_CASE_REGEX: Regex = Regex::new(r"[A-Z]").unwrap();
static ref LOWER_CASE_REGEX: Regex = Regex::new(r"[a-z]").unwrap();
static ref NUMBER_REGEX: Regex = Regex::new(r"[0-9]").unwrap();
static ref NONALPHA_REGEX: Regex = Regex::new(r"[^\w\s]").unwrap();
// static ref STRONG_PASSWORD_REGEX: Regex = Regex::new(r"^(?:(?=.*[a-z])(?:(?=.*[A-Z])(?=.*[\d\W])|(?=.*\W)(?=.*\d))|(?=.*\W)(?=.*[A-Z])(?=.*\d)).{8,}$").unwrap();
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_strong_password() {
assert_eq!(true, is_strong_password("abc123@ABC"));
assert_eq!(false, is_strong_password("abc123abc"));
assert_eq!(false, is_strong_password("abcABC"));
assert_eq!(false, is_strong_password("ab12AB"));
assert_eq!(false, is_strong_password("ab12@AB"));
}
}