otp

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2022 License: MIT Imports: 8 Imported by: 0

README

otp

Go Reference

otp is an easy-to-use implementation of RFC 4226 (HOTP) and RFC 6238 (TOTP) in Go.

Only base-32 encoded secret-keys are supported.

Supported hash functions: SHA1, SHA256, and SHA512 (the latter two from the SHA-2 family, not SHA-3).

Usage Overview

package main

import (
	"fmt"
	"log"

	"codeberg.org/ar324/otp"
)

func main() {
	// HOTP
	hk := otp.HOTPKey{
		SecretKey:    "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ",
		HashFunction: otp.SHA1,
		Digits:       8,
		Counter:      0x0000000000000001,
	}
	if !hk.Validate() {
		log.Fatalln("invalid HOTP parameters")
	}
	fmt.Println(hk.OTP()) // prints "94287082"

	// TOTP
	tk := otp.TOTPKey{
		SecretKey:    "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ",
		HashFunction: otp.SHA512,
		Digits:       8,
		TimeStep:     60,
	}
	if !tk.Validate() {
		log.Fatalln("invalid TOTP parameters")
	}
	tk.OTP() // "88486101"
}

Documentation

Overview

Package 'otp' is an easy-to-use implementation of RFC 4226 (HOTP) and RFC 6238 (TOTP).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HOTPKey

type HOTPKey struct {
	SecretKey    string       `json:"secret_key"`
	HashFunction HashFunction `json:"hash_function"`
	Digits       byte         `json:"digits"`
	Counter      uint64       `json:"counter"`
}

Represents an HOTP parameter-set. SecretKey must be base-32 encoded.

func (*HOTPKey) OTP

func (k *HOTPKey) OTP() string

Computes and returns an OTP using the HOTP parameter-set. If the receiver HOTPKey is invalid, the program panics.

func (*HOTPKey) Validate

func (k *HOTPKey) Validate() bool

Validates an HOTPKey.

type HashFunction

type HashFunction string
const (
	SHA1   HashFunction = "SHA1"
	SHA256 HashFunction = "SHA256"
	SHA512 HashFunction = "SHA512"

	MinKeySize = 16
	MaxDigits  = 10
)

type TOTPKey

type TOTPKey struct {
	SecretKey    string       `json:"secret_key"`
	HashFunction HashFunction `json:"hash_function"`
	Digits       byte         `json:"digits"`
	TimeStep     uint64       `json:"time_step"`
	T0           uint64       `json:"t0"`
}

Represents a TOTP parameter-set. Like in HOTPKey, SecretKey must be base-32 encoded. Even though T0 not a parameter in virtually all implementations, according to RFC 6238, it is not necessarily always 0—which is why it is a parameter here.

func (*TOTPKey) OTP

func (k *TOTPKey) OTP() string

Computes and returns an OTP using the TOTP parameter-set. If the receiver TOTPKey is invalid, the program panics.

func (*TOTPKey) Validate

func (k *TOTPKey) Validate() bool

Validates a TOTPKey.

Jump to

Keyboard shortcuts

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