Package com.bastiaanjansen.otp
Class HOTPGenerator
- java.lang.Object
-
- com.bastiaanjansen.otp.HOTPGenerator
-
public final class HOTPGenerator extends Object
Generates counter-based one-time passwords- Author:
- Bastiaan Jansen
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classHOTPGenerator.Builder
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static HOTPGeneratorfromURI(URI uri)Build a TOTPGenerator from an OTPAuth URIStringgenerate(long counter)Generate a codeHMACAlgorithmgetAlgorithm()intgetPasswordLength()URIgetURI(int counter, String issuer)Create an OTPAuth URI for easy user on-boarding with only an issuerURIgetURI(int counter, String issuer, String account)Create an OTPAuth URI for easy user on-boarding with an issuer and account namebooleanverify(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 HOTPGeneratorwithDefaultValues(byte[] secret)Create a com.bastiaanjansen.otp.HOTPGenerator with default values
-
-
-
Method Detail
-
fromURI
public static HOTPGenerator fromURI(URI uri) throws URISyntaxException
Build a TOTPGenerator from an OTPAuth URI- Parameters:
uri- OTPAuth URI- Returns:
- HOTP
- Throws:
URISyntaxException- when URI cannot be parsed
-
withDefaultValues
public static HOTPGenerator withDefaultValues(byte[] secret)
Create a com.bastiaanjansen.otp.HOTPGenerator with default values- Parameters:
secret- used to generate hash- Returns:
- a com.bastiaanjansen.otp.HOTPGenerator with default values
-
getURI
public URI getURI(int counter, String issuer) throws URISyntaxException
Create an OTPAuth URI for easy user on-boarding with only an issuer- Parameters:
counter- of URIissuer- name for URI- Returns:
- OTPAuth URI
- Throws:
URISyntaxException- when URI cannot be created
-
getURI
public URI getURI(int counter, String issuer, String account) throws URISyntaxException
Create an OTPAuth URI for easy user on-boarding with an issuer and account name- Parameters:
counter- of URIissuer- name for URIaccount- name for URI- Returns:
- OTPAuth URI
- Throws:
URISyntaxException- when URI cannot be created
-
getPasswordLength
public int getPasswordLength()
-
getAlgorithm
public HMACAlgorithm getAlgorithm()
-
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
public 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
-
-