-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPasswordStrengthChecker.java
57 lines (50 loc) · 2.73 KB
/
PasswordStrengthChecker.java
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
public class PasswordStrengthChecker {
public String strengthCheck(String password) {
boolean hasUpperCase = false;
boolean hasLowerCase = false;
boolean hasSymbol = false;
boolean hasNumber = false;
// Define character types used
for (int i = 0; i < password.length(); i++) {
for (int upperCaseCounter = 0; upperCaseCounter < Alphabet.UPPERCASE_LETTERS.length(); upperCaseCounter++) {
if (password.charAt(i) == Alphabet.UPPERCASE_LETTERS.charAt(upperCaseCounter)) {
hasUpperCase = true;
break;
}
}
for (int lowerCaseCounter = 0; lowerCaseCounter < Alphabet.LOWERCASE_LETTERS.length(); lowerCaseCounter++) {
if (password.charAt(i) == Alphabet.LOWERCASE_LETTERS.charAt(lowerCaseCounter)) {
hasLowerCase = true;
break;
}
}
for (int symbolCounter = 0; symbolCounter < Alphabet.SYMBOLS.length(); symbolCounter++) {
if (password.charAt(i) == Alphabet.SYMBOLS.charAt(symbolCounter)) {
hasSymbol = true;
break;
}
}
for (int numberCounter = 0; numberCounter < Alphabet.NUMBERS.length(); numberCounter++) {
if (password.charAt(i) == Alphabet.NUMBERS.charAt(numberCounter)) {
hasNumber = true;
break;
}
}
}
// Define message to be displayed
if (hasUpperCase && hasLowerCase && hasSymbol && hasNumber) {
return "This is a very strong password!";
} else if (hasLowerCase && hasSymbol && (hasUpperCase || hasNumber) || hasLowerCase && hasNumber && hasUpperCase || hasSymbol && hasNumber && (hasUpperCase || hasLowerCase)
) {
return "This password is strong, try adding one more uppercase or lowercase character, symbol, or number to maximize the strength.";
} else if ((hasUpperCase && hasLowerCase) || (hasUpperCase && hasSymbol) || (hasUpperCase && hasNumber) ||
(hasLowerCase && hasSymbol) || (hasLowerCase && hasNumber) || (hasSymbol && hasNumber)
) {
return "This is a weak password, try adding upper and lowercase characters, symbols, and numbers to strengthen your password.";
} else if (hasUpperCase || hasLowerCase || hasSymbol || hasNumber) {
return "This is a very weak password, try adding upper and lowercase characters, symbols, and numbers to strengthen your password.";
} else {
return "Please add a valid password.";
}
}
}