One time password (OTP) library for generate two-factor authentication (2FA) tokens with golang
The MIT License (MIT). Please see License File for more information.
apt install libqrencode-dev
go get github.com/ozgur-yalcin/otp.go
go get rsc.io/qr
package main
import (
"fmt"
"time"
otp "github.com/ozgur-yalcin/otp.go/src"
)
func main() {
secret := "XXXXXXXXXXXXXXXX"
fmt.Println("secret key:", secret)
totp := otp.NewDefaultTOTP(secret)
loop:
for {
var token string
fmt.Printf("enter the token (or q to quit):")
fmt.Scanln(&token)
switch token {
case "q":
break loop
default:
val := totp.Verify(token, int(time.Now().Unix()))
if !val {
fmt.Println("Not Authenticated")
continue
}
fmt.Println("Authenticated!")
}
}
}
package main
import (
"fmt"
"io/ioutil"
"time"
otp "github.com/ozgur-yalcin/otp.go/src"
qr "rsc.io/qr"
)
func main() {
secret := otp.RandomSecret(16)
fmt.Println("secret key:", secret)
totp := otp.NewDefaultTOTP(secret)
url := totp.ProvisioningUri("account", "title")
qrcode, err := qr.Encode(url, qr.Q)
if err != nil {
panic(err)
}
img := qrcode.PNG()
err = ioutil.WriteFile("qr.png", img, 0600)
if err != nil {
panic(err)
}
loop:
for {
var token string
fmt.Printf("enter the token (or q to quit):")
fmt.Scanln(&token)
switch token {
case "q":
break loop
default:
val := totp.Verify(token, int(time.Now().Unix()))
if !val {
fmt.Println("Not Authenticated")
continue
}
fmt.Println("Authenticated!")
}
}
}