-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckPasswords.java
181 lines (145 loc) · 5.07 KB
/
CheckPasswords.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import java.util.List;
/**
* Check rules on UMD passwords, as described at
* https://identity.umd.edu/password/changepassword
*
* <ul>
* <li>A password must be at least 8 and no more than 32 characters in length.
* <li>A password must contain at least one character from each of the following
* sets:
* <ul>
* <li>Uppercase alphabet (A-Z)
* <li>Lowercase alphabet (a-z)
* <li>Number (0-9) and special characters (such as # @ $ & among others)
* </ul>
* <li>A password may not begin or end with the space character.
* <li>A password may not contain more than two consecutive identical
* characters.
* <li>A password may not be (or be a variation of ) a dictionary word in
* English or many other languages. This includes making simple substitutions of
* digits or punctuation that resemble alphabetic characters (such as replacing
* the letter S in a common word with the $...
* <li>Passwords should not contain: carriage return, linefeed, /, \, or a
* trailing * symbol).
* </ul>
*
*/
public class CheckPasswords {
/**
* Count the number of uppercase letters in password; can assume only ASCII
* characters
*/
static int countUppercaseLetters(String password) {
int count = 0;
for (int x = 0; x < password.length(); x++) {
if (password.charAt(x) >= 'A' && password.charAt(x) <= 'Z')
count++;
}
return count;
}
/**
* Count the number of lowercase letters in password; can assume only ASCII
* characters
*/
static int countLowercaseLetters(String password) {
int count = 0;
for (int x = 0; x < password.length(); x++) {
if (password.charAt(x) >= 'a' && password.charAt(x) <= 'z')
count++;
}
return count;
}
/**
* Count the longest sequences of consecutive identical characters; can assume
* only ASCII characters
*/
static int longestConsecutiveIdenticalCharacters(String password) {
int longestConsec = 0;
int currentLongestConsec = 0;
int x = 0;
int y = 0;
while (x < password.length()) {
while (y < password.length() && password.charAt(x) == password.charAt(y)) {
currentLongestConsec++;
y++;
}
x = y;
if (currentLongestConsec > longestConsec)
longestConsec = currentLongestConsec;
currentLongestConsec = 0;
}
return longestConsec;
}
/**
* Check to see if a password is to similar to a dictionary word. It is too
* similar if the dictionary word is contained in the password when ignoring
* case and treating '1' and 'l' as identical , 'o' and '0' as identical, and
* 's' and '$' as identical, and the length of the password is at least 5
* characters longer than the word.
*/
static boolean similarToWord(String word, String password) {
int x = 0;
int y = 0;
boolean hasMoreThanFive = password.length() > word.length() + 4;
boolean containsWord = false;
while (x < password.length()) {
if (y < word.length() && (password.charAt(x) == word.charAt(y) || password.charAt(x) + 32 == word.charAt(y)
|| password.charAt(x) == '$' && word.charAt(y) == 's'
|| password.charAt(x) == '0' && word.charAt(y) == 'o'
|| password.charAt(x) == '1' && word.charAt(y) == 'l')) {
y++;
} else if (y == word.length()) {
containsWord = true;
break;
} else {
y = 0;
if (y < word.length()
&& (password.charAt(x) == word.charAt(y) || password.charAt(x) + 32 == word.charAt(y)
|| password.charAt(x) == '$' && (word.charAt(y) == 's' || word.charAt(y) == 'S')
|| password.charAt(x) == '0' && word.charAt(y) == 'o' || word.charAt(y) == 'O')
|| password.charAt(x) == '1' && word.charAt(y) == 'l')
y++;
}
x++;
}
return containsWord && !hasMoreThanFive;
}
/** Check to see if password is an acceptable password by UMD standards */
static boolean checkPassword(String password, List<String> dictionary) {
boolean hasLowerCase = false;
boolean hasUpperCase = false;
boolean hasNonLetter = false;
boolean inRange = password.length() >= 8 && password.length() <= 32;
boolean startsOrEndsWithSpace = password.startsWith(" ") || password.endsWith(" ");
boolean doesNotHaveWord = true;
boolean noConsec = longestConsecutiveIdenticalCharacters(password) < 3;
boolean noControlFeed = password.indexOf((char)13) == -1;
boolean noLineFeed = password.indexOf((char)10) == -1;
boolean noBackslash = password.indexOf("/") == -1 && password.indexOf("\\") == -1;
boolean noTrailingAsterisk = !password.endsWith("*");
for (int x = 0; x < password.length(); x++) {
if (password.charAt(x) >= 'A' && password.charAt(x) <= 'Z')
hasUpperCase = true;
if (password.charAt(x) >= 'a' && password.charAt(x) <= 'z')
hasLowerCase = true;
if (password.charAt(x) < 'A' || (password.charAt(x) > 'Z' && password.charAt(x) < 'a')
|| (password.charAt(x) > 'z'))
hasNonLetter = true;
}
for (String s : dictionary) {
if (similarToWord(s, password))
doesNotHaveWord = false;
}
return noConsec &&
doesNotHaveWord &&
hasLowerCase &&
hasUpperCase &&
hasNonLetter &&
inRange &&
!startsOrEndsWithSpace &&
noControlFeed &&
noLineFeed &&
noBackslash &&
noTrailingAsterisk;
}
}