-
将下面key替换成google二次认证时的key。文件名 google_authenticator.go
-
go build google_authenticator.go
会生成一个可运行文件google_authenticator
3. 可以重命名一下简单方便的名字,然后配置环境变量到这个目录下,或者移动文件到可运行的环境目录下
4. 可以在任何地方输入gooxxx +tab自动补全,然后回车
package main
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base32"
"encoding/binary"
"fmt"
"time"
)
var key = "J3UYsQJn6pDAAuHQ1G49"
func main(){
data,_ := MakeGoogleAuthenticatorForNow(key)
fmt.Println(data)
}
// MakeGoogleAuthenticator 获取key&t对应的验证码
// key 秘钥
// t 1970年的秒
func MakeGoogleAuthenticator(key string, t int64) (string, error) {
hs, e := hmacSha1(key, t/30)
if e != nil {
return "", e
}
snum := lastBit4byte(hs)
d := snum % 1000000
return fmt.Sprintf("%06d", d), nil
}
// MakeGoogleAuthenticatorForNow 获取key对应的验证码
func MakeGoogleAuthenticatorForNow(key string) (string, error) {
return MakeGoogleAuthenticator(key, time.Now().Unix())
}
func lastBit4byte(hmacSha1 []byte) int32 {
if len(hmacSha1) != sha1.Size {
return 0
}
offsetBits := int8(hmacSha1[len(hmacSha1)-1]) & 0x0f
p := (int32(hmacSha1[offsetBits]) << 24) | (int32(hmacSha1[offsetBits+1]) << 16) | (int32(hmacSha1[offsetBits+2]) << 8) | (int32(hmacSha1[offsetBits+3]) << 0)
return (p & 0x7fffffff)
}
func hmacSha1(key string, t int64) ([]byte, error) {
decodeKey, err := base32.StdEncoding.DecodeString(key)
if err != nil {
return nil, err
}
cData := make([]byte, 8)
binary.BigEndian.PutUint64(cData, uint64(t))
h1 := hmac.New(sha1.New, decodeKey)
_, e := h1.Write(cData)
if e != nil {
return nil, e
}
return h1.Sum(nil), nil
}