前端之家收集整理的这篇文章主要介绍了
go1.5 动态密码,最简单的实现使用hmac加密,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package main
import (
"crypto/hmac"
"crypto/sha512"
"fmt"
"strconv"
"time"
)
type Key struct {
gkey string
skey string
date func() int64
}
const (
Gkey = "What"
)
func main() {
K := &Key{gkey: Gkey,date: getdate}
b := hmac.New(sha512.New,[]byte(K.Hmac("Hello World")))
B := b.Sum(nil)
offset := B[len(B)-1] & 0xf
x := ((int(B[offset+1]) & 0xff) << 16) | ((int(B[offset+2]) & 0xff) << 8) | (int(B[offset+3]) & 0xff)
z := fmt.Sprint(x % 1000000)
for len(z) < 6 {
z = fmt.Sprintf("0%s",z)
}
fmt.Println(z)
}
func (self *Key) Hmac(Skey string) string {
return fmt.Sprintf("%s%s%d",self.gkey,Skey,self.date())
}
func getdate() int64 {
T := time.Now()
format_T := T.Format("0601021504")
unix_T := T.Unix() - T.Unix()%30
num,_ := strconv.Atoi(format_T)
x := num % 10
return unix_T<<uint(x) ^ int64(num)>>uint(x)
}
原文链接:https://www.f2er.com/go/190224.html