-
Notifications
You must be signed in to change notification settings - Fork 155
/
main.go
109 lines (94 loc) · 2.79 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"encoding/binary"
"encoding/hex"
"fmt"
"time"
)
const (
aesKEY = "justTest!@#$test" // must be 16 characters
md5Len = 4 //MD5 part size in access token
aesLen = 16 //bytes after being encrypted by aes
)
func main() {
token := genAccessToken(1234567)
println(token)
println(ParseAccessToken(token))
}
// generate access token
// + means concatenate
// Token = Hex(MD5(uid + time)[0:4] + AES(uid + time))
// a string with length of 40
func genAccessToken(uid int64) string {
byteInfo := make([]byte, 12)
binary.BigEndian.PutUint64(byteInfo, uint64(uid))
binary.BigEndian.PutUint32(byteInfo[8:], uint32(time.Now().Unix()))
encodeByte, err := AesEncrypt(byteInfo, []byte(aesKEY)) //returned slice's size is 16
if err != nil {
fmt.Println("desc: ", "genAccessToken-AesEncrypt", "error: ", err, "byteInfo: ", byteInfo)
}
md5Byte := md5.Sum(byteInfo)
data := append(md5Byte[0:md5Len], encodeByte...)
return hex.EncodeToString(data)
}
// parse uid from access token
func ParseAccessToken(accessToken string) (uid uint64) {
if len(accessToken) != 2*(md5Len+aesLen) {
fmt.Println("log_desc", "len(accessToken)", "length", len(accessToken))
return
}
encodeStr := accessToken[md5Len*2:]
data, e := hex.DecodeString(encodeStr)
if e != nil {
fmt.Println("log_desc", "ParseAccessToken-DecodeString)", "error", e, "accessToken", encodeStr)
return
}
decodeByte, e := AesDecrypt(data, []byte(aesKEY)) //忽略错误
if e != nil {
fmt.Println("log_desc", "ParseAccessToken-AesDecrypt)", "error", e, "data", data)
}
uid = binary.BigEndian.Uint64(decodeByte)
return uid
}
//key is 16 bytes
func AesEncrypt(origData, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
origData = PKCS5Padding(origData, blockSize)
blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
crypted := make([]byte, len(origData))
blockMode.CryptBlocks(crypted, origData)
return crypted, nil
}
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func AesDecrypt(crypted, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
origData := make([]byte, len(crypted))
blockMode.CryptBlocks(origData, crypted)
origData = PKCS5UnPadding(origData)
return origData, nil
}
func PKCS5UnPadding(origData []byte) []byte {
length := len(origData)
unPadding := int(origData[length-1])
if unPadding < 1 || unPadding > 32 {
unPadding = 0
}
return origData[:(length - unPadding)]
}