otp

package module
v0.0.0-...-7189b9c Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2025 License: MIT Imports: 8 Imported by: 0

README

go-otp

test Go Report Card codecov Version Badge License Badge Go Reference

Golang implementation of the OTP (One Time Password) algorithms.

  • TOTP: Time-based One-Time Password, RFC 6238
  • HOTP: HMAC-based One-Time Password, RFC 4226

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnsupportedAlgorithm is returned when an unsupported algorithm is requested.
	ErrUnsupportedAlgorithm = errors.New("unsupported algorithm")
)

Functions

This section is empty.

Types

type Algorithm

type Algorithm int

Algorithm represents the hashing algorithm used in OTP generation.

const (
	// AlgDefault is the default algorithm, which is HMAC-SHA1 for TOTP.
	AlgDefault Algorithm = iota
	// AlgHmacSha1 is the HMAC-SHA1 algorithm.
	AlgHmacSha1
	// AlgHmacSha256 is the HMAC-SHA256 algorithm.
	AlgHmacSha256
	// AlgHmacSha512 is the HMAC-SHA512 algorithm.
	AlgHmacSha512
)

type Option

type Option func(*otpBuilder)

Option represents a configuration option for OTPs.

func WithAlgorithm

func WithAlgorithm(algorithm Algorithm) Option

WithAlgorithm sets the hashing algorithm for the OTP.

func WithDigits

func WithDigits(digits int) Option

WithDigits sets the number of digits for the OTP.

func WithPeriod

func WithPeriod(period int64) Option

WithPeriod sets the time period in seconds for which a TOTP is valid. It configures the TOTP only.

type TOTP

type TOTP struct {
	// contains filtered or unexported fields
}

TOTP is Time-based One-Time Password algorithm implementation.

func NewTOTP

func NewTOTP(opts ...Option) *TOTP

NewTOTP creates a new TOTP instance with the given options.

func (*TOTP) Algorithm

func (t *TOTP) Algorithm() Algorithm

Algorithm returns the hashing algorithm used in TOTP. If not set, it defaults to HmacSha1.

func (*TOTP) Digits

func (t *TOTP) Digits() int

Digits returns the number of digits in the generated TOTP. If not set, it defaults to 6.

func (*TOTP) Generate

func (t *TOTP) Generate(secret []byte) (string, error)

Generate generates a TOTP using the current time and the provided secret key.

func (*TOTP) GenerateWithTime

func (t *TOTP) GenerateWithTime(tm time.Time, secret []byte) (string, error)

GenerateWithTime generates a TOTP for the given time and secret key.

func (*TOTP) Period

func (t *TOTP) Period() int64

Period returns the time period in seconds for which a TOTP is valid. If not set, it defaults to 30 seconds.

Jump to

Keyboard shortcuts

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