Class TOTPUtil

java.lang.Object
de.tomatengames.util.TOTPUtil

public class TOTPUtil extends Object
Provides methods to calculate HOTP and TOTP codes.
Since:
1.3
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    hotp(Mac mac, long counter, int codeLength)
    Returns the HOTP code resulting from the specified MAC algorithm (e.g.
    static int
    hotpFull(Mac mac, long counter)
    Returns the full-length HOTP code (10 digits) resulting from the specified MAC algorithm (e.g.
    static String
    hotpString(Mac mac, long counter, int codeLength)
    static int
    modulus(int codeLength)
    Returns the modulus to cut the last n (codeLength) digits from some integer.
    static String
    toString(int code, int codeLength)
    Returns the specified HOTP/TOTP code as a fixed-length integer string.
    static long
    totpCounter(int codeSeconds, long time)
    Returns the HOTP counter to be used for the TOTP at the specified timestamp in milliseconds (e.g.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • totpCounter

      public static long totpCounter(int codeSeconds, long time)
      Returns the HOTP counter to be used for the TOTP at the specified timestamp in milliseconds (e.g. System.currentTimeMillis()) using the specified amount of seconds a code should be valid (e.g. 30).
      Parameters:
      codeSeconds - The amount of seconds a code should be valid.
      time - The timestamp.
      Returns:
      The HOTP counter value.
    • hotpFull

      public static int hotpFull(Mac mac, long counter)
      Returns the full-length HOTP code (10 digits) resulting from the specified MAC algorithm (e.g. HashUtil.getHmacSHA1(byte[])) and the specified HOTP counter.
      This is supposed to be faster as the code is simply not cut to a lower amount of digits at this point.
      Parameters:
      mac - The MAC algorithm.
      counter - The HOTP counter.
      Returns:
      The 10-digit HOTP code.
    • modulus

      public static int modulus(int codeLength)
      Returns the modulus to cut the last n (codeLength) digits from some integer. The digits are supposed to be obtained by that integer % modulus. The return value is 10^n for n in range [0, 9]. If n >= 10 this will return Integer.MIN_VALUE, which, used as a modulus, results in the original integer for any non-negative input integer.
      Parameters:
      codeLength - The amount of digits to cut (n).
      Returns:
      The modulus.
    • hotp

      public static int hotp(Mac mac, long counter, int codeLength)
      Returns the HOTP code resulting from the specified MAC algorithm (e.g. HashUtil.getHmacSHA1(byte[])), the specified HOTP counter and the specified code length.
      Parameters:
      mac - The MAC algorithm.
      counter - The HOTP counter.
      codeLength - The code length.
      Returns:
      The HOTP code.
    • hotpString

      public static String hotpString(Mac mac, long counter, int codeLength)
      Parameters:
      mac - The MAC algorithm.
      counter - The HOTP counter.
      codeLength - The HOTP code's length.
      Returns:
      The HOTP code as a string. Not null.
    • toString

      public static String toString(int code, int codeLength)
      Returns the specified HOTP/TOTP code as a fixed-length integer string. Similar to Integer.toString(int) but prepends zeroes as needed and ignores additional digits. Ideal for putting in hotpFull(Mac, long) along with the desired code length. This does not insert any spaces.
      Parameters:
      code - The code.
      codeLength - The output code length.
      Returns:
      The code as a string. Not null.