Class PasswordChecker


  • public final class PasswordChecker
    extends Object
    Validate passwords according to predefined criteria.

    The following criteria are available:

    • minimum length;
    • maximum length;
    • constraints on characters composing the password:
      • character must belong to a predefined group of characters;
      • minimum and maximum number of characters per group can be specified.
    • Method Detail

      • factory

        public static PasswordChecker.Factory factory()
        Create a factory to specify password validation criteria and create a PasswordChecker object.
        Returns:
        an internal PasswordChecker factory
        See Also:
        PasswordChecker.Factory
      • getMinLength

        public int getMinLength()
        Get the minimum length required for a password to be accepted.
        Returns:
        the minimum length required for a password to be accepted
      • getMaxLength

        public int getMaxLength()
        Get the maximum length required for a password to be accepted.
        Returns:
        the maximum length required for a password to be accepted
      • quickCheck

        public boolean quickCheck​(String password)
        Check if a password can be validated against the specified criteria and return true if that's the case.
        Parameters:
        password - to be validated
        Returns:
        true if password matches the criteria, false otherwise
      • check

        public PasswordCheckStatus check​(String password)
        Check if a password can be validated against the specified criteria and return the first error encountered if any or PasswordCheckStatus.OK otherwise.

        Criteria are checked in this order:

        • minimum length of password;
        • maximum length of password;
        • illegal characters;
        • not enough characters from a certain group;
        • too many characters form a certain group.
        Parameters:
        password - to be validated
        Returns:
        PasswordCheckStatus.OK if password matches the criteria, otherwise a code for the first error encountered
        See Also:
        PasswordCheckStatus
      • fullCheck

        public List<PasswordCheckError> fullCheck​(String password)
        Check if a password can be validated against the specified criteria and return a list of all the problems encountered. This list is empty if there is no error.
        Parameters:
        password - to be validated
        Returns:
        a list of all the errors encountered while validating the password or an empty list if the password matches all the criteria
        See Also:
        PasswordCheckError
      • getCharacterGroups

        public List<String> getCharacterGroups()
        Returns a list of the character groups composing the generated passwords.
        Returns:
        the character groups
      • getCharacterGroupCount

        public int getCharacterGroupCount()
        Returns how many character groups are used in composing passwords.
        Returns:
        character group count
      • getCharacterGroup

        public String getCharacterGroup​(int index)
        Return the nth character group used in creating passwords. Character groups are referenced in the order they are added to the factory. The first index is 0 (zero).
        Parameters:
        index - of the character group to retrieve
        Returns:
        the character group at the index
        Throws:
        IndexOutOfBoundsException - if the index is invalid
      • getMinCharactersInGroup

        public int getMinCharactersInGroup​(int index)
        Return how many characters from the nth character group are required in generated passwords. Character groups are referenced in the order they were added to the factory. The first index is 0 (zero).
        Parameters:
        index - of the character group minimum count to retrieve
        Returns:
        the minimum number of characters from the group at the index required to form a valid password
        Throws:
        IndexOutOfBoundsException - if the index is invalid
      • getMaxCharactersInGroup

        public int getMaxCharactersInGroup​(int index)
        Return the maximum number of characters from the nth character group allowed in generated passwords. Character groups are referenced in the order they were added to the factory. The first index is 0 (zero).
        Parameters:
        index - of the character group maximum count to retrieve
        Returns:
        the maximum number of characters from the group at the index allowed in a valid password
        Throws:
        IndexOutOfBoundsException - if the index is invalid
      • getAllChars

        public String getAllChars()
        Return all characters used in generating password.
        Returns:
        characters used in generating password