Package com.bastiaanjansen.otp
Class OneTimePasswordGenerator
java.lang.Object
com.bastiaanjansen.otp.OneTimePasswordGenerator
- Direct Known Subclasses:
HOTPGenerator,TOTPGenerator
public class OneTimePasswordGenerator extends Object
Generates one-time passwords
- Author:
- Bastiaan Jansen
-
Field Summary
Fields Modifier and Type Field Description protected HMACAlgorithmalgorithmHashing algorithm used to generate code, defaults to SHA1static HMACAlgorithmDEFAULT_HMAC_ALGORITHMDefault value for HMAC Algorithmstatic intDEFAULT_PASSWORD_LENGTHDefault value for password lengthprotected intpasswordLengthNumber of digits for generated code in range 6...8, defaults to 6protected StringsecretSecret key used to generate the code, this should be a base32 string -
Constructor Summary
Constructors Modifier Constructor Description protectedOneTimePasswordGenerator(int passwordLength, HMACAlgorithm algorithm, String secret)Constructs the generator with custom password length and hashing algorithmprotectedOneTimePasswordGenerator(int passwordLength, String secret)Constructs generator with a custom password length and default hashing algorithmprotectedOneTimePasswordGenerator(HMACAlgorithm algorithm, String secret)Constructs generator with a custom hashing algorithm and default password lengthprotectedOneTimePasswordGenerator(String secret)Constructs generator with default valuesprotectedOneTimePasswordGenerator(URI uri)Constructs generator from a OTPAuth URI -
Method Summary
Modifier and Type Method Description protected Stringgenerate(BigInteger counter)Generate a codeHMACAlgorithmgetAlgorithm()intgetPasswordLength()StringgetSecret()protected URIgetURI(String type, String path, Map<String,String> query)Generate an OTPAuth URIbooleanverify(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 account
-
Field Details
-
passwordLength
protected final int passwordLengthNumber of digits for generated code in range 6...8, defaults to 6 -
algorithm
Hashing algorithm used to generate code, defaults to SHA1 -
secret
Secret key used to generate the code, this should be a base32 string -
DEFAULT_PASSWORD_LENGTH
public static final int DEFAULT_PASSWORD_LENGTHDefault value for password length- See Also:
- Constant Field Values
-
DEFAULT_HMAC_ALGORITHM
Default value for HMAC Algorithm
-
-
Constructor Details
-
OneTimePasswordGenerator
Constructs generator with default values- Parameters:
secret- used to generate hash
-
OneTimePasswordGenerator
Constructs generator with a custom password length and default hashing algorithm- Parameters:
passwordLength- Number of digits for generated code in range 6...8secret- used to generate hash
-
OneTimePasswordGenerator
Constructs generator with a custom hashing algorithm and default password length- Parameters:
algorithm- HMAC hash algorithm used to hash datasecret- used to generate hash
-
OneTimePasswordGenerator
Constructs generator from a OTPAuth URI- Parameters:
uri- OTPAuth URI- Throws:
UnsupportedEncodingException- when URI query items can't be encoded
-
OneTimePasswordGenerator
Constructs the generator with custom password length and hashing algorithm- Parameters:
passwordLength- number of digits for generated code in range 6...8algorithm- HMAC hash algorithm used to hash datasecret- used to generate hash
-
-
Method Details
-
getPasswordLength
public int getPasswordLength() -
getAlgorithm
-
getSecret
-
verify
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
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
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 path, Map<String,String> query) throws URISyntaxExceptionGenerate an OTPAuth URI- Parameters:
type- of OTPAuth URI: totp or hotppath- contains issuer and account namequery- items of URI- Returns:
- created OTPAuth URI
- Throws:
URISyntaxException- when URI cannot be created
-