Package com.bastiaanjansen.otp
Class TOTPGenerator
java.lang.Object
com.bastiaanjansen.otp.OTPGenerator
com.bastiaanjansen.otp.TOTPGenerator
public class TOTPGenerator extends OTPGenerator
Generates time-based one-time passwords
- Author:
- Bastiaan Jansen
- See Also:
OTPGenerator
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTOTPGenerator.Builder -
Field Summary
-
Constructor Summary
Constructors Constructor Description TOTPGenerator(int passwordLength, java.time.Duration period, HMACAlgorithm algorithm, byte[] secret)Constructs generator with custom password length, time interval and hashing algorithm -
Method Summary
Modifier and Type Method Description Stringgenerate()Generate a time-based one-time password for current time interval instantStringgenerate(long secondsPast1970)Generate a time-based one-time password for a specific time based on seconds past 1970Stringgenerate(java.time.Instant instant)Generate a time-based one-time password for a Java instantStringgenerate(Date date)Generate a time-based one-time password for a specific datejava.time.DurationgetPeriod()URIgetURI(String issuer)Create a OTPAuth URI for easy on-boarding with only an issuerURIgetURI(String issuer, String account)Create a OTPAuth URI for easy user on-boarding with an issuer and account namebooleanverify(String code)Checks whether a code is valid for a specific counterbooleanverify(String code, int delayWindow)Checks whether a code is valid for a specific counter taking a delay window into accountMethods inherited from class com.bastiaanjansen.otp.OTPGenerator
generateCode, getAlgorithm, getPasswordLength, getSecret, getURI, verify, verify
-
Constructor Details
-
TOTPGenerator
public TOTPGenerator(int passwordLength, java.time.Duration period, HMACAlgorithm algorithm, byte[] secret)Constructs generator with custom password length, time interval and hashing algorithm- Parameters:
passwordLength- number of digits for generated code in range 6...8period- time interval between new codesalgorithm- HMAC hash algorithm used to hash datasecret- used to generate hash
-
-
Method Details
-
generate
Generate a time-based one-time password for current time interval instant- Returns:
- generated TOTP code
- Throws:
IllegalStateException- when code could not be generated
-
generate
Generate a time-based one-time password for a Java instant- Parameters:
instant- an instant- Returns:
- generated TOTP code
- Throws:
IllegalStateException- when code could not be generated
-
generate
Generate a time-based one-time password for a specific date- Parameters:
date- specific date- Returns:
- generated TOTP code
- Throws:
IllegalStateException- when code could not be generated
-
generate
Generate a time-based one-time password for a specific time based on seconds past 1970- Parameters:
secondsPast1970- seconds past 1970- Returns:
- generated TOTP code
- Throws:
IllegalArgumentException- when code could not be generated
-
verify
Checks whether a code is valid for a specific counter- Parameters:
code- an OTP code- Returns:
- a boolean, true if code is valid, otherwise false
-
verify
Checks whether a code is valid for a specific counter taking a delay window into account- Parameters:
code- an OTP codedelayWindow- window in which a code can still be deemed valid- Returns:
- a boolean, true if code is valid, otherwise false
-
getPeriod
public java.time.Duration getPeriod() -
getURI
Create a OTPAuth URI for easy on-boarding with only an issuer- Parameters:
issuer- name- Returns:
- generated OTPAuth URI
- Throws:
URISyntaxException- when URI cannot be created
-
getURI
Create a OTPAuth URI for easy user on-boarding with an issuer and account name- Parameters:
issuer- nameaccount- name- Returns:
- generated OTPAuth URI
- Throws:
URISyntaxException- when URI cannot be created
-