totp

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: May 10, 2026 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package totp provides TOTP (RFC 6238) utilities for two-factor authentication.

Features:

  • 6-digit codes, 30-second window, base32 secrets
  • Recovery codes in XXXX-XXXX-XXXX format (SHA-256 hashed at rest)
  • Secret encryption at rest using AES-GCM

Security:

  • Adjacent window of +/-1 (~30s) absorbs clock drift
  • Recovery codes use a 32-char alphabet (no 0/O/1/I) for readability
  • All comparisons of hashes use constant-time comparison
  • No secrets are ever logged

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecryptSecret

func DecryptSecret(ciphertext string, key []byte) (string, error)

DecryptSecret decrypts a TOTP secret that was encrypted with EncryptSecret. The key must be exactly 32 bytes. Returns an error if the ciphertext is invalid, tampered with, or encrypted with a different key.

func EncryptSecret

func EncryptSecret(plaintext string, key []byte) (string, error)

EncryptSecret encrypts a TOTP secret for at-rest storage using AES-256-GCM. The key must be exactly 32 bytes. Returns a base64-encoded string containing the nonce prepended to the ciphertext.

func GenerateQRURI

func GenerateQRURI(secret, email, issuer string) string

GenerateQRURI builds an otpauth:// provisioning URI suitable for QR codes.

Format (RFC 6238 / Google Authenticator spec):

otpauth://totp/{issuer}:{email}?secret=...&issuer={issuer}&algorithm=SHA1&digits=6&period=30

func GenerateRecoveryCodes

func GenerateRecoveryCodes(n int) []string

GenerateRecoveryCodes returns n cryptographically-random recovery codes in XXXX-XXXX-XXXX format. Each code uses a 32-char alphabet for 60 bits of entropy, matching Google/Microsoft backup-code strength.

func GenerateSecret

func GenerateSecret() (string, error)

GenerateSecret returns a fresh base32 TOTP secret (160-bit / 32-char).

func HashRecoveryCode

func HashRecoveryCode(code string) string

HashRecoveryCode returns the SHA-256 hex digest of the canonicalized recovery code.

func VerifyCode

func VerifyCode(secret, code string) bool

VerifyCode verifies a 6-digit TOTP code against the secret. It checks the current time step plus +/-1 window (for clock drift).

func VerifyRecoveryCode

func VerifyRecoveryCode(code, hash string) bool

VerifyRecoveryCode checks a recovery code against a stored SHA-256 hash using constant-time comparison to prevent timing attacks.

Types

This section is empty.

Jump to

Keyboard shortcuts

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