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
+26.2k Golang : Convert file content into array of bytes
+5.9k Golang : Detect face in uploaded photo like GPlus
+7k Android Studio : How to detect camera, activate and capture example
+10k Golang : Generate random integer or float number
+28.8k Golang : missing Git command
+22.4k Golang : Calculate time different
+8.9k Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases
+5.4k Unix/Linux : How to test user agents blocked successfully ?
+4.4k Facebook : How to place save to Facebook button on your website
+7.9k Useful methods to access blocked websites
+10.1k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+34.5k Golang : Integer is between a range