go-totp
is a simple Go package to implement Timebased-One-Time-Password authentication functionality, a.k.a. TOTP
, to the Go app.
- Compatible Authenticator apps | Wiki @ GitHub
Note This is a wrapper of the awesome
github.com/pquerna/otp
package to facilitate the use of TOTP.
go get "github.com/KEINOS/go-totp"
import (
"fmt"
"log"
"github.com/KEINOS/go-totp/totp"
)
func Example() {
// Generate a new secret key
Issuer := "Example.com"
AccountName := "[email protected]"
key, err := totp.GenerateKey(Issuer, AccountName)
if err != nil {
log.Fatal(err)
}
// Generate 6 digits passcode (valid for 30 seconds)
passcode, err := key.PassCode()
if err != nil {
log.Fatal(err)
}
// Validate the passcode
valid, err := key.Validate(passcode)
if err != nil {
log.Fatal(err)
}
if valid {
fmt.Println("Passcode is valid")
}
// Output: Passcode is valid
}
// --------------------------------------------------
// Basic methods of totp.Key object
// --------------------------------------------------
// Generate the current passcode. Which is a string of
// 6 digit numbers and valid for 30 seconds.
passcode, err := key.PassCode()
// Validate the received passcode.
ok := key.Validate(passcode)
// Get 100x100 px image of QR code as PNG byte data.
// FixLevelDefault is the 15% of error correction.
qrCodeObj, err := key.QRCode(totp.FixLevelDefault)
pngBytes, err := qrCodeObj.PNG(100, 100)
// Get the secret key in PEM format text.
pemKey, err := key.PEM()
// Get the secret key in TOTP URI format string.
uriKey := key.URI()
// Get the secret value in Base32 format string.
base32Key := key.Secret.Base32()
// Get the secret value in Base62 format string.
base62Key := key.Secret.Base62()
- View more examples and advanced usages @ pkg.go.dev
Any Pull-Request for improvement is welcome!
- Branch to PR:
main
- CONTRIBUTING.md
- CIs on PR/Push:
unit-tests
golangci-lint
codeQL-analysis
platform-tests
- Security policy
- MIT, Copyright (c) 2022 KEINOS and the go-totp contributors.
- This Go package relies heavily on support from the
github.com/pquerna/otp
package.