Skip to content
/ totp Public

High-performance and secure TOTP generator and validator in Go, supporting multiple hashing algorithms: SHA1, SHA256, SHA512, and BLAKE2.

License

Notifications You must be signed in to change notification settings

itpey/totp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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