Package com.eatthepath.otp
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(Key, 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 Duration
The default time-step for a time-based one-time password generator (30 seconds).static final String
A string identifier for the HMAC-SHA1 algorithm; HMAC-SHA1 is the default algorithm for TOTP.static final String
A string identifier for the HMAC-SHA256 algorithm.static final String
A 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 TypeMethodDescriptionint
generateOneTimePassword
(Key key, Instant timestamp) Generates a one-time password using the given key and timestamp.generateOneTimePasswordString
(Key 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
(Key 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.int
Returns the length, in decimal digits, of passwords produced by this generator.Returns the time step used by this generator.
-
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
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").- Parameters:
timeStep
- the time-step for this generator
-
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 UncheckedNoSuchAlgorithmException 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 theMac
algorithm to use when generating passwords; TOTP allows for "HmacSHA1", "HmacSHA256", and "HmacSHA512"- Throws:
UncheckedNoSuchAlgorithmException
- 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 theMac
for this generator
-
generateOneTimePasswordString
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 theMac
for this generator- See Also:
-
generateOneTimePasswordString
public String generateOneTimePasswordString(Key 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 theMac
for this generator
-
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
-