totp

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2023 License: MIT Imports: 12 Imported by: 0

README

TOTP Package

This package provides a Go implementation of the Time-based One-time Password (TOTP) algorithm as described in RFC 6238. The implementation supports following customizations:

  • SHA-1, SHA-256, SHA-512 hashing algorithms (default is SHA-1)
  • up to 10 digits long codes (default is 6)
  • custom time step (default is 30 seconds)

The package doesn't have any external dependencies.

Usage

First, import the package:

import "github.com/verte-zerg/totp"

Create a new TOTP instance with a base32 encoded secret or use functions with secret as a parameter:

SECRET := "YOUR_BASE32_ENCODED_SECRET"
totpInstance, err := totp.New(SECRET, nil)
if err != nil {
    // handle error
}

Generate a TOTP:

code := totpInstance.Generate()

Verify a TOTP:

isValid := totpInstance.Verify("123456")

You can also generate and verify TOTPs at a specific time:

ts := time.Now()

code := totpInstance.GenerateAt(ts)
isValid := totpInstance.VerifyAt("123456", ts)

Options

Options can be passed to the New function to customize the TOTP instance:

options := &totp.Options{
    Digits:    6,                    // default is 6
    Algorithm: totp.SHA1,            // default is SHA1
    TimeStep:  30,                   // default is 30
}
totpInstance, err := totp.New(SECRET, options)

Functions

  • New(secret string, options *Options) (*TOTP, error): Creates a new TOTP instance with the given base32 encoded secret.
  • (t *TOTP) Generate() string: Generates a TOTP using the current time.
  • (t *TOTP) GenerateAt(timestamp time.Time) string: Generates a TOTP at the given time.
  • (t *TOTP) Verify(code string) bool: Verifies a TOTP using the current time.
  • (t *TOTP) VerifyAt(code string, timestamp time.Time) bool: Verifies a TOTP at the given time.

Contributing

Contributions are welcome! Please submit a pull request or create an issue to get started.

Documentation

Index

Constants

View Source
const (
	// DefaultDigits is the default number of digits in a generated code
	DefaultDigits = 6
	// Default period is the default number of seconds a code is valid
	DefaultPeriod = 30
	// DefaultAlgorithm is the default algorithm used to generate codes
	DefaultAlgorithm = SHA1
)

Variables

View Source
var (
	ErrDecodingBase32Secret = errors.New("error decoding base32 secret")
	ErrTooManyDigits        = errors.New("too many digits")
	ErrUnknownHashAlgorithm = errors.New("unknown hash algorithm")
)

Functions

This section is empty.

Types

type Algorithm

type Algorithm string
const (
	SHA1   Algorithm = "sha1" // default
	SHA256 Algorithm = "sha256"
	SHA512 Algorithm = "sha512"
)

type Options

type Options struct {
	Algorithm Algorithm // The algorithm used to generate codes. Can be SHA1, SHA256 or SHA512. Default: SHA1
	Digits    uint      // The number of digits in a generated code. Must be between 1 and 10. Default: 6
	Period    uint      // The number of seconds a code is valid. Default: 30
}

type TOTP

type TOTP struct {
	Secret    []byte
	ALgorithm Algorithm
	Digits    uint
	Period    uint
	HashFunc  func() hash.Hash
}

TOTP is a Time-based One-time Password algorithm implementation as described in RFC 6238. For creating use New(secret string, options *Options) (*TOTP, error)

func New

func New(secret string, options *Options) (*TOTP, error)

Create a new TOTP instance secret is the shared secret used to generate codes, encoded as base32 string options is an optional struct for configuring the TOTP with the following fields: - Algorithm: The algorithm used to generate codes. Can be SHA1, SHA256 or SHA512. Default: SHA1 - Digits: The number of digits in a generated code. Must be between 1 and 10. Default: 6 - Period: The number of seconds a code is valid. Default: 30

func (*TOTP) Generate

func (t *TOTP) Generate() string

Generate a TOTP code using the current time

func (*TOTP) GenerateAt

func (t *TOTP) GenerateAt(timestamp time.Time) string

Generate a TOTP code using the given time The timestamp must be a time.Time instance

func (*TOTP) Verify

func (t *TOTP) Verify(code string) bool

Verify a TOTP code using the current time

func (*TOTP) VerifyAt

func (t *TOTP) VerifyAt(code string, timestamp time.Time) bool

Verify a TOTP code using the given time The timestamp must be a time.Time instance

Jump to

Keyboard shortcuts

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