totp

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package totp implements the time-based one-time password of RFC 6238 over the HMAC one-time password of RFC 4226.

The package supports the single configuration that every authenticator application accepts: HMAC-SHA-1, six digits, and a thirty-second step.

SHA-1 is correct here. The HMAC construction does not inherit the collision weakness of the bare hash, and an authenticator application that reads a different algorithm from the enrolment URI can refuse it.

Index

Constants

View Source
const SecretBytes = 20

SecretBytes is the length of a new shared secret. RFC 4226 requires at least 128 bits and recommends 160 bits, which is the output length of SHA-1.

Variables

View Source
var ErrInvalidSecret = errors.New("authall/totp: the secret is not valid base32")

ErrInvalidSecret reports a secret that is not valid base32.

Functions

func DecodeSecret

func DecodeSecret(s string) ([]byte, error)

DecodeSecret returns the raw bytes of a base32 secret. It accepts a secret with padding and a secret in lower case, because a user can retype one.

func EncodeSecret

func EncodeSecret(secret []byte) string

EncodeSecret returns the base32 form of a secret. The database keeps this form, and the enrolment response shows it to the user.

func Generate

func Generate(secret []byte, t time.Time, p Params) string

Generate returns the code of one time step.

func NewSecret

func NewSecret() ([]byte, error)

NewSecret returns a new random shared secret.

func Step

func Step(t time.Time, p Params) int64

Step returns the time step counter of an instant.

func URI

func URI(secret []byte, issuer, account string, p Params) string

URI returns the otpauth URI of an enrolment. An authenticator application reads it from a QR code.

The label names the issuer and the account, so a person who holds two accounts of one application sees them apart.

func Validate

func Validate(secret []byte, input string, t time.Time, p Params) (int64, bool)

Validate reports whether a code authenticates at an instant. It returns the time step that matched, which the caller stores to refuse a replay of the same code inside its window.

Validate compares in constant time, so a wrong code discloses no digit.

Types

type Params

type Params struct {
	// Digits is the length of a code.
	Digits int
	// Period is the length of one time step.
	Period time.Duration
	// Skew is the number of steps that Validate accepts on each side of the
	// current step. A skew of one covers a clock difference of one period in
	// each direction.
	Skew int
}

Params are the parameters of one TOTP configuration.

func Default

func Default() Params

Default returns the parameters that every authenticator application accepts.

Jump to

Keyboard shortcuts

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