Package com.eatthepath.otp
Class TimeBasedOneTimePasswordGenerator
- java.lang.Object
-
- com.eatthepath.otp.HmacOneTimePasswordGenerator
-
- com.eatthepath.otp.TimeBasedOneTimePasswordGenerator
-
public class TimeBasedOneTimePasswordGenerator extends HmacOneTimePasswordGenerator
Generates time-based one-time passwords (TOTP) as specified in RFC 6238.
TimeBasedOneTimePasswordGeneratorinstances are thread-safe and may be shared between threads.- Author:
- Jon Chambers
-
-
Field Summary
Fields Modifier and Type Field Description static DurationDEFAULT_TIME_STEPThe default time-step for a time-based one-time password generator (30 seconds).static StringTOTP_ALGORITHM_HMAC_SHA1A string identifier for the HMAC-SHA1 algorithm (required by HOTP and allowed by TOTP).static StringTOTP_ALGORITHM_HMAC_SHA256A string identifier for the HMAC-SHA256 algorithm (allowed by TOTP).static StringTOTP_ALGORITHM_HMAC_SHA512A string identifier for the HMAC-SHA512 algorithm (allowed by TOTP).-
Fields inherited from class com.eatthepath.otp.HmacOneTimePasswordGenerator
DEFAULT_PASSWORD_LENGTH, HOTP_HMAC_ALGORITHM
-
-
Constructor Summary
Constructors Constructor Description 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(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 Type Method Description intgenerateOneTimePassword(Key key, Instant timestamp)Generates a one-time password using the given key and timestamp.DurationgetTimeStep()Returns the time step used by this generator.-
Methods inherited from class com.eatthepath.otp.HmacOneTimePasswordGenerator
generateOneTimePassword, getAlgorithm, getPasswordLength
-
-
-
-
Field Detail
-
DEFAULT_TIME_STEP
public static final Duration DEFAULT_TIME_STEP
The default time-step for a time-based one-time password generator (30 seconds).
-
TOTP_ALGORITHM_HMAC_SHA1
public static final String TOTP_ALGORITHM_HMAC_SHA1
A string identifier for the HMAC-SHA1 algorithm (required by HOTP and allowed by TOTP). HMAC-SHA1 is the default algorithm for TOTP.- See Also:
- Constant Field Values
-
TOTP_ALGORITHM_HMAC_SHA256
public static final String TOTP_ALGORITHM_HMAC_SHA256
A string identifier for the HMAC-SHA256 algorithm (allowed by TOTP).- See Also:
- Constant Field Values
-
TOTP_ALGORITHM_HMAC_SHA512
public static final String TOTP_ALGORITHM_HMAC_SHA512
A string identifier for the HMAC-SHA512 algorithm (allowed by TOTP).- See Also:
- Constant Field Values
-
-
Constructor Detail
-
TimeBasedOneTimePasswordGenerator
public TimeBasedOneTimePasswordGenerator() throws NoSuchAlgorithmExceptionConstructs a new time-based one-time password generator with a default time-step (30 seconds), password length (6 decimal digits), and HMAC algorithm ("HmacSHA1").- Throws:
NoSuchAlgorithmException- if the underlying JRE doesn't support the "HmacSHA1" algorithm, which should never happen except in cases of serious misconfiguration
-
TimeBasedOneTimePasswordGenerator
public TimeBasedOneTimePasswordGenerator(Duration timeStep) throws NoSuchAlgorithmException
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- Throws:
NoSuchAlgorithmException- if the underlying JRE doesn't support the "HmacSHA1" algorithm, which should never happen except in cases of serious misconfiguration
-
TimeBasedOneTimePasswordGenerator
public TimeBasedOneTimePasswordGenerator(Duration timeStep, int passwordLength) throws NoSuchAlgorithmException
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- Throws:
NoSuchAlgorithmException- if the underlying JRE doesn't support the "HmacSHA1" algorithm, which should never happen except in cases of serious misconfiguration
-
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:
NoSuchAlgorithmException- if the underlying JRE doesn't support the given algorithm- See Also:
TOTP_ALGORITHM_HMAC_SHA1,TOTP_ALGORITHM_HMAC_SHA256,TOTP_ALGORITHM_HMAC_SHA512
-
-
Method Detail
-
generateOneTimePassword
public int generateOneTimePassword(Key key, Instant timestamp) throws InvalidKeyException
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
-
getTimeStep
public Duration getTimeStep()
Returns the time step used by this generator.- Returns:
- the time step used by this generator
-
-