go1.5 动态密码,最简单的实现使用hmac加密
发布时间:2020-12-16 18:39:38 所属栏目:大数据 来源:网络整理
导读:package mainimport ("crypto/hmac""crypto/sha512""fmt""strconv""time")type Key struct {gkey stringskey stringdate func() int64}const (Gkey = "What")func main() {K := Key{gkey: Gkey,date: getdate}b := hmac.New(sha512.New,[]byte(K.Hmac("Hello
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) } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |