Package org.cryptacular.generator
Class TOTPGenerator
java.lang.Object
org.cryptacular.generator.AbstractOTPGenerator
org.cryptacular.generator.TOTPGenerator
OTP generator component that implements the TOTP scheme described in
RFC 6238.
- Author:
- Middleware Services
-
Field Summary
Fields inherited from class org.cryptacular.generator.AbstractOTPGenerator
DEFAULT_NUMBER_OF_DIGITS -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new TOTP generator that uses a SHA-1 digest.TOTPGenerator(int numberOfDigits) Creates a new TOTP generator that uses a SHA-1 digest.TOTPGenerator(Spec<org.bouncycastle.crypto.Digest> specification) Creates a new TOTP generator.TOTPGenerator(Spec<org.bouncycastle.crypto.Digest> specification, int numberOfDigits) Creates a new TOTP generator.TOTPGenerator(Spec<org.bouncycastle.crypto.Digest> specification, int numberOfDigits, Duration timeStep) Creates a new TOTP generator.TOTPGenerator(Spec<org.bouncycastle.crypto.Digest> specification, int numberOfDigits, Instant startTime) Creates a new TOTP generator.TOTPGenerator(Spec<org.bouncycastle.crypto.Digest> specification, int numberOfDigits, Instant startTime, Duration timeStep) Creates a new TOTP generator. -
Method Summary
Modifier and TypeMethodDescriptionprotected Instantintgenerate(byte[] key) Generates the OTP given a per-user key.protected org.bouncycastle.crypto.DigestSpec<org.bouncycastle.crypto.Digest>protected voidsetCurrentTime(Instant time) Sets the current time (supports testing).Methods inherited from class org.cryptacular.generator.AbstractOTPGenerator
generateInternal, getNumberOfDigits
-
Constructor Details
-
TOTPGenerator
public TOTPGenerator()Creates a new TOTP generator that uses a SHA-1 digest. -
TOTPGenerator
public TOTPGenerator(int numberOfDigits) Creates a new TOTP generator that uses a SHA-1 digest.- Parameters:
numberOfDigits- Number of digits in generated OTP. MUST be in the range 6 - 9.
-
TOTPGenerator
Creates a new TOTP generator.- Parameters:
specification- SHA-1, SHA-256, or SHA-512 digest specification.
-
TOTPGenerator
Creates a new TOTP generator.- Parameters:
specification- SHA-1, SHA-256, or SHA-512 digest specification.numberOfDigits- Number of digits in generated OTP. MUST be in the range 6 - 9.
-
TOTPGenerator
public TOTPGenerator(Spec<org.bouncycastle.crypto.Digest> specification, int numberOfDigits, Instant startTime) Creates a new TOTP generator.- Parameters:
specification- SHA-1, SHA-256, or SHA-512 digest specification.numberOfDigits- Number of digits in generated OTP. MUST be in the range 6 - 9. Default is 6.startTime- Start time instant. Input is truncated to seconds.
-
TOTPGenerator
public TOTPGenerator(Spec<org.bouncycastle.crypto.Digest> specification, int numberOfDigits, Duration timeStep) Creates a new TOTP generator.- Parameters:
specification- SHA-1, SHA-256, or SHA-512 digest specification.numberOfDigits- Number of digits in generated OTP. MUST be in the range 6 - 9. Default is 6.timeStep- Time step duration. Default is 30s. This value determines the validity window of generated OTP values.
-
TOTPGenerator
public TOTPGenerator(Spec<org.bouncycastle.crypto.Digest> specification, int numberOfDigits, Instant startTime, Duration timeStep) Creates a new TOTP generator.- Parameters:
specification- SHA-1, SHA-256, or SHA-512 digest specification.numberOfDigits- Number of digits in generated OTP. MUST be in the range 6 - 9. Default is 6.startTime- Start time instant. Input is truncated to seconds.timeStep- Time step duration. Default is 30s. This value determines the validity window of generated OTP values.
-
-
Method Details
-
getDigestSpecification
- Returns:
- Digest algorithm used with the HMAC function.
-
getStartTime
- Returns:
- Reference start time.
-
getTimeStep
- Returns:
- Time step in seconds.
-
generate
public int generate(byte[] key) Generates the OTP given a per-user key.- Parameters:
key- Per-user key.- Returns:
- Integer OTP.
-
getDigest
protected org.bouncycastle.crypto.Digest getDigest()- Specified by:
getDigestin classAbstractOTPGenerator- Returns:
- Digest algorithm used for HMAC operation.
-
setCurrentTime
Sets the current time (supports testing). This value is used if and only if it is a non-negative value; otherwise the current system time is used.- Parameters:
time- to override the current time with
-
currentTime
- Returns:
- Current system time in seconds since the start of epoch, 1970-01-01T00:00:00.
-