otp

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package otp implements Time-based One-Time Passwords (TOTP) per RFC 6238 using only the Go standard library. It is compatible with Google Authenticator, Authy, 1Password, and any other RFC 6238-compliant authenticator app.

Quick start:

// On account setup — generate a secret and show the provisioning URI as a QR code.
secret, _ := otp.NewSecret()
uri := otp.ProvisioningURI("alice@example.com", "MyApp", secret)
// Render uri as a QR code using any QR library.

// On login — verify the code from the user's authenticator app.
ok, err := otp.Verify(secret, userSuppliedCode)

Index

Constants

View Source
const (
	// DefaultDigits is the standard OTP code length.
	DefaultDigits = 6
	// DefaultPeriod is the standard TOTP time step in seconds.
	DefaultPeriod = 30
	// DefaultSkew is the number of adjacent time steps checked on either side
	// of the current step to account for clock drift.
	DefaultSkew = 1
	// SecretBytes is the recommended secret length (20 bytes → 160-bit secret).
	SecretBytes = 20
)

Variables

View Source
var ErrInvalidCode = errors.New("otp: invalid code")

ErrInvalidCode is returned when the supplied code does not match.

Functions

func Generate

func Generate(secret string) (string, error)

Generate returns the current TOTP code using the system clock.

func GenerateAt

func GenerateAt(secret string, t time.Time) (string, error)

GenerateAt returns the TOTP code for the given secret at time t, using the default 30-second period and 6-digit output.

func GenerateBackupCodes

func GenerateBackupCodes(n int) ([]string, error)

GenerateBackupCodes returns n single-use backup codes. Each code is a random 10-character alphanumeric string. Store the SHA-256 hashes of the returned codes (use crypto.HashPassword) and cross them off as they are used.

func NewSecret

func NewSecret() (string, error)

NewSecret generates a cryptographically secure, base32-encoded TOTP secret. Store this value (encrypted at rest) per user. Show it to the user during setup via ProvisioningURI.

func ProvisioningURI

func ProvisioningURI(account, issuer, secret string) string

ProvisioningURI returns an otpauth:// URI that encodes the TOTP parameters. Most authenticator apps accept this URI as a QR code payload.

uri := otp.ProvisioningURI("alice@example.com", "MyApp", secret)
// Pass uri to a QR code library (e.g. github.com/skip2/go-qrcode).

func Verify

func Verify(secret, code string) (bool, error)

Verify checks whether code is a valid TOTP for secret at the current time, allowing for the default clock drift tolerance (±1 step).

func VerifyWithOptions

func VerifyWithOptions(secret, code string, opts VerifyOptions) (bool, error)

VerifyWithOptions is like Verify but accepts custom parameters.

Types

type VerifyOptions

type VerifyOptions struct {
	// Digits is the expected code length (default: 6).
	Digits int
	// Period is the time step in seconds (default: 30).
	Period uint
	// Skew is the number of time steps to check on either side of the current
	// step (default: 1, meaning ±30 seconds tolerance with a 30-second period).
	Skew uint
}

VerifyOptions configures TOTP verification behaviour.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL