Package org.forgerock.openam.utils
Class RecoveryCodeGenerator
- java.lang.Object
-
- org.forgerock.openam.utils.RecoveryCodeGenerator
-
@SupportedAll public class RecoveryCodeGenerator extends Object
Generates codes of a specified length using a givenAlphabetas valid characters.
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_LENGTHDefault length of generated codes.static intDEFAULT_RETRIESDefault number of retries to use before resulting in error.
-
Constructor Summary
Constructors Constructor Description RecoveryCodeGenerator(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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StringgenerateCode(CodeGeneratorSource alphabet, int length)Generates a code of the supplied length, using the provided alphabet as its source of random characters.List<String>generateCodes(int numCodes, CodeGeneratorSource alphabet, boolean allowDuplicates)Generate a set of codes using the provided alphabet of the default length.List<String>generateCodes(int numCodes, CodeGeneratorSource alphabet, int length, boolean allowDuplicates)Generate a set of codes using the provided alphabet of the provided length.StringgenerateDelimitedCode(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.StringgenerateDelimitedCodeWithSpecifics(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 Detail
-
DEFAULT_LENGTH
public static final int DEFAULT_LENGTH
Default length of generated codes.- See Also:
- Constant Field Values
-
DEFAULT_RETRIES
public static final int DEFAULT_RETRIES
Default number of retries to use before resulting in error.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
RecoveryCodeGenerator
public RecoveryCodeGenerator(SecureRandom secureRandom, int retryMaximum)
Generates a new CodeUtils which can be used to generate a plethora of codes suited to fit your needs.- Parameters:
secureRandom- TheSecureRandominstance 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
@Inject public RecoveryCodeGenerator(SecureRandom secureRandom)
Generates a new CodeUtils which can be used to generate a plethora of codes suited to fit your needs. Uses theDEFAULT_RETRIESas its number of retries before failing.- Parameters:
secureRandom- TheSecureRandominstance to use when selecting characters from an alphabet set. Must not be null.
-
-
Method Detail
-
generateCode
public String generateCode(CodeGeneratorSource alphabet, int length)
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
public String generateDelimitedCode(CodeGeneratorSource alphabet, char delimiter, 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 adelimiterbetween 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 adelimiterbetween 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 adelimiterbetween 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 adelimiterbetween 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 adelimiterbetween 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 adelimiterbetween 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.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 adelimiterbetween 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 adelimiterbetween 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.
-
-