public class TimeBasedOneTimePasswordGenerator extends HmacOneTimePasswordGenerator
Generates time-based one-time passwords (TOTP) as specified in RFC 6238.
TimeBasedOneTimePasswordGenerator instances are thread-safe and may be shared and re-used across multiple
threads.
| Modifier and Type | Field and Description |
|---|---|
static String |
TOTP_ALGORITHM_HMAC_SHA1
A string identifier for the HMAC-SHA1 algorithm (required by HOTP and allowed by TOTP).
|
static String |
TOTP_ALGORITHM_HMAC_SHA256
A string identifier for the HMAC-SHA256 algorithm (allowed by TOTP).
|
static String |
TOTP_ALGORITHM_HMAC_SHA512
A string identifier for the HMAC-SHA512 algorithm (allowed by TOTP).
|
DEFAULT_PASSWORD_LENGTH, HOTP_HMAC_ALGORITHM| Constructor and 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(long timeStep,
TimeUnit timeStepUnit)
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(long timeStep,
TimeUnit timeStepUnit,
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(long timeStep,
TimeUnit timeStepUnit,
int passwordLength,
String algorithm)
Constructs a new time-based one-time password generator with the given time-step, password length, and HMAC
algorithm.
|
| Modifier and Type | Method and Description |
|---|---|
int |
generateOneTimePassword(Key key,
Date timestamp)
Generates a one-time password using the given key and timestamp.
|
long |
getTimeStep(TimeUnit timeUnit)
Returns the time step used by this generator.
|
generateOneTimePassword, getAlgorithm, getPasswordLengthpublic static final String TOTP_ALGORITHM_HMAC_SHA1
public static final String TOTP_ALGORITHM_HMAC_SHA256
public static final String TOTP_ALGORITHM_HMAC_SHA512
public TimeBasedOneTimePasswordGenerator()
throws NoSuchAlgorithmException
NoSuchAlgorithmException - if the underlying JRE doesn't support the
"HmacSHA1" algorithm, which should never
happen except in cases of serious misconfigurationpublic TimeBasedOneTimePasswordGenerator(long timeStep,
TimeUnit timeStepUnit)
throws NoSuchAlgorithmException
timeStep - the magnitude of the time-step for this generatortimeStepUnit - the units for the the given time stepNoSuchAlgorithmException - if the underlying JRE doesn't support the
"HmacSHA1" algorithm, which should never
happen except in cases of serious misconfigurationpublic TimeBasedOneTimePasswordGenerator(long timeStep,
TimeUnit timeStepUnit,
int passwordLength)
throws NoSuchAlgorithmException
timeStep - the magnitude of the time-step for this generatortimeStepUnit - the units for the the given time steppasswordLength - the length, in decimal digits, of the one-time passwords to be generated; must be between
6 and 8, inclusiveNoSuchAlgorithmException - if the underlying JRE doesn't support the
"HmacSHA1" algorithm, which should never
happen except in cases of serious misconfigurationpublic TimeBasedOneTimePasswordGenerator(long timeStep,
TimeUnit timeStepUnit,
int passwordLength,
String algorithm)
throws NoSuchAlgorithmException
timeStep - the magnitude of the time-step for this generatortimeStepUnit - the units for the the given time steppasswordLength - the length, in decimal digits, of the one-time passwords to be generated; must be between
6 and 8, inclusivealgorithm - the name of the Mac algorithm to use when generating passwords; TOTP allows
for "HmacSHA1",
"HmacSHA256", and
"HmacSHA512"NoSuchAlgorithmException - if the underlying JRE doesn't support the given algorithmTOTP_ALGORITHM_HMAC_SHA1,
TOTP_ALGORITHM_HMAC_SHA256,
TOTP_ALGORITHM_HMAC_SHA512public int generateOneTimePassword(Key key, Date timestamp) throws InvalidKeyException
key - a secret key to be used to generate the passwordtimestamp - the timestamp for which to generate the passwordInvalidKeyException - if the given key is inappropriate for initializing the Mac for this generatorpublic long getTimeStep(TimeUnit timeUnit)
timeUnit - the units of time in which to return the time stepCopyright © 2016–2018. All rights reserved.