Package com.amdelamar.jotp
Class OTP
- java.lang.Object
-
- com.amdelamar.jotp.OTP
-
public final class OTP extends java.lang.ObjectOTP (One Time Password) utility in Java. To enable two-factor authentication (2FA) using HMAC-based) or Time-based algorithms.- Since:
- 1.0.0
- See Also:
- https://github.com/amdelamar/jotp
-
-
Field Summary
Fields Modifier and Type Field Description static intBYTES
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static java.lang.Stringcreate(java.lang.String secret, java.lang.String base, int digits, Type type)Create a one-time-password with the given key, base, digits, and OTP.Type.static java.lang.StringgetURL(java.lang.String secret, int digits, Type type, java.lang.String issuer, java.lang.String email)Gets the "otpauth://" URL for adding to 2FA compatible devices/apps.static java.lang.Stringrandom(java.lang.String characters, int length)Deprecated.static java.lang.StringrandomBase32(int length)Generate a random string in Base32, with the specified length.static java.lang.StringtimeInHex()Deprecated.static java.lang.StringtimeInHex(long timeInMillis)A quick method to get a Time rounded down to the nearest 30 seconds.static java.lang.StringtimeInHex(long timeInMillis, int periodInSec)A method to get a Unix Time converted to Hexadecimal using a token period.protected static booleanvalidateParameters(java.lang.String secret, java.lang.String base, int digits, Type type)Validate the parameters used for generating one-time passwords.static booleanverify(java.lang.String secret, java.lang.String base, java.lang.String code, int digits, Type type)Returns true if the code is valid for the Hmac-based or Time-based OTP of the secret.
-
-
-
Field Detail
-
BYTES
public static final int BYTES
- See Also:
- Constant Field Values
-
-
Method Detail
-
random
@Deprecated public static java.lang.String random(java.lang.String characters, int length)Deprecated.Generate a random string using the characters provided, with the specified length.- Parameters:
characters- A set of possible characters to be chosen.length- default 20- Returns:
- secure random string
-
randomBase32
public static java.lang.String randomBase32(int length)
Generate a random string in Base32, with the specified length.- Parameters:
length- default 20- Returns:
- secure random string
-
timeInHex
@Deprecated public static java.lang.String timeInHex() throws java.io.IOExceptionDeprecated.A quick method to get Unix Time rounded down to the nearest 30 seconds.- Returns:
- String Hex time
- Throws:
java.io.IOException- when generating Unix time
-
timeInHex
public static java.lang.String timeInHex(long timeInMillis) throws java.io.IOExceptionA quick method to get a Time rounded down to the nearest 30 seconds.- Parameters:
timeInMillis- long (likeSystem.currentTimeMillis())- Returns:
- String Hex time
- Throws:
java.io.IOException- when generating Unix time
-
timeInHex
public static java.lang.String timeInHex(long timeInMillis, int periodInSec) throws java.io.IOExceptionA method to get a Unix Time converted to Hexadecimal using a token period.- Parameters:
timeInMillis- long (likeSystem.currentTimeMillis())periodInSec- int seconds period for the time to be rounded down to- Returns:
- String Hex time
- Throws:
java.io.IOException
-
create
public static java.lang.String create(java.lang.String secret, java.lang.String base, int digits, Type type) throws java.lang.IllegalArgumentException, java.security.InvalidKeyException, java.security.NoSuchAlgorithmExceptionCreate a one-time-password with the given key, base, digits, and OTP.Type.- Parameters:
secret- The secret.base- The offset. (e.g. TOTP base is time from UTC rounded to the half-second while HOTP is a counter)digits- The length of the code (Commonly '6')type- Type.TOTP or Type.HOTP- Returns:
- code
- Throws:
java.lang.IllegalArgumentException- when parameters are invalidjava.security.NoSuchAlgorithmException- when HMAC is not available on this jvmjava.security.InvalidKeyException- when secret is invalid- See Also:
- https://tools.ietf.org/html/rfc4226, https://tools.ietf.org/html/rfc6238
-
verify
public static boolean verify(java.lang.String secret, java.lang.String base, java.lang.String code, int digits, Type type) throws java.lang.IllegalArgumentException, java.security.InvalidKeyException, java.security.NoSuchAlgorithmExceptionReturns true if the code is valid for the Hmac-based or Time-based OTP of the secret. For Hmac-based the 'base' is a counter, like 1,2,3. For Time-based the 'base' is Unix-time rounded down to the nearest 30 seconds.- Parameters:
secret- Shhhhh. (Base32)base- The base or counter.code- An OTP code to check.digits- Length of code (Commonly '6')type- Type.TOTP or Type.HOTP- Returns:
- true if valid
- Throws:
java.lang.IllegalArgumentException- when parameters are invalidjava.security.NoSuchAlgorithmException- when HMAC is not available on this jvmjava.security.InvalidKeyException- when secret is invalid- See Also:
- https://tools.ietf.org/html/rfc4226, https://tools.ietf.org/html/rfc6238
-
validateParameters
protected static boolean validateParameters(java.lang.String secret, java.lang.String base, int digits, Type type) throws java.lang.IllegalArgumentExceptionValidate the parameters used for generating one-time passwords.- Parameters:
secret- Shhhhh. (Base32)base- The base or counter.digits- Length of code (Commonly '6')type- Type.TOTP or Type.HOTP- Returns:
- true if parameters are valid
- Throws:
java.lang.IllegalArgumentException- when parameters are invalid
-
getURL
public static java.lang.String getURL(java.lang.String secret, int digits, Type type, java.lang.String issuer, java.lang.String email) throws java.lang.IllegalArgumentExceptionGets the "otpauth://" URL for adding to 2FA compatible devices/apps.- Parameters:
secret- Shhhhh. (Base32)digits- Length of code (Commonly '6')type- Type.TOTP or Type.HOTPissuer- Company or Domain nameemail- Username or Email address- Returns:
- otpauth://...
- Throws:
java.lang.IllegalArgumentException- when parameters are invalid
-
-