Skip to content

Instantly share code, notes, and snippets.

@asaph
Created April 25, 2016 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asaph/f2d5e2a1c706312838768abd4cb9c454 to your computer and use it in GitHub Desktop.
Save asaph/f2d5e2a1c706312838768abd4cb9c454 to your computer and use it in GitHub Desktop.
Generate a random Google Authenticator compatible 20 byte base32 encoded secret key
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