otp

package module
v0.0.0-...-4cd0868 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

otp

Go code to generate one-time passwords based on RFCs 4226 and 6238.

Documentation

Overview

Package otp provides tools for generating and validating one-time passwords (OTPs) according to the HOTP (RFC 4226) and TOTP (RFC 6238) standards.

Index

Examples

Constants

View Source
const DefaultTimeStepSeconds = 30

DefaultTimeStepSeconds defines the time step in seconds used to calculate the TOTP counter, as per RFC 6238.

Variables

View Source
var DefaultHashFunction = sha1.New

DefaultHashFunction provides a default hash function (SHA-1) for TOTP generation when none is specified. SHA-1 is chosen for its compatibility with RFC 6238.

Functions

func ComputeHMAC

func ComputeHMAC(hashFunc func() hash.Hash, key, message []byte) ([]byte, error)

ComputeHMAC calculates the HMAC checksum for message using the provided hash function and key. It returns the HMAC checksum or an error if the message cannot be processed.

func GenerateHOTP

func GenerateHOTP(secret []byte, counter uint64, digits uint) (string, error)

GenerateHOTP generates a HMAC-based One-Time Password (HOTP) per RFC 4226 using the provided secret, counter, and the desired OTP length in digits.

Example
package main

import (
	"fmt"

	"github.com/bnixon67/otp"
)

func main() {
	secret := []byte("12345678901234567890")
	counter := uint64(9)
	digits := uint(6)

	fmt.Println("secret", secret)
	fmt.Println("counter", counter)
	fmt.Println("digits", digits)

	hotp, err := otp.GenerateHOTP(secret, counter, digits)
	if err != nil {
		fmt.Println("Error generating HOTP:", err)
		return
	}

	fmt.Println("HOTP:", hotp)
}
Output:

secret [49 50 51 52 53 54 55 56 57 48 49 50 51 52 53 54 55 56 57 48]
counter 9
digits 6
HOTP: 520489

func GenerateOTP

func GenerateOTP(hashFunc func() hash.Hash, secret []byte, counter uint64, digits uint) (string, error)

GenerateOTP generates a HMAC-based One Time Password (OTP) using the given parameters. It supports generating OTPs as per HOTP (RFC 4226) and TOTP (RFC 6238) standards. This function contains common logic of the HOTP and TOTP algorithms.

func GenerateTOTP

func GenerateTOTP(secret []byte, timestamp time.Time, digits uint, hashFunc func() hash.Hash) (string, error)

GenerateTOTP generates a Time-based One-Time Password (TOTP) per RFC 6238 using the provided secret, timestamp, and the desired OTP length in digits. It allows for a custom hash function; if none is provided, SHA-1 is used by default.

Example
package main

import (
	"fmt"
	"time"

	"github.com/bnixon67/otp"
)

func main() {
	secret := []byte("12345678901234567890")
	layout := "2006-01-02 15:04:05"
	time, err := time.Parse(layout, "2033-05-18 03:33:20")
	if err != nil {
		fmt.Println(err)
		return
	}
	digits := uint(8)

	fmt.Println("secret", secret)
	fmt.Println("time", time)
	fmt.Println("digits", digits)

	hotp, err := otp.GenerateTOTP(secret, time, digits, nil)
	if err != nil {
		fmt.Println("Error generating TOTP:", err)
		return
	}

	fmt.Println("TOTP:", hotp)
}
Output:

secret [49 50 51 52 53 54 55 56 57 48 49 50 51 52 53 54 55 56 57 48]
time 2033-05-18 03:33:20 +0000 UTC
digits 8
TOTP: 69279037

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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