/
dingtalkCrypto.go
226 lines (204 loc) · 6.06 KB
/
dingtalkCrypto.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package dingtalk
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha1"
"encoding/base64"
"encoding/binary"
"errors"
"fmt"
logger "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"github.com/yockii/celestial/internal/module/uc/model"
r "math/rand"
"sort"
"strings"
"time"
)
var DingTalkCryptoMap = make(map[uint64]*dingTalkCrypto)
type dingTalkCrypto struct {
Token string
EncodingAESKey string
SuiteKey string
BKey []byte
Block cipher.Block
}
func initDingtalkCrypto(source *model.ThirdSource) (*dingTalkCrypto, error) {
configuration := source.Configuration
conf := gjson.Parse(configuration)
token := conf.Get("callbackToken").String()
encodingAESKey := conf.Get("callbackAesKey").String()
suiteKey := conf.Get("appKey").String()
if len(encodingAESKey) != int(43) {
return nil, errors.New("不合法的EncodingAESKey")
}
bkey, err := base64.StdEncoding.DecodeString(encodingAESKey + "=")
if err != nil {
return nil, err
}
block, err := aes.NewCipher(bkey)
if err != nil {
return nil, err
}
return &dingTalkCrypto{
Token: token,
EncodingAESKey: encodingAESKey,
SuiteKey: suiteKey,
BKey: bkey,
Block: block,
}, nil
}
func checkCryptoSource(source *model.ThirdSource) (c *dingTalkCrypto, err error) {
if source.ID == 0 {
err = errors.New("sourceId is 0")
logger.Errorln(err)
return
}
var ok bool
c, ok = DingTalkCryptoMap[source.ID]
if !ok {
c, err = initDingtalkCrypto(source)
if err != nil {
return
}
DingTalkCryptoMap[source.ID] = c
}
return
}
func GetDecryptMsg(source *model.ThirdSource, signature, timestamp, nonce, secretMsg string) (msg string, err error) {
var c *dingTalkCrypto
c, err = checkCryptoSource(source)
if err != nil {
return
}
if c == nil {
return
}
if !c.VerificationSignature(c.Token, timestamp, nonce, secretMsg, signature) {
return "", errors.New("ERROR: 签名不匹配")
}
decode, err := base64.StdEncoding.DecodeString(secretMsg)
if err != nil {
return "", err
}
if len(decode) < aes.BlockSize {
return "", errors.New("ERROR: 密文太短")
}
blockMode := cipher.NewCBCDecrypter(c.Block, c.BKey[:c.Block.BlockSize()])
plantText := make([]byte, len(decode))
blockMode.CryptBlocks(plantText, decode)
plantText = pkCS7UnPadding(plantText)
size := binary.BigEndian.Uint32(plantText[16:20])
plantText = plantText[20:]
corpID := plantText[size:]
if string(corpID) != c.SuiteKey {
logger.Info(corpID, "==============", c.SuiteKey)
return "", errors.New("ERROR: CorpID匹配不正确")
}
return string(plantText[:size]), nil
}
func GetEncryptMsg(source *model.ThirdSource, msg string) (result map[string]string, err error) {
var c *dingTalkCrypto
c, err = checkCryptoSource(source)
if err != nil {
return
}
if c == nil {
return
}
var timestamp = time.Now().Second()
var nonce = randomString(12)
str, sign, err := c.GetEncryptMsgDetail(msg, fmt.Sprint(timestamp), nonce)
if err != nil {
logger.Errorln(err)
return
}
result = map[string]string{"nonce": nonce, "timeStamp": fmt.Sprint(timestamp), "encrypt": str, "msg_signature": sign}
return
}
func (c *dingTalkCrypto) GetEncryptMsgDetail(msg, timestamp, nonce string) (string, string, error) {
size := make([]byte, 4)
binary.BigEndian.PutUint32(size, uint32(len(msg)))
msg = randomString(16) + string(size) + msg + c.SuiteKey
plantText := pkCS7Padding([]byte(msg), c.Block.BlockSize())
if len(plantText)%aes.BlockSize != 0 {
return "", "", errors.New("ERROR: 消息体size不为16的倍数")
}
blockMode := cipher.NewCBCEncrypter(c.Block, c.BKey[:c.Block.BlockSize()])
chipherText := make([]byte, len(plantText))
blockMode.CryptBlocks(chipherText, plantText)
outMsg := base64.StdEncoding.EncodeToString(chipherText)
signature := c.CreateSignature(c.Token, timestamp, nonce, string(outMsg))
return string(outMsg), signature, nil
}
func sha1Sign(s string) string {
// The pattern for generating a hash is `sha1.New()`,
// `sha1.Write(bytes)`, then `sha1.Sum([]byte{})`.
// Here we start with a new hash.
h := sha1.New()
// `Write` expects bytes. If you have a string `s`,
// use `[]byte(s)` to coerce it to bytes.
h.Write([]byte(s))
// This gets the finalized hash result as a byte
// slice. The argument to `Sum` can be used to append
// to an existing byte slice: it usually isn't needed.
bs := h.Sum(nil)
// SHA1 values are often printed in hex, for example
// in git commits. Use the `%x` format verb to convert
// a hash results to a hex string.
return fmt.Sprintf("%x", bs)
}
// 数据签名
func (c *dingTalkCrypto) CreateSignature(token, timestamp, nonce, msg string) string {
params := make([]string, 0)
params = append(params, token)
params = append(params, timestamp)
params = append(params, nonce)
params = append(params, msg)
sort.Strings(params)
return sha1Sign(strings.Join(params, ""))
}
// 验证数据签名
func (c *dingTalkCrypto) VerificationSignature(token, timestamp, nonce, msg, sigture string) bool {
return c.CreateSignature(token, timestamp, nonce, msg) == sigture
}
// 解密补位
func pkCS7UnPadding(plantText []byte) []byte {
length := len(plantText)
unpadding := int(plantText[length-1])
return plantText[:(length - unpadding)]
}
// 加密补位
func pkCS7Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
// 随机字符串
func randomString(n int, alphabets ...byte) string {
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, n)
var randby bool
if num, err := rand.Read(bytes); num != n || err != nil {
r.Seed(time.Now().UnixNano())
randby = true
}
for i, b := range bytes {
if len(alphabets) == 0 {
if randby {
bytes[i] = alphanum[r.Intn(len(alphanum))]
} else {
bytes[i] = alphanum[b%byte(len(alphanum))]
}
} else {
if randby {
bytes[i] = alphabets[r.Intn(len(alphabets))]
} else {
bytes[i] = alphabets[b%byte(len(alphabets))]
}
}
}
return string(bytes)
}