Class OTP


  • public final class OTP
    extends java.lang.Object
    OTP (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 int BYTES  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static java.lang.String create​(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.String getURL​(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.String random​(java.lang.String characters, int length)
      Deprecated.
      static java.lang.String randomBase32​(int length)
      Generate a random string in Base32, with the specified length.
      static java.lang.String timeInHex()
      Deprecated.
      static java.lang.String timeInHex​(long timeInMillis)
      A quick method to get a Time rounded down to the nearest 30 seconds.
      static java.lang.String timeInHex​(long timeInMillis, int periodInSec)
      A method to get a Unix Time converted to Hexadecimal using a token period.
      protected static boolean validateParameters​(java.lang.String secret, java.lang.String base, int digits, Type type)
      Validate the parameters used for generating one-time passwords.
      static boolean verify​(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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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.IOException
        Deprecated.
        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.IOException
        A quick method to get a Time rounded down to the nearest 30 seconds.
        Parameters:
        timeInMillis - long (like System.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.IOException
        A method to get a Unix Time converted to Hexadecimal using a token period.
        Parameters:
        timeInMillis - long (like System.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.NoSuchAlgorithmException
        Create 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 invalid
        java.security.NoSuchAlgorithmException - when HMAC is not available on this jvm
        java.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.NoSuchAlgorithmException
        Returns 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 invalid
        java.security.NoSuchAlgorithmException - when HMAC is not available on this jvm
        java.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.IllegalArgumentException
        Validate 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.IllegalArgumentException
        Gets 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.HOTP
        issuer - Company or Domain name
        email - Username or Email address
        Returns:
        otpauth://...
        Throws:
        java.lang.IllegalArgumentException - when parameters are invalid