Class HmacOneTimePasswordGenerator
java.lang.Object
com.eatthepath.otp.HmacOneTimePasswordGenerator
Generates HMAC-based one-time passwords (HOTP) as specified in
RFC 4226.
HmacOneTimePasswordGenerator instances are
thread-safe and may be shared between threads.- Author:
- Jon Chambers
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default length, in decimal digits, for one-time passwords. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new HMAC-based one-time password (HOTP) generator using a default password length (6 digits).HmacOneTimePasswordGenerator(int passwordLength) Creates a new HMAC-based one-time password (HOTP) generator using the given password length. -
Method Summary
Modifier and TypeMethodDescriptionintgenerateOneTimePassword(SecretKey key, long counter) Generates a one-time password using the given key and counter value.generateOneTimePasswordString(SecretKey key, long counter) Generates a one-time password using the given key and counter value and formats it as a string using the system default locale.generateOneTimePasswordString(SecretKey key, long counter, Locale locale) Generates a one-time password using the given key and counter value and formats it as a string using the given locale.Returns the name of the HMAC algorithm used by this generator.intReturns the length, in decimal digits, of passwords produced by this generator.booleanvalidateOneTimePassword(SecretKey key, long counter, int oneTimePassword) Checks whether a given one-time password matches the one-time password generated for the given key and counter value.booleanvalidateOneTimePassword(SecretKey key, long counter, String oneTimePassword) Checks whether a given one-time password matches the one-time password generated for the given key and counter value.
-
Field Details
-
DEFAULT_PASSWORD_LENGTH
public static final int DEFAULT_PASSWORD_LENGTHThe default length, in decimal digits, for one-time passwords.- See Also:
-
-
Constructor Details
-
HmacOneTimePasswordGenerator
public HmacOneTimePasswordGenerator()Creates a new HMAC-based one-time password (HOTP) generator using a default password length (6 digits). -
HmacOneTimePasswordGenerator
public HmacOneTimePasswordGenerator(int passwordLength) Creates a new HMAC-based one-time password (HOTP) generator using the given password length.- Parameters:
passwordLength- the length, in decimal digits, of the one-time passwords to be generated; must be between 6 and 8, inclusive
-
-
Method Details
-
generateOneTimePassword
Generates a one-time password using the given key and counter value.- Parameters:
key- the key to be used to generate the passwordcounter- the counter value for which to generate the password- Returns:
- an integer representation of a one-time password; callers will need to format the password for display on their own
- Throws:
InvalidKeyException- if the given key is inappropriate for initializing theMacfor this generator
-
generateOneTimePasswordString
Generates a one-time password using the given key and counter value and formats it as a string using the system default locale.- Parameters:
key- the key to be used to generate the passwordcounter- the counter value for which to generate the password- Returns:
- a string representation of a one-time password
- Throws:
InvalidKeyException- if the given key is inappropriate for initializing theMacfor this generator- See Also:
-
generateOneTimePasswordString
public String generateOneTimePasswordString(SecretKey key, long counter, Locale locale) throws InvalidKeyException Generates a one-time password using the given key and counter value and formats it as a string using the given locale.- Parameters:
key- the key to be used to generate the passwordcounter- the counter value for which to generate the passwordlocale- the locale to apply during formatting- Returns:
- a string representation of a one-time password
- Throws:
InvalidKeyException- if the given key is inappropriate for initializing theMacfor this generator
-
validateOneTimePassword
public boolean validateOneTimePassword(SecretKey key, long counter, String oneTimePassword) throws InvalidKeyException Checks whether a given one-time password matches the one-time password generated for the given key and counter value. Note that this method simply checks equality of two one-time passwords; incrementing expected counter values, throttling/rate-limiting, counter resynchronization, and so on are all beyond the scope of this method.- Parameters:
key- the key to be used to generate the passwordcounter- the counter value for which to generate the passwordoneTimePassword- the user-provided one-time password to check against the generated one-time password- Returns:
trueif and only if the given one-time password matches the one-time password generated for the given key and counter value; one-time password strings match if they have the correct number of digits (seegetPasswordLength()), can be parsed as an integer, and that integer matches the one-time password generated for the given key and counter value- Throws:
InvalidKeyException- if the given key is inappropriate for initializing theMacfor this generatorNullPointerException- if the given one-time password isnull- See Also:
-
validateOneTimePassword
public boolean validateOneTimePassword(SecretKey key, long counter, int oneTimePassword) throws InvalidKeyException Checks whether a given one-time password matches the one-time password generated for the given key and counter value. Note that this method simply checks equality of two one-time passwords; incrementing expected counter values, throttling/rate-limiting, counter resynchronization, and so on are all beyond the scope of this method.- Parameters:
key- the key to be used to generate the passwordcounter- the counter value for which to generate the passwordoneTimePassword- the user-provided one-time password to check against the generated one-time password- Returns:
trueif and only if the given one-time password matches the one-time password generated for the given key and counter value- Throws:
InvalidKeyException- if the given key is inappropriate for initializing theMacfor this generator- See Also:
-
getPasswordLength
public int getPasswordLength()Returns the length, in decimal digits, of passwords produced by this generator.- Returns:
- the length, in decimal digits, of passwords produced by this generator
-
getAlgorithm
Returns the name of the HMAC algorithm used by this generator.- Returns:
- the name of the HMAC algorithm used by this generator
-