Class TOTPUtil
java.lang.Object
de.tomatengames.util.TOTPUtil
-
Method Summary
Modifier and TypeMethodDescriptionstatic intReturns the HOTP code resulting from the specified MAC algorithm (e.g.static intReturns the full-length HOTP code (10 digits) resulting from the specified MAC algorithm (e.g.static StringhotpString(Mac mac, long counter, int codeLength) static intmodulus(int codeLength) Returns the modulus to cut the last n (codeLength) digits from some integer.static StringtoString(int code, int codeLength) Returns the specified HOTP/TOTP code as a fixed-length integer string.static longtotpCounter(int codeSeconds, long time) Returns the HOTP counter to be used for the TOTP at the specified timestamp in milliseconds (e.g.
-
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
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 is10^nfor n in range[0, 9]. Ifn >= 10this will returnInteger.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
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
- 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
Returns the specified HOTP/TOTP code as a fixed-length integer string. Similar toInteger.toString(int)but prepends zeroes as needed and ignores additional digits. Ideal for putting inhotpFull(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.
-