Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
This is a supplement tutorial for the previous tutorial on how to generate QR codes with Golang and how to fix the error "Cannot interpret QR code"
To generate QR codes that is acceptable to Google Authenticator App. You need to follow the URI format specified in https://github.com/google/google-authenticator/wiki/Key-Uri-Format. Otherwise, the QR codes will not be interpreted correctly by Google Authenticator. Your app users will see the following error when attempt to scan the QR code.
Below is a code example on how to generate the proper QR codes for Google Authenticator :
package main
import (
"bytes"
"code.google.com/p/rsc/qr"
"crypto/rand"
"encoding/base32"
"fmt"
"github.com/disintegration/imaging"
"image"
"os"
"runtime"
)
func randStr(strSize int, randType string) string {
var dictionary string
if randType == "alphanum" {
dictionary = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
}
if randType == "alpha" {
dictionary = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
}
if randType == "number" {
dictionary = "0123456789"
}
var bytes = make([]byte, strSize)
rand.Read(bytes)
for k, v := range bytes {
bytes[k] = dictionary[v%byte(len(dictionary))]
}
return string(bytes)
}
func main() {
// maximize CPU usage for maximum performance
runtime.GOMAXPROCS(runtime.NumCPU())
// generate a random string - preferably 6 or 8 characters
randomStr := randStr(6, "alphanum")
fmt.Println(randomStr)
// For Google Authenticator purpose
// for more details see
// https://github.com/google/google-authenticator/wiki/Key-Uri-Format
secret := base32.StdEncoding.EncodeToString([]byte(randomStr))
// authentication link. Remember to replace SocketLoop with yours.
// for more details see
// https://github.com/google/google-authenticator/wiki/Key-Uri-Format
authLink := "otpauth://totp/SocketLoop?secret=" + secret + "&issuer=SocketLoop"
// Encode authLink to QR codes
// qr.H = 65% redundant level
// see https://godoc.org/code.google.com/p/rsc/qr#Level
code, err := qr.Encode(authLink, qr.H)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
imgByte := code.PNG()
// convert byte to image for saving to file
img, _, _ := image.Decode(bytes.NewReader(imgByte))
err = imaging.Save(img, "./QRImgGA.png")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// everything ok
fmt.Println("QR code generated and saved to QRimgGA.png")
}
Output ( tested with Google Authenticator app ) :
Hope this tutorial can be helpful to you!
References :
http://blog.gopheracademy.com/advent-2013/day-21-two-factor-auth/
https://github.com/google/google-authenticator/wiki/Key-Uri-Format
See also : Golang : How to generate QR codes?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.5k PHP : Get client IP address
+24.4k Golang : Create PDF file from HTML file
+5.5k Golang : Launching your executable inside a console under Linux
+22.4k Golang : Test file read write permission example
+6.7k Restart Apache or Nginx web server without password prompt
+6.8k Golang : How to iterate a slice without using for loop?
+14k Golang : How to check if your program is running in a terminal
+8.4k Golang : Executing and evaluating nested loop in html template
+22.6k Golang : Get ASCII code from a key press(cross-platform) example
+12.1k Golang : HTTP response JSON encoded data
+18.8k Golang : Check if directory exist and create if does not exist
+6.5k Fix sudo yum hang problem with no output or error messages