otp – zgo.at/otp Index | Files | Directories

package otp

import "zgo.at/otp"

Package otp implemnts HOTP and TOTP one-time passwords.

Index

Functions

func New

func New(sharedSecret []byte, tokenLength int, hash func() hash.Hash, c CounterFunc) generator

New returns a generator to generate and verify HMAC one-time passwords.

Panics if tokenLength is <= 0 or if any of the other parameters are nil.

func Secret

func Secret() []byte

Secret generates a new shared secret.

It's not required to use this function specifically. It's just here for convenience.

func URL

func URL(key []byte, issuer, email string) url

URL creates an URL that can be used by most OTP apps, such as FreeOTP, Yubico Authenticator, Google Authenticator, etc.

Types

type CounterFunc

type CounterFunc func(offset int) uint64

CounterFunc is a function that is called when generating a one-time password and returns a seed value.

In HOTP this will be an incrementing counter, in TOTP it returns the current time.

Offset indicates that we want the token relative to the current token by offset (eg. -1 for the previous token).

func TOTP

func TOTP(step time.Duration, t func() time.Time) CounterFunc

TOTP returns a counter function to generate TOTP tokens as defined in RFC6238.

TOTP tokens are time-based and valid for step duration. It will use 30 seconds if zero, which is a reasonable default, but in some cases where clock skew is expected a longer value may be used.

Providing the time can be useful to provide a fixed time for testing. It uses time.Now() if nil.

Source Files

otp.go

Directories

PathSynopsis
internal
Version
v0.0.0-20250619162254-96c2c40ec359 (latest)
Published
Jun 19, 2025
Platform
linux/amd64
Imports
14 packages
Last checked
2 weeks ago

Tools for package owners.