Created
April 25, 2016 03:40
-
-
Save asaph/c06635f7c3e419046e058fcc68b24a59 to your computer and use it in GitHub Desktop.
Calculate the 6-digit 2fa code based on base32 encoded secret key and the current time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String getTOTPCode(String secretKey) { | |
String normalizedBase32Key = secretKey.replace(" ", "").toUpperCase(); | |
Base32 base32 = new Base32(); | |
byte[] bytes = base32.decode(normalizedBase32Key); | |
String hexKey = Hex.encodeHexString(bytes); | |
long time = (System.currentTimeMillis() / 1000) / 30; | |
String hexTime = Long.toHexString(time); | |
return TOTP.generateTOTP(hexKey, hexTime, "6"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment