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
+21.4k SSL : How to check if current certificate is sha1 or sha2
+7.6k Golang : Getting Echo framework StartAutoTLS to work
+19k Golang : Delete item from slice based on index/key position
+12.1k Golang : Validate email address
+27.2k Golang : dial tcp: too many colons in address
+9.2k Golang : Scramble and unscramble text message by randomly replacing words
+8.7k Golang : Sort lines of text example
+13.3k Golang : Strings comparison
+4.6k PHP : Extract part of a string starting from the middle
+19.7k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+7k Golang : Use modern ciphers only in secure connection
+51.1k Golang : Check if item is in slice/array