totp

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package totp implements time-based one-time passwords (RFC 6238, the algorithm every mainstream authenticator app speaks) plus the small amount of supporting logic 2FA needs: secret generation, an otpauth:// provisioning URI, and single-use recovery codes.

No third-party dependency: the algorithm is HMAC-SHA1 (crypto/hmac, crypto/sha1) over a big-endian 8-byte counter, base32-decoded key (encoding/base32), all stdlib. Adding a library for this would be adding a dependency for something the standard library already covers completely.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateCode

func GenerateCode(secretBase32 string, now time.Time) (string, error)

GenerateCode returns the 6-digit TOTP for secretBase32 at the step containing now, with no clock-skew tolerance (that's Validate's job, which calls this internally): the symmetric counterpart callers need wherever something other than a live authenticator app must produce a code, e.g. tests exercising the confirm/verify handlers end to end.

func GenerateRecoveryCode

func GenerateRecoveryCode() (string, error)

GenerateRecoveryCode returns one single-use fallback code: 80 random bits, base32-encoded, grouped into four hyphenated blocks of four characters for readability when typed by hand.

func GenerateSecret

func GenerateSecret() (string, error)

GenerateSecret returns a fresh random base32-encoded TOTP secret.

func NormalizeRecoveryCode

func NormalizeRecoveryCode(code string) string

NormalizeRecoveryCode strips whitespace and hyphens and uppercases a recovery code, so a user's re-typed or copy-pasted input matches regardless of formatting differences from GenerateRecoveryCode's own output.

func ProvisioningURI

func ProvisioningURI(secretBase32, accountName, issuer string) string

ProvisioningURI builds the otpauth:// key URI most authenticator apps accept either scanned as a QR code or pasted directly, per Google Authenticator's key URI format. issuer and accountName are shown to the user inside their authenticator app to distinguish this entry from others, and both belong in the label and as an issuer query param per that same format's own recommendation (some apps only read one or the other).

func Validate

func Validate(secretBase32, code string, now time.Time) bool

Validate reports whether code is a currently valid 6-digit TOTP for secretBase32 at now, tolerating +-skewSteps steps of clock drift. Returns false on any malformed input rather than an error: every caller treats "not a valid code right now" identically regardless of why.

Types

This section is empty.

Jump to

Keyboard shortcuts

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