Lab: Password Checker
The Problem
Many systems nowadays rely on the strength of user passwords for their security and therefore impose a variety of constraints on what are considered acceptable passwords. For instance, OSU requires that all passwords satisfy, among other criteria, the following conditions:
- passwords must be at least 8 characters long, and
- they must use at least 3 of the following 4 types of characters:
- upper case letters (e.g., A, B, C, ...)
- lower case letter (e.g., a, b, c, ...)
- digits (e.g., 1, 2, 3, ...)
- special characters (e.g., !, @, $, %, ...)
For this lab, you will implement a program that checks whether a string entered by the user meets several criteria to qualify as a valid password.
Setup
Follow these steps to set up a project for this lab.
- Create a new Eclipse project by copying
ProjectTemplate. Name the new projectPasswordChecker. - Open the
srcfolder of this project and then open(default package). As a starting point you should useProgramWithIOAndStaticMethod.java. Rename itCheckPasswordand delete the other files from the project.
Method
-
Edit
CheckPassword.java(including updating comments appropriately) to ask the user for a string (a password candidate), input it, and let the user know whether it satisfies certain criteria, and if not, which one(s) it fails to satisfy.You will do this in stages. In this first step, only check that the string satisfies the length requirement. In the next few steps, you will add other checks. Test your program after adding each new check to increase your confidence that the program is working.
All the checks should be done in a static method declared as follows:
/** * Checks whether the given String satisfies the OSU criteria for a valid * password. Prints an appropriate message to the given output stream. * * @param passwordCandidate * the String to check * @param out * the output stream */ private static void checkPassword(String passwordCandidate, SimpleWriter out) {...}The
Stringmethods are summarized here. Theintmethodlength()will be useful in completing this first task. -
Now add a check to
checkPasswordthat the string also contains an upper case letter. For this check you should implement and use another static method declared as follows:/** * Checks if the given String contains an upper case letter. * * @param str * the String to check * @return true if str contains an upper case letter, false otherwise */ private static boolean containsUpperCaseLetter(String str) {...}You may find the
booleanstatic methodCharacter.isUpperCase(char)and theStringclasscharmethodcharAt(int)useful. -
In a similar fashion, add checks to
checkPasswordthat the string contains a lower case letter and that it contains a digit. Declare and use appropriate static methods following the example ofcontainsUpperCaseLetter. Thebooleanstatic methodsCharacter.isLowerCase(char)andCharacter.isDigit(char)can simplify this task. Also modifycheckPasswordso that it rejects a password if it does not contain characters from at least two of the three types checked so far (upper case letters, lower case letters, and digits).
Additional Activities
- Complete the password checker by adding the check for special
characters (use a new static method like the ones for the other
checks) and rejecting passwords that do not contain characters from
at least three of the four types. For this step assume a valid
special character is one in the string
"!@#$%^&*()_-+={}[]:;,.?". Another usefulStringmethod is theintmethodindexOf(int). - Modify the main program so that it repeatedly asks the user for a new password, checks it and prints a message. The program should exit when the user enters an empty string (i.e., the user just presses the Enter key without entering any other characters).