Package org.forgerock.openam.utils
Class RecoveryCodeGenerator
java.lang.Object
org.forgerock.openam.utils.RecoveryCodeGenerator
Generates codes of a specified length using a given
Alphabet
as valid characters.-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Default length of generated codes.static final int
Default number of retries to use before resulting in error. -
Constructor Summary
ConstructorDescriptionRecoveryCodeGenerator
(SecureRandom secureRandom) Generates a new CodeUtils which can be used to generate a plethora of codes suited to fit your needs.RecoveryCodeGenerator
(SecureRandom secureRandom, int retryMaximum) Generates a new CodeUtils which can be used to generate a plethora of codes suited to fit your needs. -
Method Summary
Modifier and TypeMethodDescriptiongenerateCode
(CodeGeneratorSource alphabet, int length) Generates a code of the supplied length, using the provided alphabet as its source of random characters.generateCodes
(int numCodes, CodeGeneratorSource alphabet, boolean allowDuplicates) Generate a set of codes using the provided alphabet of the default length.generateCodes
(int numCodes, CodeGeneratorSource alphabet, int length, boolean allowDuplicates) Generate a set of codes using the provided alphabet of the provided length.generateDelimitedCode
(CodeGeneratorSource alphabet, char delimiter, int... groups) Generates a code of the supplied length, using the provided alphabet as its source of random characters.String[]
generateDelimitedCodes
(int numCodes, CodeGeneratorSource alphabet, char delimiter, boolean allowDuplicates, int... groups) Generates a code of the supplied length, using the provided alphabet as its source of random characters.String[]
generateDelimitedCodesWithSpecifics
(int numCodes, CodeGeneratorSource alphabet, char delimiter, Map<Integer, Character> specifics, boolean allowDuplicates, int... groups) Generates a code of the supplied length, using the provided alphabet as its source of random characters.generateDelimitedCodeWithSpecifics
(CodeGeneratorSource alphabet, char delimiter, Map<Integer, Character> specifics, int... groups) Generates a code of the supplied length, using the provided alphabet as its source of random characters.
-
Field Details
-
DEFAULT_LENGTH
public static final int DEFAULT_LENGTHDefault length of generated codes.- See Also:
-
DEFAULT_RETRIES
public static final int DEFAULT_RETRIESDefault number of retries to use before resulting in error.- See Also:
-
-
Constructor Details
-
RecoveryCodeGenerator
Generates a new CodeUtils which can be used to generate a plethora of codes suited to fit your needs.- Parameters:
secureRandom
- TheSecureRandom
instance to use when selecting characters from an alphabet set. Must not be null.retryMaximum
- If duplicate codes are not allowed, this is the number of times we will attempt to generate another code upon discovering a collision.
-
RecoveryCodeGenerator
Generates a new CodeUtils which can be used to generate a plethora of codes suited to fit your needs. Uses theDEFAULT_RETRIES
as its number of retries before failing.- Parameters:
secureRandom
- TheSecureRandom
instance to use when selecting characters from an alphabet set. Must not be null.
-
-
Method Details
-
generateCode
Generates a code of the supplied length, using the provided alphabet as its source of random characters.- Parameters:
alphabet
- The alpha to use from which to pick characters. Must not be null.length
- The size of the produced codes. Must be greater than 0.- Returns:
- a randomly generated code.
-
generateDelimitedCode
Generates a code of the supplied length, using the provided alphabet as its source of random characters. The generated codes will have random characters for the size of each of the suppliedgroups
, with adelimiter
between them.- Parameters:
alphabet
- The alpha to use from which to pick characters. Must not be null.groups
- The groupings of characters. For example,{4, 4}
will produce two sets of four characters with adelimiter
between them. Must not be null. Must have at least one entry. Entries must be greater than 0.delimiter
- The character to use between each of the groups.- Returns:
- a randomly generated, delimited code.
-
generateDelimitedCodeWithSpecifics
public String generateDelimitedCodeWithSpecifics(CodeGeneratorSource alphabet, char delimiter, Map<Integer, Character> specifics, int... groups) Generates a code of the supplied length, using the provided alphabet as its source of random characters. The generated codes will have random characters for the size of each of the suppliedgroups
, with adelimiter
between them. The provided specifics map can then be used to replace individual character indexes with a specific character.- Parameters:
alphabet
- The alpha to use from which to pick characters. Must not be null.groups
- The groupings of characters. For example,{4, 4}
will produce two sets of four characters with adelimiter
between them. Must not be null. Must have at least one entry. Entries must be greater than 0.delimiter
- The character to use between each of the groups. Must not be null.specifics
- A map used to specify characters to use at a given location.- Returns:
- a randomly generated, delimited code with specific characters at given indexes.
-
generateCodes
public List<String> generateCodes(int numCodes, CodeGeneratorSource alphabet, boolean allowDuplicates) throws org.forgerock.openam.utils.CodeException Generate a set of codes using the provided alphabet of the default length.- Parameters:
numCodes
- The number of codes to generate. Must be greater than 0.alphabet
- The alphabet to use from which to select characters. Must not be null.allowDuplicates
- Whether to allow duplicates in the result set.- Returns:
- A set of recovery codes.
- Throws:
org.forgerock.openam.utils.CodeException
- if duplicate codes were produced, disallowed and the number of retries was exceeded.
-
generateCodes
public List<String> generateCodes(int numCodes, CodeGeneratorSource alphabet, int length, boolean allowDuplicates) throws org.forgerock.openam.utils.CodeException Generate a set of codes using the provided alphabet of the provided length.- Parameters:
numCodes
- Number of recovery codes to generate. Must be greater than 0.alphabet
- The alphabet to use from which to select characters. Must not be null.length
- The length of produced codes. Must be greater than 0.allowDuplicates
- Whether or not to allow duplicates in the result set.- Returns:
- a String array of randomly generated recovery codes, of size numSize.
- Throws:
org.forgerock.openam.utils.CodeException
- if duplicate codes were produced, disallowed and the number of retries was exceeded.
-
generateDelimitedCodes
public String[] generateDelimitedCodes(int numCodes, CodeGeneratorSource alphabet, char delimiter, boolean allowDuplicates, int... groups) throws org.forgerock.openam.utils.CodeException Generates a code of the supplied length, using the provided alphabet as its source of random characters. The generated codes will have random characters for the size of each of the suppliedgroups
, with adelimiter
between them.- Parameters:
numCodes
- Number of recovery codes to generate. Must be greater than 0.alphabet
- The alphabet to use from which to select numbers. Must not be null.groups
- The groupings of lengths of characters. For example,{4, 4}
will produce two set of four characters with adelimiter
between them. Must not be null. Must have at least one entry. Entries must be greater than 0.delimiter
- The character to use between each of the groups.allowDuplicates
- Whether or not to allow duplicates in the result set.- Returns:
- a String array of randomly generated recovery codes, of size numCodes.
- Throws:
org.forgerock.openam.utils.CodeException
- if duplicate codes were produced, disallowed and the number of retries was exceeded.
-
generateDelimitedCodesWithSpecifics
public String[] generateDelimitedCodesWithSpecifics(int numCodes, CodeGeneratorSource alphabet, char delimiter, Map<Integer, Character> specifics, boolean allowDuplicates, int... groups) throws org.forgerock.openam.utils.CodeExceptionGenerates a code of the supplied length, using the provided alphabet as its source of random characters. The generated codes will have random characters for the size of each of the suppliedgroups
, with adelimiter
between them. The provided specifics map can then be used to replace individual character indexes with a specific character.- Parameters:
numCodes
- The number of codes to generate. Must be greater than 0.alphabet
- The alphabet to use from which to pick characters. Must not be null.groups
- The groupings of lengths of characters. For example,{4, 4}
will produce two sets of four characters with adelimiter
between them. Must not be null. Must have at least one entry. Entries must be greater than 0.delimiter
- The character to use between each of the groups.specifics
- A map used to specify characters to use at a given location.allowDuplicates
- Whether to allow duplicate codes.- Returns:
- a String array of randomly generated recovery codes, of size numCodes.
- Throws:
org.forgerock.openam.utils.CodeException
- if duplicate codes were produced, disallowed and the number of retries was exceeded.
-