otp: github.com/pquerna/otp Index | Files | Directories

package otp

import "github.com/pquerna/otp"

Package otp implements both HOTP and TOTP based one time passcodes in a Google Authenticator compatible manner.

When adding a TOTP for a user, you must store the "secret" value persistently. It is recommend to store the secret in an encrypted field in your datastore. Due to how TOTP works, it is not possible to store a hash for the secret value like you would a password.

To enroll a user, you must first generate an OTP for them. Google Authenticator supports using a QR code as an enrollment method:

import (


key, err := totp.Generate(totp.GenerateOpts{
		Issuer: "Example.com",
		AccountName: "alice@example.com",

// Convert TOTP key into a QR code encoded as a PNG image.
var buf bytes.Buffer
img, err := key.Image(200, 200)
png.Encode(&buf, img)

// display the QR code to the user.

// Now Validate that the user's successfully added the passcode.
passcode := promptForPasscode()
valid := totp.Validate(passcode, key.Secret())

if valid {
	// User successfully used their TOTP, save it to your backend!
	storeSecret("alice@example.com", key.Secret())

Validating a TOTP passcode is very easy, just prompt the user for a passcode and retrieve the associated user's previously stored secret.

import "github.com/pquerna/otp/totp"

passcode := promptForPasscode()
secret := getSecret("alice@example.com")

valid := totp.Validate(passcode, secret)

if valid {
	// Success! continue login process.


Package Files

doc.go otp.go


var ErrGenerateMissingAccountName = errors.New("AccountName must be set")

When generating a Key, the Account Name must be set.

var ErrGenerateMissingIssuer = errors.New("Issuer must be set")

When generating a Key, the Issuer must be set.

var ErrValidateInputInvalidLength = errors.New("Input length unexpected")

The user provided passcode length was not expected.

var ErrValidateSecretInvalidBase32 = errors.New("Decoding of secret as base32 failed.")

Error when attempting to convert the secret from base32 to raw bytes.

type Algorithm Uses

type Algorithm int

Algorithm represents the hashing function to use in the HMAC operation needed for OTPs.

const (
    AlgorithmSHA1 Algorithm = iota

func (Algorithm) Hash Uses

func (a Algorithm) Hash() hash.Hash

func (Algorithm) String Uses

func (a Algorithm) String() string

type Digits Uses

type Digits int

Digits represents the number of digits present in the user's OTP passcode. Six and Eight are the most common values.

const (
    DigitsSix   Digits = 6
    DigitsEight Digits = 8

func (Digits) Format Uses

func (d Digits) Format(in int32) string

Format converts an integer into the zero-filled size for this Digits.

func (Digits) Length Uses

func (d Digits) Length() int

Length returns the number of characters for this Digits.

func (Digits) String Uses

func (d Digits) String() string

type Key Uses

type Key struct {
    // contains filtered or unexported fields

Key represents an TOTP or HTOP key.

func NewKeyFromURL Uses

func NewKeyFromURL(orig string) (*Key, error)

NewKeyFromURL creates a new Key from an TOTP or HOTP url.

The URL format is documented here:


func (*Key) AccountName Uses

func (k *Key) AccountName() string

AccountName returns the name of the user's account.

func (*Key) Image Uses

func (k *Key) Image(width int, height int) (image.Image, error)

Image returns an QR-Code image of the specified width and height, suitable for use by many clients like Google-Authenricator to enroll a user's TOTP/HOTP key.

func (*Key) Issuer Uses

func (k *Key) Issuer() string

Issuer returns the name of the issuing organization.

func (*Key) Secret Uses

func (k *Key) Secret() string

Secret returns the opaque secret for this Key.

func (*Key) String Uses

func (k *Key) String() string

func (*Key) Type Uses

func (k *Key) Type() string

Type returns "hotp" or "totp".

func (*Key) URL Uses

func (k *Key) URL() string

URL returns the OTP URL as a string



Package otp imports 12 packages (graph) and is imported by 82 packages. Updated 2019-06-01. Refresh now. Tools for package owners.