otp

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package otp implements the one-time password schemes carried in otpauth:// URIs: TOTP (RFC 6238) and HOTP (RFC 4226).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Kind is totp or hotp.
	Kind Kind
	// Secret is the shared key, already base32-decoded.
	Secret []byte
	// Digits is the length of the generated code, normally 6.
	Digits int
	// Period is the TOTP time step.
	Period time.Duration
	// Counter is the HOTP counter.
	Counter uint64
	// Algorithm names the HMAC hash: SHA1, SHA256 or SHA512.
	Algorithm string
	// Issuer is the service the code belongs to, for display.
	Issuer string
	// Account is the account name, for display.
	Account string
}

Config is a parsed otpauth:// URI.

func Parse

func Parse(uri string) (*Config, error)

Parse interprets an otpauth:// URI.

func (*Config) Code

func (c *Config) Code(at time.Time) (string, error)

Code returns the one-time password for the given moment. For HOTP the time is ignored and the configured counter is used.

func (*Config) Expires

func (c *Config) Expires(at time.Time) time.Time

Expires returns when the current TOTP code stops being valid.

func (*Config) Next

func (c *Config) Next() *Config

Next returns a copy of the configuration with the HOTP counter advanced. Callers must store the result, or the code would repeat.

type Kind

type Kind string

Kind distinguishes time-based from counter-based one-time passwords.

const (
	// TOTP is the time-based scheme.
	TOTP Kind = "totp"
	// HOTP is the counter-based scheme.
	HOTP Kind = "hotp"
)

Supported OTP kinds.

Jump to

Keyboard shortcuts

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