Skip to content

Instantly share code, notes, and snippets.

@asaph
Created April 25, 2016 03:49
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/19291ed93730ac49601708f8657e244d to your computer and use it in GitHub Desktop.
Save asaph/19291ed93730ac49601708f8657e244d to your computer and use it in GitHub Desktop.
Create the data string for the Google Authenticator QR Code
public static String getGoogleAuthenticatorBarCode(String secretKey, String account, String issuer) {
String normalizedBase32Key = secretKey.replace(" ", "").toUpperCase();
try {
return "otpauth://totp/"
+ URLEncoder.encode(issuer + ":" + account, "UTF-8").replace("+", "%20")
+ "?secret=" + URLEncoder.encode(normalizedBase32Key, "UTF-8").replace("+", "%20")
+ "&issuer=" + URLEncoder.encode(issuer, "UTF-8").replace("+", "%20");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment