Class TimeBasedOneTimePasswordGenerator
java.lang.Object
com.eatthepath.otp.TimeBasedOneTimePasswordGenerator
Generates time-based one-time passwords (TOTP) as specified in RFC 6238.
TimeBasedOneTimePasswordGenerator instances are thread-safe and may be shared between threads. Note that
the generateOneTimePassword(SecretKey, Instant) method (and its relatives) are synchronized; in
multi-threaded applications that make heavy use of a shared TimeBasedOneTimePasswordGenerator instance,
synchronization may become a performance bottleneck. In that case, callers may benefit from using one
TimeBasedOneTimePasswordGenerator instance per thread (for example, with a ThreadLocal).
- Author:
- Jon Chambers
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DurationThe default time-step for a time-based one-time password generator (30 seconds).static final StringA string identifier for the HMAC-SHA1 algorithm; HMAC-SHA1 is the default algorithm for TOTP.static final StringA string identifier for the HMAC-SHA256 algorithm.static final StringA string identifier for the HMAC-SHA512 algorithm. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new time-based one-time password generator with a default time-step (30 seconds), password length (6 decimal digits), and HMAC algorithm ("HmacSHA1").TimeBasedOneTimePasswordGenerator(Duration timeStep) Constructs a new time-based one-time password generator with the given time-step and a default password length (6 decimal digits) and HMAC algorithm ("HmacSHA1").TimeBasedOneTimePasswordGenerator(Duration timeStep, int passwordLength) Constructs a new time-based one-time password generator with the given time-step and password length and a default HMAC algorithm ("HmacSHA1").TimeBasedOneTimePasswordGenerator(Duration timeStep, int passwordLength, String algorithm) Constructs a new time-based one-time password generator with the given time-step, password length, and HMAC algorithm. -
Method Summary
Modifier and TypeMethodDescriptionintgenerateOneTimePassword(SecretKey key, Instant timestamp) Generates a one-time password using the given key and timestamp.generateOneTimePasswordString(SecretKey key, Instant timestamp) Generates a one-time password using the given key and timestamp and formats it as a string with the system default locale.generateOneTimePasswordString(SecretKey key, Instant timestamp, Locale locale) Generates a one-time password using the given key and timestamp and formats it as a string with 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.Returns the time step used by this generator.booleanvalidateOneTimePassword(SecretKey key, Instant timestamp, int oneTimePassword) Checks whether a given one-time password matches the one-time password generated for the given key and timestamp.booleanvalidateOneTimePassword(SecretKey key, Instant timestamp, String oneTimePassword) Checks whether a given one-time password matches the one-time password generated for the given key and timestamp.
-
Field Details
-
DEFAULT_TIME_STEP
The default time-step for a time-based one-time password generator (30 seconds). -
TOTP_ALGORITHM_HMAC_SHA1
A string identifier for the HMAC-SHA1 algorithm; HMAC-SHA1 is the default algorithm for TOTP.- See Also:
-
TOTP_ALGORITHM_HMAC_SHA256
A string identifier for the HMAC-SHA256 algorithm.- See Also:
-
TOTP_ALGORITHM_HMAC_SHA512
A string identifier for the HMAC-SHA512 algorithm.- See Also:
-
-
Constructor Details
-
TimeBasedOneTimePasswordGenerator
public TimeBasedOneTimePasswordGenerator()Constructs a new time-based one-time password generator with a default time-step (30 seconds), password length (6 decimal digits), and HMAC algorithm ("HmacSHA1"). -
TimeBasedOneTimePasswordGenerator
-
TimeBasedOneTimePasswordGenerator
Constructs a new time-based one-time password generator with the given time-step and password length and a default HMAC algorithm ("HmacSHA1").- Parameters:
timeStep- the time-step for this generatorpasswordLength- the length, in decimal digits, of the one-time passwords to be generated; must be between 6 and 8, inclusive
-
TimeBasedOneTimePasswordGenerator
public TimeBasedOneTimePasswordGenerator(Duration timeStep, int passwordLength, String algorithm) throws NoSuchAlgorithmException Constructs a new time-based one-time password generator with the given time-step, password length, and HMAC algorithm.- Parameters:
timeStep- the time-step for this generatorpasswordLength- the length, in decimal digits, of the one-time passwords to be generated; must be between 6 and 8, inclusivealgorithm- the name of theMacalgorithm to use when generating passwords; TOTP allows for "HmacSHA1", "HmacSHA256", and "HmacSHA512"- Throws:
IllegalArgumentException- if the given algorithm is not an algorithm allowed by RFC 6238 (i.e. "HmacSHA1", "HmacSHA256", or "HmacSHA512")NoSuchAlgorithmException- if the given algorithm is "HmacSHA512" and the JVM does not support that algorithm; all JVMs are required to support "HmacSHA1" and "HmacSHA256", but are not required to support "HmacSHA512"- See Also:
-
-
Method Details
-
generateOneTimePassword
Generates a one-time password using the given key and timestamp.- Parameters:
key- the key to be used to generate the passwordtimestamp- the timestamp 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
public String generateOneTimePasswordString(SecretKey key, Instant timestamp) throws InvalidKeyException Generates a one-time password using the given key and timestamp and formats it as a string with the system default locale.- Parameters:
key- the key to be used to generate the passwordtimestamp- the timestamp 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, Instant timestamp, Locale locale) throws InvalidKeyException Generates a one-time password using the given key and timestamp and formats it as a string with the given locale.- Parameters:
key- the key to be used to generate the passwordtimestamp- the timestamp 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, Instant timestamp, String oneTimePassword) throws InvalidKeyException Checks whether a given one-time password matches the one-time password generated for the given key and timestamp. Note that this method simply checks equality of two one-time passwords; compensating for clock drift, throttling/rate-limiting, clock resynchronization, and so on are all beyond the scope of this method.- Parameters:
key- the key to be used to generate the passwordtimestamp- the timestamp 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 timestamp; 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 timestamp- Throws:
InvalidKeyException- if the given key is inappropriate for initializing theMacfor this generatorNullPointerException- if the given timestamp or one-time password isnull- See Also:
-
validateOneTimePassword
public boolean validateOneTimePassword(SecretKey key, Instant timestamp, int oneTimePassword) throws InvalidKeyException Checks whether a given one-time password matches the one-time password generated for the given key and timestamp. Note that this method simply checks equality of two one-time passwords; compensating for clock drift, throttling/rate-limiting, clock resynchronization, and so on are all beyond the scope of this method.- Parameters:
key- the key to be used to generate the passwordtimestamp- the timestamp 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 timestamp- Throws:
InvalidKeyException- if the given key is inappropriate for initializing theMacfor this generatorNullPointerException- if the given timestamp isnull- See Also:
-
getTimeStep
Returns the time step used by this generator.- Returns:
- the time step used by this generator
-
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
-