/
random_auth_key.go
66 lines (53 loc) · 1.89 KB
/
random_auth_key.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package random_auth_key
import (
"encoding/base64"
"fmt"
"github.com/wumansgy/goEncrypt/aes"
"github.com/jinzhu/now"
)
// RandomAuthKey 伪随机的登录验证 Key
type RandomAuthKey struct {
offset int
authKey AuthKey // 基础的密钥,密钥会基于这个基础的密钥生成
}
// NewRandomAuthKey offset 建议是 5
func NewRandomAuthKey(offset int, authKey AuthKey) *RandomAuthKey {
tmpOffset := offset
if tmpOffset < 0 || tmpOffset > 5 {
tmpOffset = 5
}
return &RandomAuthKey{
offset: tmpOffset,
authKey: authKey,
}
}
// GetAuthKey 获取这个小时的认证密码
func (r *RandomAuthKey) GetAuthKey() (string, error) {
// 当前时间 Unix时间戳 1653099199,这里获取到的是整点的小时时间
nowUnixTime := now.BeginningOfHour().Unix()
return r.getAuthKey(nowUnixTime)
}
func (r RandomAuthKey) getAuthKey(hourUnixTime int64) (string, error) {
nowUnixTimeStr := fmt.Sprintf("%d", hourUnixTime)
prefixStr := nowUnixTimeStr[len(nowUnixTimeStr)-r.offset:] + nowUnixTimeStr[:r.offset]
// 拼接
orgString := prefixStr + r.authKey.BaseKey + nowUnixTimeStr + r.authKey.BaseKey[:r.offset]
plaintext := []byte(orgString)
// 传入明文和自己定义的密钥,密钥为 16 字节 可以自己传入初始化向量,如果不传就使用默认的初始化向量, 16 字节
cryptText, err := aes.AesCbcEncrypt(plaintext, []byte(r.authKey.AESKey16), []byte(r.authKey.AESIv16))
if err != nil {
return "", err
}
// 加密后的字符串
return base64.StdEncoding.EncodeToString(cryptText), nil
}
type AuthKey struct {
BaseKey string // 基础的密钥,密钥会基于这个基础的密钥生成
AESKey16 string // AES密钥
AESIv16 string // 初始化向量
}
const (
BaseKey = "0123456789123456789" // 基础的密钥,密钥会基于这个基础的密钥生成
AESKey16 = "1234567890123456" // AES密钥
AESIv16 = "1234567890123456" // 初始化向量
)