Categorygithub.com/huk10/go-otp
modulepackage
1.0.0
Repository: https://github.com/huk10/go-otp.git
Documentation: pkg.go.dev

# README

OTP

Build Status MIT License Go Doc Go Coverage Release

This is a Golang OTP library compatible with google-authenticator APP.

Installation

$ go get github.com/huk10/go-otp

Usage

Check API docs at https://godoc.org/github.com/huk10/go-otp

Time-based OTPs

secret := otp.Base32Encode(otp.RandomSecret(20))
totp := otp.NewTOTP(secret)

// Get the token of the current time
_ = totp.Now()

// Get the token for the specified time
token := totp.At(time.Unix(1704075000000, 0))

// otp verify
totp.Verify(token,  time.Unix(1704075000000, 0))

// get qrcode 
png, err := totp.KeyURI("[email protected]", "Example").QRCode()
if err != nil {
    panic(err)
}
// qrcode write to a file
err = os.WriteFile("./qrcode.png", png, 0666)

// get uri
_ = totp.KeyURI("[email protected]", "Example").URI().String()

Counter-based OTPs

secret := otp.Base32Encode(otp.RandomSecret(20))
hotp := otp.NewHOTP(secret)

// Get the token corresponding to counter 1
token := hotp.At(1)

// otp verify
hotp.Verify(token, 1)

// get qrcode 
png, err := hotp.KeyURI("[email protected]", "Example").QRCode()
if err != nil {
    panic(err)
}
// qrcode write to a file
err = os.WriteFile("./qrcode.png", png, 0666)

// get uri
_ = hotp.KeyURI("[email protected]", "Example").URI().String()

Generate random secret

// generate a 20 byte random secret
secret := otp.RandomSecret(20)
// base32 encode
str := otp.Base32Encode(secret)

Google Authenticator Compatible

This library works with the Google Authenticator APP.

// secret 
secret := otp.Base32Encode(otp.RandomSecret(20))

// totp
otp.NewTOTP(secret).KeyURI("[email protected]", "Example").URI().String()

// hotp
otp.NewHOTP(secret, otp.WithCounter(10)).KeyURI("[email protected]", "Example").URI().String()

You can use the URI generated by the above code as the QR code content and use the Google Authenticator APP to scan the code and import it.

Working example

Use Google Authenticator to scan the QR code below.

Demo

Now run the following and compare the output:

package main

import (
	"fmt"
	"github.com/huk10/go-otp"
)

func main() {
	totp := otp.NewTOTP("J3W2XPZP5HDYXYRB4HS6ZLU6M6VBO6C6")
	token := totp.Now()
	fmt.Println("Current OTP is", token)
}

Links

License

go-otp is licensed under the MIT License

# Packages

No description provided by the author

# Functions

Base32Decode 对一个字符串进行 base32 解码.
Base32Encode 对一个字符串进行 base32 编码.
FromURI 解析 URI 创建一个 KeyURI 结构体。.
NewHOTP 创建一个 HOTP 结构体,可以使用 option 的模式传递参数。 Params: secret : 必传,一个 base32 编码后的字符串,建议使用 RandomSecret 方法生成的。 WithCounter : 设置初始计数器,该值仅用于 KeyURI 方法。 WithSkew : 是否校验相邻的窗口。 WithAlgorithm: 设置 hmac 算法类型。 Panic: - secret base32 decode error - secret is an empty string 注意: Google Authenticator 可能仅支持 Counter 这一个参数 See https://github.com/google/google-authenticator/wiki/Key-Uri-Format Example: secret := Base32Encode(RandomSecret(20)) hotp := NewHOTP(secret, WithCounter(2)).
NewTOTP 创建一个 TOTP 结构体,可以使用 option 的模式传递参数。 Params: secret : 必传,一个 base32 编码后的字符串,建议使用 RandomSecret 方法生成的。 WithPeriod : 设置 token 有效期长度。 WithSkew : 是否校验相邻的窗口。 WithAlgorithm: 设置 hmac 算法类型。 Panic: - secret base32 decode error - secret is an empty string 默认参数才是 Google Authenticator 兼容的,自定义参数的话 Google Authenticator 可能不会识别。 See https://github.com/google/google-authenticator/wiki/Key-Uri-Format Example: secret := Base32Encode(RandomSecret(20)) totp := NewTOTP(secret, WithDigits(DigitsEight)).
RandomSecret 获取一个给定长度(字节数)的随机秘钥,如果生成失败将会 panic。 建议存储时将其转换至 base32 或其他的编码,直接转换成字符串可能会存在换行符等奇怪的字符。 内部使用 rand.Read 方法,如果此方法报错将会 panic rfc4266 中建议 secret 最少为 160 位也就是 20 个字节。 https://datatracker.ietf.org/doc/html/rfc4226 也可看下此文档解释自行选择合适长度: https://github.com/darrenedale/php-totp/blob/HEAD/Secrets.md.
WithAlgorithm 配置哈希算法类型。.
WithCounter 配置计数器的值,默认为 1 (Google 的默认就是 1),仅支持 HOTP 类型。.
WithDigits 配置一次性密码的显示长度,默认为 6, Google Authenticator 可能不支持其他的长度。.
WithPeriod 配置时间一次性密码的有效期,默认 30 秒,仅支持 TOTP 类型。 取值范围是:period >=10 如果传入的值小于 10 将会设置为 10。.
WithSkew 配置同时校验的窗口数,默认为 0 仅校验当前时间窗口。 取值范围是:skew >=0 如果传入的值小于 0 将会设置为 0。.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author

# Structs

HOTP 基于 RFC-4266 的 HOTP 算法.
KeyURI TOTP 或 HOTP 的 URI 包含的参数。 URI 的格式可以参考:https://github.com/google/google-authenticator/wiki/Key-Uri-Format 部分属性 Google Authenticator 可能不会采用仅支持默认值。具体细节可以查看上面链接的文档。.
No description provided by the author
TOTP 基于 RFC-6238 的 TOTP 算法.

# Type aliases

Algorithms 支持的 HMAC 类型。 默认值:HMAC_SHA1,与 Google Authenticator 兼容。 See https://github.com/google/google-authenticator/wiki/Key-Uri-Format.
Digits 生成出来的一次性密码的长度。6 和 8 是最常见的值。.
No description provided by the author