otp

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2025 License: MIT Imports: 12 Imported by: 0

README

go-otp

test Go Report Card codecov Version Badge License Badge Go Reference

go-otp is a Go library for generating OTP (One-Time Password) codes. It currently supports TOTP (Time-based One-Time Password, RFC 6238) and is suitable for two-factor authentication, dynamic password, and other security scenarios.

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

Features

  • Supports multiple hash algorithms: SHA1, SHA256, SHA512
  • Customizable code length and period
  • Compatible with popular TOTP apps (e.g., Google Authenticator)
  • Simple and easy-to-use API
  • URI generation for easy integration with authenticator apps

Installation

go get github.com/ghosind/go-otp

Quick Start

package main

import (
	"fmt"
	"time"
	"github.com/ghosind/go-otp"
)

func main() {
	secret := []byte("your-secret-key")
	totp := otp.NewTOTP(
		otp.WithAlgorithm(otp.AlgHmacSha1),
		otp.WithDigits(6),
		otp.WithPeriod(30),
	)
	code, err := totp.Generate(secret)
	if err != nil {
		panic(err)
	}
	fmt.Println("TOTP Code:", code)

	// Generate code for a specific time
	customTime := time.Now()
	code, _ = totp.GenerateWithTime(customTime, secret)
	fmt.Println("Custom Time TOTP:", code)
}

API Reference

TOTP
  • NewTOTP(options...) creates a TOTP instance
  • Generate(secret []byte) generates a code for the current time
  • GenerateWithTime(t time.Time, secret []byte) generates a code for a specific time
  • GetURI(accountName, issuer string, secret []byte) generates an otpauth URI for use with authenticator apps
HOTP
  • NewHOTP(options...) creates a HOTP instance
  • Generate(counter uint64, secret []byte) generates a code for a given counter
  • GetURI(accountName, issuer string, secret []byte, counter uint64) generates an otpauth URI for use with authenticator apps

Testing

The project includes RFC 6238 standard test vectors to ensure algorithm correctness.

Run tests:

go test ./...

License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Examples

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
)

func (Algorithm) String added in v0.2.0

func (a Algorithm) String() string

String returns the string representation of the Algorithm.

type HOTP added in v0.2.0

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

HOTP is HMAC-based One-Time Password algorithm implementation.

Example
package main

import (
	"fmt"

	"github.com/ghosind/go-otp"
)

func main() {
	hotp := otp.NewHOTP()
	secret := []byte("12345678901234567890")
	code, _ := hotp.Generate(0, secret)
	fmt.Println(code)
}
Output:
755224

func NewHOTP added in v0.2.0

func NewHOTP(opts ...Option) *HOTP

NewHOTP creates a new HOTP instance with the given options.

func (*HOTP) Digits added in v0.2.0

func (h *HOTP) Digits() int

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

func (*HOTP) Generate added in v0.2.0

func (h *HOTP) Generate(counter uint64, secret []byte) (string, error)

Generate generates an HOTP using the provided secret key and counter.

func (*HOTP) GetURI added in v1.0.0

func (h *HOTP) GetURI(accountName, issuer string, secret []byte, counter uint64) (string, error)

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.

Example
package main

import (
	"fmt"
	"time"

	"github.com/ghosind/go-otp"
)

func main() {
	totp := otp.NewTOTP()
	secret := []byte("12345678901234567890")
	code, _ := totp.GenerateWithTime(time.Date(2006, time.January, 2, 15, 4, 5, 0, time.UTC), secret)
	fmt.Println(code)
}
Output:
413931

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) GetURI added in v1.0.0

func (t *TOTP) GetURI(accountName, issuer string, secret []byte) (string, error)

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