Created
April 25, 2016 03:34
-
-
Save asaph/f2d5e2a1c706312838768abd4cb9c454 to your computer and use it in GitHub Desktop.
Generate a random Google Authenticator compatible 20 byte base32 encoded secret key
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 getRandomSecretKey() { | |
SecureRandom random = new SecureRandom(); | |
byte[] bytes = new byte[20]; | |
random.nextBytes(bytes); | |
Base32 base32 = new Base32(); | |
String secretKey = base32.encodeToString(bytes); | |
// make the secret key more human-readable by lower-casing and | |
// inserting spaces between each group of 4 characters | |
return secretKey.toLowerCase().replaceAll("(.{4})(?=.{4})", "$1 "); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment