Package org.passay.generate
Class PasswordGenerator
java.lang.Object
org.passay.generate.PasswordGenerator
Creates passwords that meet password character rule criteria.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final List<CharacterAppender>Character appenders derived from the rules.private static final intDefault retry limit.private final intLength of passwords to generate.private static final intMaximum supported length of password generation.Rules to determine password character appenders.private final RandomSource of random data.private intTracks the total number of retries.private final intNumber of times to retry password generation before giving up. -
Constructor Summary
ConstructorsConstructorDescriptionPasswordGenerator(int length, int retryLimit, List<? extends Rule> rules) Creates a new password generator.PasswordGenerator(int length, int retryLimit, Rule... rules) Creates a new password generator.PasswordGenerator(int length, List<? extends Rule> rules) Creates a new password generator.PasswordGenerator(int length, Rule... rules) Creates a new password generator.PasswordGenerator(Random random, int length, int retryLimit, List<? extends Rule> rules) Creates a new password generator.PasswordGenerator(Random random, int length, int retryLimit, Rule... rules) Creates a new password generator. -
Method Summary
Modifier and TypeMethodDescriptiongenerate()Generates a new password of the configured length which meets the requirements of the configured rules.private UnicodeStringgetAllowedCharacters(List<? extends Rule> rules) Returns the unique set of allowed characters as defined in the supplied list ofAllowedCharacterRule.protected List<CharacterAppender>getCharacterAppenders(List<? extends Rule> rules, UnicodeString allowedChars, UnicodeString illegalChars, Random rand) Returns the list of character appenders used for password generation.private UnicodeStringgetIllegalCharacters(List<? extends Rule> rules) Returns the unique set of illegal characters as defined in the supplied list ofIllegalCharacterRule.intTracks the number retries.protected voidrandomize(CharBuffer buffer) Randomizes the contents of the given buffer.toString()
-
Field Details
-
MAX_PASSWORD_LENGTH
private static final int MAX_PASSWORD_LENGTHMaximum supported length of password generation.- See Also:
-
DEFAULT_RETRY_LIMIT
private static final int DEFAULT_RETRY_LIMITDefault retry limit.- See Also:
-
random
Source of random data. -
length
private final int lengthLength of passwords to generate. -
retryLimit
private final int retryLimitNumber of times to retry password generation before giving up. -
passwordRules
Rules to determine password character appenders. -
characterAppenders
Character appenders derived from the rules. -
retryCount
private int retryCountTracks the total number of retries.
-
-
Constructor Details
-
PasswordGenerator
Creates a new password generator.- Parameters:
length- of the password to generaterules- to govern the content of the password
-
PasswordGenerator
Creates a new password generator.- Parameters:
length- of the password to generaterules- to govern the content of the password
-
PasswordGenerator
Creates a new password generator.- Parameters:
length- of the password to generateretryLimit- retry password generation at most this many timesrules- to govern the content of the password
-
PasswordGenerator
Creates a new password generator.- Parameters:
length- of the password to generateretryLimit- retry password generation at most this many timesrules- to govern the content of the password
-
PasswordGenerator
Creates a new password generator.- Parameters:
random- for ordering of password characterslength- of the password to generateretryLimit- retry password generation at most this many timesrules- to govern the content of the password
-
PasswordGenerator
Creates a new password generator.- Parameters:
random- for ordering of password characterslength- of the password to generateretryLimit- retry password generation at most this many timesrules- 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
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 beFillRemainingCharactersAppenderto ensure password generation satisfies the requested length.- Parameters:
rules- to derive password charactersallowedChars- characters allowedillegalChars- characters not allowedrand- to randomize character selection- Returns:
- unmodifiable list of character appenders
-
getAllowedCharacters
Returns the unique set of allowed characters as defined in the supplied list ofAllowedCharacterRule. 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
Returns the unique set of illegal characters as defined in the supplied list ofIllegalCharacterRule.- Parameters:
rules- to extract unique characters from- Returns:
- unicode string containing unique characters or empty string
-
toString
-
randomize
Randomizes the contents of the given buffer.- Parameters:
buffer- character buffer whose contents will be randomized.
-