Package com.bastiaanjansen.otp
Class TOTPGenerator
- java.lang.Object
-
- com.bastiaanjansen.otp.TOTPGenerator
-
public final class TOTPGenerator extends Object
Generates time-based one-time passwords- Author:
- Bastiaan Jansen
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTOTPGenerator.Builder
-
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 dateDurationdurationUntilNextTimeWindow()Calculates time until next time window will be reached and a new totp should be generatedstatic TOTPGeneratorfromURI(URI uri)Build a TOTPGenerator from an OTPAuth URIHMACAlgorithmgetAlgorithm()ClockgetClock()intgetPasswordLength()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 nameStringnow()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 accountstatic TOTPGeneratorwithDefaultValues(byte[] secret)
-
-
-
Method Detail
-
fromURI
public static TOTPGenerator 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 TOTPGenerator withDefaultValues(byte[] secret)
-
now
public String now() throws IllegalStateException
Generate a time-based one-time password for current time interval instant- 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- 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- 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- 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- 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- 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()
-
getClock
public Clock getClock()
-
getAlgorithm
public HMACAlgorithm getAlgorithm()
-
getPasswordLength
public int getPasswordLength()
-
getURI
public URI getURI(String issuer) throws URISyntaxException
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
public URI getURI(String issuer, String account) throws URISyntaxException
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
-
durationUntilNextTimeWindow
public Duration durationUntilNextTimeWindow()
Calculates time until next time window will be reached and a new totp should be generated- Returns:
- a duration object with duration until next time window
-
-