Package rife.tools

Class TOTPUtils

java.lang.Object
rife.tools.TOTPUtils

public final class TOTPUtils extends Object
Utility class providing the necessary functions to build 2FA using a time-based OTP algorithm
Since:
1.0
  • Method Details

    • generateSecret

      public static String generateSecret()
      Generates a random secret
      Returns:
      secret as a UTF-8 encoded String
      Since:
      1.0
    • getCode

      public static String getCode(String secret)
      Generates the time-based code based on the given secret
      Parameters:
      secret - should be the UTF-8 encoded secret
      Returns:
      time-based code as a String to use for authentication or null if secret is null or an empty string
      Since:
      1.0
    • validateCode

      public static boolean validateCode(String secret, String inputCode)
      Code validation with a default of 1-step back, granting a 30-60 second window
      Parameters:
      secret - should be the UTF-8 encoded secret
      inputCode - should be the code input by the challenger
      Returns:
      true if inputCode is valid, false if invalid or if secret or inputCode are null
      Since:
      1.0
    • validateCode

      public static boolean validateCode(String secret, String inputCode, int stepsBack)
      Code validation where steps back can be customized to allow looser time-based authentication
      Parameters:
      secret - should be the UTF-8 encoded secret
      inputCode - should be the code input by the challenger
      stepsBack - number of steps (30 second increments) to look back during authentication
      Returns:
      true if inputCode is valid, @{code false} if invalid or if secret or inputCode are null
      Since:
      1.0
    • getUrl

      public static String getUrl(String secret, String issuer, String user)
      Generates a Google Authenticator-compatible URL Formatting based on the document found here Can be used for QR-code image scanning
      Parameters:
      secret - should be the UTF-8 encoded secret
      issuer - should represent the account associated with the authentication
      user - represents the user associated with the authentication
      Returns:
      the URL to be used as a String
      Since:
      1.0