An easy-to-use implementation of RFC 4226 (HOTP) and RFC 6238 (TOTP) in Go.
2022-06-20 09:10:05 +05:30
go.mod init 2022-01-28 07:01:10 +05:30
LICENSE init 2022-01-28 07:01:10 +05:30
otp.go otp.go: Replaced log.Fatal with panic; updated documentation. 2022-01-28 08:32:16 +05:30
otp_test.go otp_test: use idiomatic Go 2022-06-20 09:10:05 +05:30
README.md otp.go: Replaced log.Fatal with panic; updated documentation. 2022-01-28 08:32:16 +05:30

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"
}