gogotp

package module
v0.0.0-...-6f04214 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2019 License: MIT Imports: 9 Imported by: 0

README

一个用于生成和验证一次性密码的Golang包,它可用于在需要用户登录的任何地方实施双因素(2FA)或多因素(MFA)身份验证。 开放式MFA标准在RFC 4226(HOTP:基于HMAC的一次性密码算法)和RFC 6238(TOTP:基于时间的一次性密码算法)中定义,此包为这两个标准实现了服务器端支持。

基于时间 OTP

totp := gotp.NewDefaultTOTP("4S62BZNFXXSZLCRO")
totp.Now()          // current otp '123456'
totp.At(1524486261) // otp of timestamp 1524486261 '123456'

// 根据给定的时间戳验证 OTP
totp.Verify('492039', 1524486261)  // true
totp.Verify('492039', 1520000000)  // false

// 生成验证URI
totp.ProvisioningUri("demoAccountName", "issuerName")
// otpauth://totp/issuerName:demoAccountName?secret=4S62BZNFXXSZLCRO&issuer=issuerName

基于计数器 OTP

hotp := gotp.NewDefaultHOTP("4S62BZNFXXSZLCRO")
hotp.At(0)  // '944181'
hotp.At(1)  // '770975'

// 根据给定的计数验证OTP
hotp.Verify('944181', 0)  // true
hotp.Verify('944181', 1)  // false

// 生成验证URI
hotp.ProvisioningUri("demoAccountName", "issuerName", 1)
// otpauth://hotp/issuerName:demoAccountName?secret=4S62BZNFXXSZLCRO&counter=1&issuer=issuerName

生成随机 secret

secretLength := 16
gotp.RandomSecret(secretLength) // LMT4URYNZKEWZRAA

兼容Google身份验证器

可与iPhone和Android的Google身份验证器以及其他OTP应用(如Authy)配合使用。 其包括生成供QR码使用的验证URI能力,通过otpObj.ProvisioningUri方法内置到MFA客户端应用程序中的扫描程序:

// TOTP
gotp.NewDefaultTOTP("4S62BZNFXXSZLCRO").ProvisioningUri("demoAccountName", "issuerName")
// otpauth://totp/issuerName:demoAccountName?secret=4S62BZNFXXSZLCRO&issuer=issuerName

// HOTP
gotp.NewDefaultHOTP("4S62BZNFXXSZLCRO").ProvisioningUri("demoAccountName", "issuerName", 1)
// otpauth://hotp/issuerName:demoAccountName?secret=4S62BZNFXXSZLCRO&counter=1&issuer=issuerName

然后,可以将此URL呈现为QR码,将其扫描并添加到OTP凭证的用户列表中。

示例

使用手机的OTP应用扫描以下条形码(例如Google身份验证器):

Demo

运行以下命令并比较输出:

package main

import (
	"fmt"
	"gogotp"
	"gogotp/util"
	"crypto/sha512"
)

func main() {
	//totp := gogotp.NewDefaultTOTP("FEFEiowefewf")

	//key:=util.RandomSecret(16)
	totp2 := gogotp.NewTOTP("FD5AP7ZA4IUSIPWB", 8, 100, &gogotp.Hasher{HashName: "sha512", Digest: sha512.New})
	//totp2 := gogotp.NewTOTP("ffff", 8, 100, &gogotp.Hasher{HashName: "sha512", Digest: sha512.New})
	fmt.Println(totp2.ProvisioningUri("haha@haha.com", "thinking"))

	//fmt.Println(totp.ProvisioningUri("test@test.com", "test_comp"))
	//totp2.Verify("25230888", int(time.Now().UnixNano()))
	//fmt.Println(util.CurrentTimestamp())
	fmt.Println(totp2.Verify("71478833", util.CurrentTimestamp()))
	//fmt.Println(totp2.At(util.CurrentTimestamp()))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HOTP

type HOTP struct {
	OTP
}

基于HMAC的OTP计数器

func NewDefaultHOTP

func NewDefaultHOTP(secret string) *HOTP

生成默认OTP对象

func NewHOTP

func NewHOTP(secret string, digits int, hasher *Hasher) *HOTP

func (*HOTP) At

func (h *HOTP) At(count int) string

根据给定的整数生成OTP值

func (*HOTP) ProvisioningUri

func (h *HOTP) ProvisioningUri(accountName, issuerName string, initialCount int) string

获取需要验证OTP的URI,可以嵌入到二维码中 https://github.com/google/google-authenticator/wiki/Key-Uri-Format * 参数说明:

	accountName:	账号名
    issuerName:		OTP发行人名称,这是OTP的组织标题
    initialCount:	初始HMAC计数器值

返回值:

用于验证的URI

func (*HOTP) Verify

func (h *HOTP) Verify(otp string, count int) bool

验证OTP * 参数说明:

	otp:	待检查的OTP值
    count:	验证OTP的HMAC计数器

返回值:

bool	是否验证成功,成功返回true

type Hasher

type Hasher struct {
	HashName string
	Digest   func() hash.Hash
}

type OTP

type OTP struct {
	// contains filtered or unexported fields
}

func NewOTP

func NewOTP(secret string, digits int, hasher *Hasher) OTP

新建OTP对象

type TOTP

type TOTP struct {
	OTP
	// contains filtered or unexported fields
}

基于时间的OTP计数器

func NewDefaultTOTP

func NewDefaultTOTP(secret string) *TOTP

生成默认OTP对象

func NewTOTP

func NewTOTP(secret string, digits, interval int, hasher *Hasher) *TOTP

func (*TOTP) At

func (t *TOTP) At(timestamp int) string

根据给定的时间戳生成OTP值

func (*TOTP) Now

func (t *TOTP) Now() string

生成当前时间的OTP值

func (*TOTP) NowWithExpiration

func (t *TOTP) NowWithExpiration() (string, int64)

生成当前时间的OTP值,并返回过期时间

func (*TOTP) ProvisioningUri

func (t *TOTP) ProvisioningUri(accountName, issuerName string) string

获取需要验证OTP的URI,可以嵌入到二维码中 https://github.com/google/google-authenticator/wiki/Key-Uri-Format * 参数说明:

	accountName:	账号名
    issuerName:		OTP发行人名称,这是OTP的组织标题

返回值:

用于验证的URI

func (*TOTP) Verify

func (t *TOTP) Verify(otp string, timestamp int) bool

验证OTP * 参数说明:

	otp:         待检查的OTP值
    timestamp:   验证OTP的时间戳

返回值:

bool	是否验证成功,成功返回true

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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