-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcrypto.go
126 lines (98 loc) · 2.69 KB
/
crypto.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package crypter
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha1"
"encoding/base64"
"encoding/binary"
"errors"
"fmt"
"io"
"sort"
"strings"
)
// MessageCrypter 封装了生成签名和消息加解密的方法
type MessageCrypter struct {
token string
appID string
key []byte
iv []byte
}
// NewMessageCrypter 方法用于创建 MessageCrypter 实例
//
// token 为开发者在微信开放平台上设置的 Token,
// encodingAESKey 为开发者在微信开放平台上设置的 EncodingAESKey,
// appID 为企业号的 CorpId 或者 AppId
func NewMessageCrypter(token, encodingAESKey, appID string) (MessageCrypter, error) {
var key []byte
var err error
if key, err = base64.StdEncoding.DecodeString(encodingAESKey + "="); err != nil {
return MessageCrypter{}, err
}
if len(key) != 32 {
return MessageCrypter{}, errors.New("encodingAESKey invalid")
}
iv := key[:16]
return MessageCrypter{
token,
appID,
key,
iv,
}, nil
}
// GetSignature 方法用于返回签名
func (w MessageCrypter) GetSignature(timestamp, nonce, msgEncrypt string) string {
sl := []string{w.token, timestamp, nonce, msgEncrypt}
sort.Strings(sl)
s := sha1.New()
io.WriteString(s, strings.Join(sl, ""))
return fmt.Sprintf("%x", s.Sum(nil))
}
// Decrypt 方法用于对密文进行解密
//
// 返回解密后的消息,CropId/AppId, 或者错误信息
func (w MessageCrypter) Decrypt(text string) ([]byte, string, error) {
var msgDecrypt []byte
var id string
deciphered, err := base64.StdEncoding.DecodeString(text)
if err != nil {
return nil, "", err
}
c, err := aes.NewCipher(w.key)
if err != nil {
return nil, "", err
}
cbc := cipher.NewCBCDecrypter(c, w.iv)
cbc.CryptBlocks(deciphered, deciphered)
decoded := PKCS7Decode(deciphered)
buf := bytes.NewBuffer(decoded[16:20])
var msgLen int32
binary.Read(buf, binary.BigEndian, &msgLen)
msgDecrypt = decoded[20 : 20+msgLen]
id = string(decoded[20+msgLen:])
return msgDecrypt, id, nil
}
// Encrypt 方法用于对明文进行加密
func (w MessageCrypter) Encrypt(text string) (string, error) {
message := []byte(text)
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.BigEndian, int32(len(message))); err != nil {
return "", err
}
msgLen := buf.Bytes()
randBytes := make([]byte, 16)
if _, err := io.ReadFull(rand.Reader, randBytes); err != nil {
return "", err
}
messageBytes := bytes.Join([][]byte{randBytes, msgLen, message, []byte(w.appID)}, nil)
encoded := PKCS7Encode(messageBytes)
c, err := aes.NewCipher(w.key)
if err != nil {
return "", err
}
cbc := cipher.NewCBCEncrypter(c, w.iv)
cbc.CryptBlocks(encoded, encoded)
return base64.StdEncoding.EncodeToString(encoded), nil
}