Skip to content
/ go-totp Public

Simple Go package to implement TOTP authentication functionality to the Go app. It supports ECDH as well.

License

Notifications You must be signed in to change notification settings

KEINOS/go-totp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go1.15+ Go Reference

go-totp

go-totp is a simple Go package to implement Timebased-One-Time-Password authentication functionality, a.k.a. TOTP, to the Go app.

Note This is a wrapper of the awesome github.com/pquerna/otp package to facilitate the use of TOTP.

Usage

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()

Statuses

UnitTests golangci-lint CodeQL-Analysis PlatformTests

codecov Go Report Card

Contributing

go1.15+ Go Reference Opened Issues PR

Any Pull-Request for improvement is welcome!

License, copyright and credits

About

Simple Go package to implement TOTP authentication functionality to the Go app. It supports ECDH as well.

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Languages