Package com.bastiaanjansen.otp
Class TOTP
- java.lang.Object
-
- com.bastiaanjansen.otp.TOTP
-
- All Implemented Interfaces:
TOTPGenerator,TOTPVerifier
public final class TOTP extends Object implements TOTPGenerator, TOTPVerifier
Generates time-based one-time passwords- Author:
- Bastiaan Jansen
- See Also:
OTP
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTOTP.Builder
-
Field Summary
Fields Modifier and Type Field Description protected HMACAlgorithmalgorithmHashing algorithm used to generate code, defaults to SHA1protected intpasswordLengthNumber of digits for generated code in range 6...8, defaults to 6protected byte[]secretSecret key used to generate the code, this should be a base32 string
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Stringat(long secondsPast1970)Generate a time-based one-time password for a specific time based on seconds past 1970Stringat(Instant instant)Generate a time-based one-time password for a Java instantStringat(Date date)Generate a time-based one-time password for a specific datestatic TOTPfromURI(URI uri)Build a TOTPGenerator from an OTPAuth URIprotected Stringgenerate(long counter)Generate a codeHMACAlgorithmgetAlgorithm()intgetPasswordLength()DurationgetPeriod()byte[]getSecret()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 nameprotected URIgetURI(String type, String issuer, String account, Map<String,String> query)Generate an OTPAuth URIStringnow()Generate a time-based one-time password for current time interval instantbooleanverify(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 accountbooleanverify(String code, long counter)Checks whether a code is valid for a specific counter with a delay window of 0booleanverify(String code, long counter, int delayWindow)Checks whether a code is valid for a specific counter taking a delay window into accountstatic TOTPwithDefaultValues(byte[] secret)Create a TOTPGenerator with default values
-
-
-
Field Detail
-
passwordLength
protected final int passwordLength
Number of digits for generated code in range 6...8, defaults to 6
-
algorithm
protected final HMACAlgorithm algorithm
Hashing algorithm used to generate code, defaults to SHA1
-
secret
protected final byte[] secret
Secret key used to generate the code, this should be a base32 string
-
-
Method Detail
-
fromURI
public static TOTP fromURI(URI uri) throws URISyntaxException
Build a TOTPGenerator from an OTPAuth URI- Parameters:
uri- OTPAuth URI- Returns:
- TOTP
- Throws:
URISyntaxException- when URI cannot be parsed
-
withDefaultValues
public static TOTP withDefaultValues(byte[] secret)
Create a TOTPGenerator with default values- Parameters:
secret- used to generate hash- Returns:
- a TOTPGenerator with default values
-
now
public String now() throws IllegalStateException
Generate a time-based one-time password for current time interval instant- Specified by:
nowin interfaceTOTPGenerator- Returns:
- generated TOTP code
- Throws:
IllegalStateException- when code could not be generated
-
at
public String at(Instant instant) throws IllegalStateException
Generate a time-based one-time password for a Java instant- Specified by:
atin interfaceTOTPGenerator- Parameters:
instant- an instant- Returns:
- generated TOTP code
- Throws:
IllegalStateException- when code could not be generated
-
at
public String at(Date date) throws IllegalStateException
Generate a time-based one-time password for a specific date- Specified by:
atin interfaceTOTPGenerator- Parameters:
date- specific date- Returns:
- generated TOTP code
- Throws:
IllegalStateException- when code could not be generated
-
at
public String at(long secondsPast1970) throws IllegalArgumentException
Generate a time-based one-time password for a specific time based on seconds past 1970- Specified by:
atin interfaceTOTPGenerator- Parameters:
secondsPast1970- seconds past 1970- Returns:
- generated TOTP code
- Throws:
IllegalArgumentException- when code could not be generated
-
verify
public boolean verify(String code)
Checks whether a code is valid for a specific counter- Specified by:
verifyin interfaceTOTPVerifier- Parameters:
code- an OTP code- Returns:
- a boolean, true if code is valid, otherwise false
-
verify
public boolean verify(String code, int delayWindow)
Checks whether a code is valid for a specific counter taking a delay window into account- Specified by:
verifyin interfaceTOTPVerifier- 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 Duration getPeriod()
-
getURI
public URI getURI(String issuer) throws URISyntaxException
Create a OTPAuth URI for easy on-boarding with only an issuer- Specified by:
getURIin interfaceTOTPGenerator- Parameters:
issuer- name- Returns:
- generated OTPAuth URI
- Throws:
URISyntaxException- when URI cannot be created
-
getURI
public URI getURI(String issuer, String account) throws URISyntaxException
Create a OTPAuth URI for easy user on-boarding with an issuer and account name- Specified by:
getURIin interfaceTOTPGenerator- Parameters:
issuer- nameaccount- name- Returns:
- generated OTPAuth URI
- Throws:
URISyntaxException- when URI cannot be created
-
getPasswordLength
public int getPasswordLength()
-
getAlgorithm
public HMACAlgorithm getAlgorithm()
-
getSecret
public byte[] getSecret()
-
verify
public boolean verify(String code, long counter)
Checks whether a code is valid for a specific counter with a delay window of 0- Parameters:
code- an OTP codecounter- how many times time interval has passed since 1970- Returns:
- a boolean, true if code is valid, otherwise false
-
verify
public boolean verify(String code, long counter, int delayWindow)
Checks whether a code is valid for a specific counter taking a delay window into account- Parameters:
code- an OTP codeecounter- how many times time interval has passed since 1970delayWindow- window in which a code can still be deemed valid- Returns:
- a boolean, true if code is valid, otherwise false
-
generate
protected String generate(long counter) throws IllegalStateException
Generate a code- Parameters:
counter- how many times time interval has passed since 1970- Returns:
- generated OTP code
- Throws:
IllegalStateException- when hashing algorithm throws an error
-
getURI
protected URI getURI(String type, String issuer, String account, Map<String,String> query) throws URISyntaxException
Generate an OTPAuth URI- Parameters:
type- of OTPAuth URI: totp or hotpissuer- name for URIaccount- name for URIquery- items of URI- Returns:
- created OTPAuth URI
- Throws:
URISyntaxException- when URI cannot be created
-
-