hotp

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(opts GenerateOpts) (*otp.Key, error)

Generate creates a new HOTP Key.

func GenerateCode

func GenerateCode(secret string, counter uint64) (string, error)

GenerateCode creates a HOTP passcode given a counter and secret. This is a shortcut for GenerateCodeCustom, with parameters that are compataible with Google-Authenticator.

func GenerateCodeCustom

func GenerateCodeCustom(
	secret string,
	counter uint64,
	opts ValidateOpts,
) (string, error)

GenerateCodeCustom uses a counter and secret value and options struct to create a passcode.

func GenerateCodeCustomRaw

func GenerateCodeCustomRaw(secret []byte, counter uint64, opts ValidateOpts) (string, error)

GenerateCodeCustomRaw generates a passcode from pre-decoded secret bytes. This avoids repeated base32 decoding when validating against multiple counters (e.g. TOTP with skew). Most callers should use GenerateCodeCustom instead.

func GenerateCodeUint32

func GenerateCodeUint32(secret string, counter uint64) (uint32, error)

GenerateCodeUint32 generates a numeric passcode for the default encoder. Unlike GenerateCodeCustom / GenerateCode, it returns the raw uint32 value without formatting, saving a string allocation. Returns an error only if the secret is invalid.

func Validate

func Validate(passcode string, counter uint64, secret string) bool

Validate a HOTP passcode given a counter and secret. This is a shortcut for ValidateCustom, with parameters that are compataible with Google-Authenticator.

func ValidateCustom

func ValidateCustom(
	passcode string,
	counter uint64,
	secret string,
	opts ValidateOpts,
) (bool, error)

ValidateCustom validates an HOTP with customizable options. Most users should use Validate().

func ValidateCustomRaw

func ValidateCustomRaw(
	passcode string,
	counter uint64,
	secret []byte,
	opts ValidateOpts,
) (bool, error)

ValidateCustomRaw validates a passcode against pre-decoded secret bytes. This avoids repeated base32 decoding when validating against multiple counters.

Types

type GenerateOpts

type GenerateOpts struct {
	// Name of the issuing Organization/Company.
	Issuer string
	// Name of the User's Account (eg, email address)
	AccountName string
	// Size in size of the generated Secret. Defaults to 10 bytes.
	SecretSize uint
	// Secret to store. Defaults to a randomly generated secret of SecretSize.  You should generally leave this empty.
	Secret []byte
	// Digits to request. Defaults to 6.
	Digits otp.Digits
	// Algorithm to use for HMAC. Defaults to SHA1.
	Algorithm otp.Algorithm
	// Reader to use for generating HOTP Key.
	Rand io.Reader
}

GenerateOpts provides options for .Generate()

type ValidateOpts

type ValidateOpts struct {
	// Digits as part of the input. Defaults to 6.
	Digits otp.Digits
	// Algorithm to use for HMAC. Defaults to SHA1.
	Algorithm otp.Algorithm
	// Encoder to use for output code.
	Encoder otp.Encoder
}

ValidateOpts provides options for ValidateCustom().

Jump to

Keyboard shortcuts

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