Package com.eatthepath.otp
Class HmacOneTimePasswordGenerator
- java.lang.Object
-
- com.eatthepath.otp.HmacOneTimePasswordGenerator
-
- Direct Known Subclasses:
TimeBasedOneTimePasswordGenerator
public class HmacOneTimePasswordGenerator extends Object
Generates HMAC-based one-time passwords (HOTP) as specified in RFC 4226.
HmacOneTimePasswordGeneratorinstances are thread-safe and may be shared between threads.- Author:
- Jon Chambers
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_PASSWORD_LENGTHThe default length, in decimal digits, for one-time passwords.static StringHOTP_HMAC_ALGORITHMThe HMAC algorithm specified by the HOTP standard.
-
Constructor Summary
Constructors Constructor Description HmacOneTimePasswordGenerator()Creates a new HMAC-based one-time password (HOTP) generator using a default password length (6 digits).HmacOneTimePasswordGenerator(int passwordLength)Creates a new HMAC-based one-time password (HOTP) generator using the given password length.
-
Method Summary
Modifier and Type Method Description intgenerateOneTimePassword(Key key, long counter)Generates a one-time password using the given key and counter value.StringgetAlgorithm()Returns the name of the HMAC algorithm used by this generator.intgetPasswordLength()Returns the length, in decimal digits, of passwords produced by this generator.
-
-
-
Field Detail
-
DEFAULT_PASSWORD_LENGTH
public static final int DEFAULT_PASSWORD_LENGTH
The default length, in decimal digits, for one-time passwords.- See Also:
- Constant Field Values
-
HOTP_HMAC_ALGORITHM
public static final String HOTP_HMAC_ALGORITHM
The HMAC algorithm specified by the HOTP standard.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
HmacOneTimePasswordGenerator
public HmacOneTimePasswordGenerator() throws NoSuchAlgorithmExceptionCreates a new HMAC-based one-time password (HOTP) generator using a default password length (6 digits).- Throws:
NoSuchAlgorithmException- if the underlying JRE doesn't support the "HmacSHA1" algorithm, which should never happen except in cases of serious misconfiguration
-
HmacOneTimePasswordGenerator
public HmacOneTimePasswordGenerator(int passwordLength) throws NoSuchAlgorithmExceptionCreates a new HMAC-based one-time password (HOTP) generator using the given password length.- Parameters:
passwordLength- the length, in decimal digits, of the one-time passwords to be generated; must be between 6 and 8, inclusive- Throws:
NoSuchAlgorithmException- if the underlying JRE doesn't support the "HmacSHA1" algorithm, which should never happen except in cases of serious misconfiguration
-
-
Method Detail
-
generateOneTimePassword
public int generateOneTimePassword(Key key, long counter) throws InvalidKeyException
Generates a one-time password using the given key and counter value.- Parameters:
key- the key to be used to generate the passwordcounter- the counter value for which to generate the password- Returns:
- an integer representation of a one-time password; callers will need to format the password for display on their own
- Throws:
InvalidKeyException- if the given key is inappropriate for initializing theMacfor this generator
-
getPasswordLength
public int getPasswordLength()
Returns the length, in decimal digits, of passwords produced by this generator.- Returns:
- the length, in decimal digits, of passwords produced by this generator
-
getAlgorithm
public String getAlgorithm()
Returns the name of the HMAC algorithm used by this generator.- Returns:
- the name of the HMAC algorithm used by this generator
-
-