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:

  1. passwords must be at least 8 characters long, and
  2. they must use at least 3 of the following 4 types of characters:

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.

  1. Create a new Eclipse project by copying ProjectTemplate. Name the new project PasswordChecker.
  2. Open the src folder of this project and then open (default package). As a starting point you should use ProgramWithIOAndStaticMethod.java. Rename it CheckPassword and delete the other files from the project.

Method

  1. 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: The String methods are summarized here. The int method length() will be useful in completing this first task.
  2. Now add a check to checkPassword that the string also contains an upper case letter. For this check you should implement and use another static method declared as follows: You may find the boolean static method Character.isUpperCase(char) and the String class char method charAt(int) useful.
  3. In a similar fashion, add checks to checkPassword that the string contains a lower case letter and that it contains a digit. Declare and use appropriate static methods following the example of containsUpperCaseLetter. The boolean static methods Character.isLowerCase(char) and Character.isDigit(char) can simplify this task. Also modify checkPassword so 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

  1. 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 useful String method is the int method indexOf(int).
  2. 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).