Class PasswordGenerator

java.lang.Object
org.passay.generate.PasswordGenerator

public class PasswordGenerator extends Object
Creates passwords that meet password character rule criteria.
  • Field Details

    • MAX_PASSWORD_LENGTH

      private static final int MAX_PASSWORD_LENGTH
      Maximum supported length of password generation.
      See Also:
    • DEFAULT_RETRY_LIMIT

      private static final int DEFAULT_RETRY_LIMIT
      Default retry limit.
      See Also:
    • random

      private final Random random
      Source of random data.
    • length

      private final int length
      Length of passwords to generate.
    • retryLimit

      private final int retryLimit
      Number of times to retry password generation before giving up.
    • passwordRules

      private final List<Rule> passwordRules
      Rules to determine password character appenders.
    • characterAppenders

      private final List<CharacterAppender> characterAppenders
      Character appenders derived from the rules.
    • retryCount

      private int retryCount
      Tracks the total number of retries.
  • Constructor Details

    • PasswordGenerator

      public PasswordGenerator(int length, Rule... rules)
      Creates a new password generator.
      Parameters:
      length - of the password to generate
      rules - to govern the content of the password
    • PasswordGenerator

      public PasswordGenerator(int length, List<? extends Rule> rules)
      Creates a new password generator.
      Parameters:
      length - of the password to generate
      rules - to govern the content of the password
    • PasswordGenerator

      public PasswordGenerator(int length, int retryLimit, Rule... rules)
      Creates a new password generator.
      Parameters:
      length - of the password to generate
      retryLimit - retry password generation at most this many times
      rules - to govern the content of the password
    • PasswordGenerator

      public PasswordGenerator(int length, int retryLimit, List<? extends Rule> rules)
      Creates a new password generator.
      Parameters:
      length - of the password to generate
      retryLimit - retry password generation at most this many times
      rules - to govern the content of the password
    • PasswordGenerator

      public PasswordGenerator(Random random, int length, int retryLimit, Rule... rules)
      Creates a new password generator.
      Parameters:
      random - for ordering of password characters
      length - of the password to generate
      retryLimit - retry password generation at most this many times
      rules - to govern the content of the password
    • PasswordGenerator

      public PasswordGenerator(Random random, int length, int retryLimit, List<? extends Rule> rules)
      Creates a new password generator.
      Parameters:
      random - for ordering of password characters
      length - of the password to generate
      retryLimit - retry password generation at most this many times
      rules - to govern the content of the password
  • Method Details

    • getRetryCount

      public int getRetryCount()
      Tracks the number retries. A password generator with a high percentage of retries may be indicative of a password ruleset that needs adjustment.
      Returns:
      Total number of retry attempts.
    • generate

      public UnicodeString generate()
      Generates a new password of the configured length which meets the requirements of the configured rules.
      Returns:
      generated password
    • getCharacterAppenders

      protected List<CharacterAppender> getCharacterAppenders(List<? extends Rule> rules, UnicodeString allowedChars, UnicodeString illegalChars, Random rand)
      Returns the list of character appenders used for password generation. The last element of this list is guaranteed to be FillRemainingCharactersAppender to ensure password generation satisfies the requested length.
      Parameters:
      rules - to derive password characters
      allowedChars - characters allowed
      illegalChars - characters not allowed
      rand - to randomize character selection
      Returns:
      unmodifiable list of character appenders
    • getAllowedCharacters

      private UnicodeString getAllowedCharacters(List<? extends Rule> rules)
      Returns the unique set of allowed characters as defined in the supplied list of AllowedCharacterRule. These are the characters that are common to every rule in the list.
      Parameters:
      rules - to extract unique characters from
      Returns:
      unicode string containing unique characters or empty string
    • getIllegalCharacters

      private UnicodeString getIllegalCharacters(List<? extends Rule> rules)
      Returns the unique set of illegal characters as defined in the supplied list of IllegalCharacterRule.
      Parameters:
      rules - to extract unique characters from
      Returns:
      unicode string containing unique characters or empty string
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • randomize

      protected void randomize(CharBuffer buffer)
      Randomizes the contents of the given buffer.
      Parameters:
      buffer - character buffer whose contents will be randomized.