Package com.amdelamar.jotp.type
Class HOTP
- java.lang.Object
-
- com.amdelamar.jotp.type.HOTP
-
- All Implemented Interfaces:
OTPInterface
public class HOTP extends java.lang.Object implements OTPInterface
Hmac based OTP class implements OTPInterface- Since:
- 1.0.0
- See Also:
- https://tools.ietf.org/html/rfc4226
-
-
Constructor Summary
Constructors Constructor Description HOTP()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected static int
checksum(long num, int digits)
Calculates the checksum using the credit card algorithm.java.lang.String
create(java.lang.String secret, java.lang.String base, int digits)
Create a one-time-password with the given key, base, and digits.protected static java.lang.String
generateHotp(byte[] secret, long movingFactor, int digits, boolean addChecksum, int truncationOffset, java.lang.String crypto)
This method generates an OTP value for the given set of parameters.java.lang.String
getLabel()
-
-
-
Method Detail
-
getLabel
public java.lang.String getLabel()
- Specified by:
getLabel
in interfaceOTPInterface
-
create
public java.lang.String create(java.lang.String secret, java.lang.String base, int digits) throws java.security.InvalidKeyException, java.security.NoSuchAlgorithmException
Create a one-time-password with the given key, base, and digits.- Specified by:
create
in interfaceOTPInterface
- Parameters:
secret
- The secret. Shhhhhh!base
- The offset. (HOTP is a counter incremented by each use)digits
- The length of the code (Commonly '6')- Returns:
- code
- Throws:
java.security.NoSuchAlgorithmException
- when HMAC is not available on this jvmjava.security.InvalidKeyException
- when secret is invalid- See Also:
- https://tools.ietf.org/html/rfc4226
-
checksum
protected static int checksum(long num, int digits)
Calculates the checksum using the credit card algorithm. This algorithm has the advantage that it detects any single mistyped digit and any single transposition of adjacent digits.- Parameters:
num
- the number to calculate the checksum fordigits
- number of significant places in the number- Returns:
- the checksum of num
-
generateHotp
protected static java.lang.String generateHotp(byte[] secret, long movingFactor, int digits, boolean addChecksum, int truncationOffset, java.lang.String crypto) throws java.security.NoSuchAlgorithmException, java.security.InvalidKeyException
This method generates an OTP value for the given set of parameters.- Parameters:
secret
- Shhhhh.movingFactor
- the counter, time, or other valuedigits
- length of the codeaddChecksum
- a flag that indicates if the checksum digit should be appened to the OTPtruncationOffset
- the offset into the MAC result to begin truncation. If this value is out of the range of 0 ... 15, then dynamic truncation will be used. Dynamic truncation is when the last 4 bits of the last byte of the MAC are used to determine the start offset.crypto
- the crypto function to use- Returns:
- An OTP code.
- Throws:
java.security.NoSuchAlgorithmException
- if hmac sha1 is not availablejava.security.InvalidKeyException
- if given key is inappropriate for this mac
-
-