Java Reference
In-Depth Information
134
// Sets password to a new value; keeps current & previous values in history up to max
number
135
public void
set
(
String
pswd
)
throws
Exception
136
{
137
String
encryptPswd;
138
boolean
pswdAdded =
true
;
139
140
pswd = pswd.trim
()
;
// remove any leading, trailing white space
141
verifyFormat
(
pswd
)
;
// verify password was entered properly
142
encryptPswd = encrypt
(
pswd
)
;
// convert to encrypted form
143
144
if
(
!pswdHistory.contains
(
encryptPswd
))
// if pswd not in recently used list
145
{
146
if
(
pswdHistory.size
()
== maxHistory
)
// if list is at max size
147
pswdHistory.remove
(
0
)
;
// remove 1st, oldest, pswd from list
148
149
pswdAdded = pswdHistory.add
(
encryptPswd
)
;
// add new pswd to end of ArrayList
150
151
if
(
!pswdAdded
)
// should never happen
152
throw new
Exception
(
"Internal list error - Password not accepted"
)
;
153
154
if
(
expired
)
// if pswd has expired,
155
expired =
false
;
// reset to not expired
156
157
if
(
autoExpires
)
// if pswd auto expires,
158
remainingUses = maxUses;
// reset uses to max
159
}
160
else
161
throw new
Exception
(
"Password recently used"
)
;
162
}
FIGURE 9-31
164
// Validates entered password against most recently saved value
165
public void
validate
(
String
pswd
)
throws
Exception
166
{
167
String
encryptPswd;
168
String
currentPswd;
169
int
currentPswdIndex;
170
171
verifyFormat
(
pswd
)
;
// verify password was entered properly
172
encryptPswd = encrypt
(
pswd
)
;
// convert to encrypted form
173
174
if
(
!pswdHistory.isEmpty
())
// at least one password entry is in history
175
{
176
currentPswdIndex = pswdHistory.size
()
-1;
177
currentPswd =
(
String
)
pswdHistory.get
(
currentPswdIndex
)
;
178
179
if
(
!encryptPswd.equals
(
currentPswd
))
// if not most recent pswd
180
throw new
Exception
(
"Password is invalid"
)
;
181
182
if
(
expired
)
183
throw new
Exception
(
"Password has expired - please change"
)
;
184
185
if
(
autoExpires
)
186
{
187
--remainingUses;
188
if
(
remainingUses <= 0
)
189
expired =
true
;
190
}
191
}
192
else
193
throw new
Exception
(
"No password on file - list corrupted!"
)
;
// should never
happen
194
195
196
}
FIGURE 9-32
Search WWH ::
Custom Search