Java Reference
In-Depth Information
validate() method (no return data; parameter is String: entered password)
Begin
verify format of new password
encrypt entered password
If the history is not empty
Obtain the current password from history
If entered password <> current password
throw Exception: invalid password
End If
If password has expired
throw Exception: password has expired
End If
If password autoexpires
decrement remaining uses by 1
If no remaining uses
Set password to expired
End If
End If
Else
throw Exception: no password in history
End If
End
FIGURE 9-30
is not empty. Thus, the password history list, thus, always should contain at least
one value for a given Password object, unless the history list somehow was cor-
rupted. Although it never should happen, it is safest to check for an empty list.
Because items in an ArrayList can be accessed much like items in any ordi-
nary array, any password value in the history list can be accessed directly by
using the proper index value. The current password is the latest one in the list,
which has an index equal to the size of the list minus one. Line 176, as shown in
Figure 9-32 on the next page, calculates the current password index and then
line 177 obtains the item in the ArrayList with this index.
Next, the validate() method verifies that the entered password matches the
latest entry in the password history list. If it does not match, an exception is
thrown. If it does match, the validate() method then determines if the password
is set to expire automatically, which is true by default. If the password is set to
expire automatically and the number of remaining uses is zero, then the
validate() method must throw an exception; otherwise, the method simply
decrements the remaining uses.
Figures 9-31 and 9-32 on the next page display the code for the set() and
validate() methods, respectively.
Search WWH ::




Custom Search