totp

package
v1.0.0-dev.18 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package totp computes and checks time-based one-time passwords on the standard library: RFC 6238 over RFC 4226's HOTP, fixed at HMAC-SHA-1, six digits and a 30-second step, since Google Authenticator ignores any other algorithm, length or period. It emits the Key Uri Format and never parses one. Replay protection is the caller's: Match returns the step it accepted, and the caller stores it and passes it back as after.

Index

Constants

View Source
const (
	SecretBytes = 20               // 160 bits: RFC 4226 §4 R6; NIST SP 800-63B-4 §3.1.4 asks for 112
	Digits      = 6                // RFC 4226 §5.3; the Key Uri Format's default
	Period      = 30 * time.Second // RFC 6238 §5.2's recommended X
	Skew        = 1                // steps accepted either side of now: RFC 6238 §5.2 and §6
)

The fixed parameters of every secret and code.

Variables

This section is empty.

Functions

func Code

func Code(secret []byte, step int64) string

Code is the HOTP value of RFC 4226 §5.3 for secret at counter step: HMAC-SHA-1, dynamic truncation, then mod 10^Digits, zero-padded.

func NewSecret

func NewSecret() []byte

NewSecret returns SecretBytes from crypto/rand.

func Step

func Step(t time.Time) int64

Step is RFC 6238 §4.2's T for t, with T0 = 0 and X = Period.

func Text

func Text(secret []byte) string

Text is secret in RFC 4648 §6 base32 without padding, the key a person types when they cannot scan the URI.

func URI

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

URI is the Key Uri Format's otpauth://totp/<issuer>:<account>?secret=… with issuer equal to the label prefix, algorithm SHA1, 6 digits and a 30-second period. A colon in either part becomes "_", since the format reads an encoded colon as the separator too.

Types

type Result

type Result int

Result is what Match found.

const (
	NoMatch Result = iota
	Matched
	Reused    // the code is a step at or below after, and no fresher step matched
	Malformed // not six ASCII digits after removing spaces; the caller refuses it before any count
)

The answers of Match.

func Match

func Match(secret []byte, raw string, now time.Time, after int64) (step int64, r Result)

Match removes ASCII spaces from raw, so "123 456" is the same code as "123456" for replay and for the count, then compares it with each step from Step(now)+Skew down to Step(now)-Skew by subtle.ConstantTimeCompare, stopping at the first match above after. It returns Matched with that step; else Reused when a step at or below after matches; else NoMatch. The step is meaningful only with Matched, and the caller stores it as the next after. Scanning from the newest step keeps a replay refused when two steps in the window share a code.

Jump to

Keyboard shortcuts

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