totp

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2025 License: MIT Imports: 16 Imported by: 0

README

TOTP (Time-Based One-Time Password) Generator

Go Version License MIT GoDoc Go Report Card

A high-performance and secure TOTP (Time-Based One-Time Password) generator and validator implemented in Go that supports multiple hashing algorithms, including SHA1, SHA256, SHA512, and BLAKE2.

Features

  • Efficient HMAC pooling to optimize performance.
  • Supports multiple hashing algorithms (SHA1, SHA256, SHA512, BLAKE2, etc.).
  • Customizable TOTP settings (digits, time period, skew allowance).
  • Secure validation with constant-time comparison.

Installation

go get -u github.com/itpey/totp

Usage

Generate a TOTP Code
package main

import (
	"fmt"
	"time"

	"github.com/itpey/totp"
)

func main() {
	generator := totp.New(totp.Config{
		Secret:    "JBSWY3DPEHPK3PXP", // Base32 encoded secret key
		Algorithm: totp.AlgorithmSHA1,
		Digits:    totp.DigitsSix,
		Period:    30,
		Skew:      1,
	})

	code, err := generator.Generate()
	if err != nil {
		fmt.Println("Error generating TOTP:", err)
		return
	}

	fmt.Println("Generated TOTP Code:", code)
}
Validate a TOTP Code
valid, err := generator.Validate("123456")
if err != nil {
	fmt.Println("Validation error:", err)
} else if valid {
	fmt.Println("TOTP is valid!")
} else {
	fmt.Println("TOTP is invalid!")
}

Configuration

Field Type Default Description
Secret string --- Base32 encoded secret key
Algorithm Algorithm SHA1 Hashing algorithm
Digits Digits 6 Number of digits in OTP
Period int64 30 Time step in seconds
Skew int64 1 Allowed time skew in periods

Supported Hashing Algorithms

  • SHA1
  • SHA224
  • SHA256
  • SHA384
  • SHA512
  • SHA3-224
  • SHA3-256
  • SHA3-384
  • SHA3-512
  • BLAKE2S-256
  • BLAKE2B-256
  • BLAKE2B-384
  • BLAKE2B-512
  • MD5

Feedback and Contributions

If you encounter any issues or have suggestions for improvement, please open an issue on GitHub.

We welcome contributions! Fork the repository, make your changes, and submit a pull request.

License

TOTP is open-source software released under the MIT License. You can find a copy of the license in the LICENSE file.

Author

TOTP was created by itpey

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConfigDefault = Config{
	Algorithm: AlgorithmSHA1,
	Digits:    DigitsSix,
	Period:    30,
	Skew:      1,
}

ConfigDefault is the default configuration.

Functions

This section is empty.

Types

type Algorithm

type Algorithm int

Algorithm represents the hashing algorithm to use.

const (
	// Constants for supported algorithms.
	AlgorithmSHA1 Algorithm = iota
	AlgorithmSHA224
	AlgorithmSHA256
	AlgorithmSHA384
	AlgorithmSHA512
	AlgorithmSHA3_224
	AlgorithmSHA3_256
	AlgorithmSHA3_384
	AlgorithmSHA3_512
	AlgorithmBLAKE2S_256
	AlgorithmBLAKE2B_256
	AlgorithmBLAKE2B_384
	AlgorithmBLAKE2B_512
	AlgorithmMD5
)

type Config

type Config struct {
	Algorithm Algorithm // Hashing algorithm to use (default: SHA1)
	Digits    Digits    // Number of output digits (default: 6)
	Period    int64     // Validity period in seconds (default: 30)
	Secret    string    // Base32 encoded secret key
	Skew      int64     // Time skew adjustment (default: 1)
}

Config holds the configuration settings for hashing.

type Digits

type Digits int

Digits represents the number of digits to use.

const (
	// Constants for the number of digits.
	DigitsFour  Digits = 4
	DigitsFive  Digits = 5
	DigitsSix   Digits = 6
	DigitsEight Digits = 8
)

type TOTP

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

TOTP generates time-based one-time passwords.

func New

func New(config ...Config) *TOTP

New initializes a TOTP generator with the given configuration.

func (*TOTP) Generate

func (o *TOTP) Generate() (string, error)

Generate generates a TOTP for the current time.

func (*TOTP) GenerateForTime

func (o *TOTP) GenerateForTime(t time.Time) (string, error)

GenerateForTime generates a TOTP for a specific Unix time.

func (*TOTP) Validate

func (o *TOTP) Validate(totp string) (bool, error)

Validate checks whether the provided TOTP is valid for the current time.

func (*TOTP) ValidateForTime

func (o *TOTP) ValidateForTime(totp string, t time.Time) (bool, error)

ValidateForTime checks if the given TOTP is valid for a specific time, considering allowed skew.

Jump to

Keyboard shortcuts

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