Skip to content

Commit 1f05ddf

Browse files
authored
Merge pull request #2 from ArtisanCloud/feature/utils-base
add aes
2 parents a47b240 + b0e6154 commit 1f05ddf

File tree

10 files changed

+616
-6
lines changed

10 files changed

+616
-6
lines changed

.github/workflows/go-build.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Go Build
2+
3+
on: [ push ]
4+
jobs:
5+
build-go:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v2
9+
- name: Set up Go
10+
uses: actions/setup-go@v2
11+
with:
12+
go-version: 1.16
13+
14+
- name: Build
15+
run: make build
16+

.github/workflows/go-test.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: Go Test
2+
3+
on: [ push ]
4+
jobs:
5+
test-go:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v2
9+
- name: Set up Go
10+
uses: actions/setup-go@v2
11+
with:
12+
go-version: 1.16
13+
- name: Test
14+
run: make test

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
test: test-aes
2+
3+
test-aes:
4+
go test -v src/kernel/Encryptor.go src/kernel/Encryptor_test.go
5+
go test -v src/kernel/Support/AES.go src/kernel/Support/AES_test.go
6+
7+
build:
8+
go build

go.mod

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ module github.com/ArtisanCloud/go-wechat
22

33
go 1.16
44

5-
replace github.com/ArtisanCloud/go-libs => ../go-libs
6-
7-
replace github.com/ArtisanCloud/go-socialite => ../go-socialite
8-
95
require (
10-
github.com/ArtisanCloud/go-libs v1.0.10
11-
github.com/ArtisanCloud/go-socialite v0.0.0-00010101000000-000000000000 // indirect
6+
github.com/ArtisanCloud/go-libs v1.0.11
7+
github.com/ArtisanCloud/go-socialite v1.0.0
128
github.com/gin-gonic/gin v1.7.1 // indirect
9+
github.com/go-playground/assert/v2 v2.0.1
1310
github.com/go-redis/redis/v8 v8.10.0 // indirect
1411
github.com/mattn/go-isatty v0.0.13 // indirect
12+
github.com/stretchr/testify v1.7.0
1513
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect
1614
golang.org/x/sys v0.0.0-20210608053332-aa57babbf139 // indirect
1715
gopkg.in/yaml.v2 v2.4.0 // indirect

go.sum

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
github.com/ArtisanCloud/go-libs v1.0.11 h1:CF39XsRaOKyphG7eoUVDkdqj2L2DUKfSrDr+tZyYbMI=
2+
github.com/ArtisanCloud/go-libs v1.0.11/go.mod h1:QdDzmJskQvtLTwVpF6KZqnFEwBfTdIpF0FLy6WHvKmA=
3+
github.com/ArtisanCloud/go-socialite v1.0.0 h1:eQnhkQTP8a7qr3QXFU1YOaf1jHJL6UnrmLNPowruUp8=
4+
github.com/ArtisanCloud/go-socialite v1.0.0/go.mod h1:/VYnYt7mGKZxPyWr+qPlLv1/+H6xcZP+B7XdsbNhgt0=
15
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
26
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
37
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -6,12 +10,14 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
610
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
711
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
812
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
13+
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
914
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
1015
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
1116
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
1217
github.com/gin-gonic/gin v1.6.0/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
1318
github.com/gin-gonic/gin v1.7.1 h1:qC89GU3p8TvKWMAVhEpmpB2CIb1hnqt2UdKZaP93mS8=
1419
github.com/gin-gonic/gin v1.7.1/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
20+
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
1521
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
1622
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
1723
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
@@ -40,6 +46,7 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
4046
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
4147
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
4248
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
49+
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
4350
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
4451
github.com/guonaihong/gout v0.1.11 h1:Sl4OwyNtp1BoMZXankNC0DimAPuh9kgBsZIGynD9kiw=
4552
github.com/guonaihong/gout v0.1.11/go.mod h1:JkjNv1G2oRWvFgP/r4DUbYhoeIBB0zMP2j1ID+5CYpU=
@@ -55,12 +62,15 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OH
5562
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
5663
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
5764
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
65+
github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78=
5866
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
5967
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
6068
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
69+
github.com/onsi/ginkgo v1.15.0 h1:1V1NfVQR87RtWAgp1lv9JZJ5Jap+XFGKPi00andXGi4=
6170
github.com/onsi/ginkgo v1.15.0/go.mod h1:hF8qUzuuC8DJGygJH3726JnCZX4MYbRB8yFfISqnKUg=
6271
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
6372
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
73+
github.com/onsi/gomega v1.10.5 h1:7n6FEkpFmfCoo2t+YYqXH0evK+a9ICQz0xcAy9dYcaQ=
6474
github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48=
6575
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
6676
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
@@ -85,6 +95,7 @@ go.opentelemetry.io/otel/metric v0.19.0/go.mod h1:8f9fglJPRnXuskQmKpnad31lcLJ2Vm
8595
go.opentelemetry.io/otel/metric v0.20.0 h1:4kzhXFP+btKm4jwxpjIqjs41A7MakRFUS86bqLHTIw8=
8696
go.opentelemetry.io/otel/metric v0.20.0/go.mod h1:598I5tYlH1vzBjn+BTuhzTCSb/9debfNp6R3s7Pr1eU=
8797
go.opentelemetry.io/otel/oteltest v0.19.0/go.mod h1:tI4yxwh8U21v7JD6R3BcA/2+RBoTKFexE/PJ/nSO7IA=
98+
go.opentelemetry.io/otel/oteltest v0.20.0 h1:HiITxCawalo5vQzdHfKeZurV8x7ljcqAgiWzF6Vaeaw=
8899
go.opentelemetry.io/otel/oteltest v0.20.0/go.mod h1:L7bgKf9ZB7qCwT9Up7i9/pn0PWIa9FqQ2IQ8LoxiGnw=
89100
go.opentelemetry.io/otel/trace v0.19.0/go.mod h1:4IXiNextNOpPnRlI4ryK69mn5iC84bjBWZQA5DXz/qg=
90101
go.opentelemetry.io/otel/trace v0.20.0 h1:1DL6EXUdcg95gukhuRRvLDO/4X5THh/5dIV52lqtnbw=
@@ -126,6 +137,7 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn
126137
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
127138
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
128139
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
140+
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
129141
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
130142
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
131143
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
@@ -147,6 +159,7 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
147159
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
148160
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
149161
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
162+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
150163
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
151164
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
152165
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@ package main
33
import (
44
"fmt"
55
fmt2 "github.com/ArtisanCloud/go-libs/fmt"
6+
"github.com/ArtisanCloud/go-libs/object"
67
"github.com/ArtisanCloud/go-wechat/src/work"
8+
"os"
79
)
810

11+
func GetConfig() *object.HashMap {
12+
return &object.HashMap{
13+
"corp_id": os.Getenv("corp_id"),
14+
"secret": os.Getenv("secret"),
15+
}
16+
}
17+
918
func main() {
1019

1120
fmt.Printf("hello Wechat! \n")

src/kernel/Encryptor.go

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
package kernel
2+
3+
import (
4+
"bytes"
5+
"crypto/aes"
6+
"crypto/sha1"
7+
"encoding/base64"
8+
"encoding/binary"
9+
"encoding/xml"
10+
"fmt"
11+
"github.com/ArtisanCloud/go-wechat/src/kernel/Support"
12+
"math/rand"
13+
"sort"
14+
"strings"
15+
)
16+
17+
// Wechat Docs: https://open.work.weixin.qq.com/api/doc/90000/90138/90307
18+
const (
19+
ErrorInvalidSignature = -40001 // 签名验证错误
20+
ErrorParseXml = -40002 // xml/json解析失败
21+
ErrorCalcSignature = -40003 // sha加密生成签名失败
22+
ErrorInvalidAesKey = -40004 // AESKey 非法
23+
ErrorInvalidAppId = -40005 // ReceiveId 校验错误
24+
ErrorEncryptAes = -40006 // AES 加密失败
25+
ErrorDecryptAes = -40007 // AES 解密失败
26+
ErrorInvalidXml = -40008 // 解密后得到的buffer非法
27+
ErrorBase64Encode = -40009 // base64 编码失败
28+
ErrorBase64Decode = -40010 // base64 解码失败
29+
ErrorXmlBuild = -40011 // 生成xml失败
30+
IllegalBuffer = -41003 // Illegal buffer
31+
letterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
32+
)
33+
34+
type CDATA struct {
35+
Value string `xml:",cdata"`
36+
}
37+
38+
type WeComReplyMsg struct {
39+
XMLName xml.Name `xml:"xml"`
40+
Encrypt CDATA `xml:"Encrypt"`
41+
Signature CDATA `xml:"MsgSignature"`
42+
Timestamp string `xml:"TimeStamp"`
43+
Nonce CDATA `xml:"Nonce"`
44+
}
45+
46+
type WeComRecvMsg struct {
47+
ToUserName string `xml:"ToUserName"`
48+
Encrypt string `xml:"Encrypt"`
49+
AgentId string `xml:"AgentID"`
50+
}
51+
52+
type Encryptor struct {
53+
appId string // App token
54+
token string
55+
aesKey []byte
56+
blockSize int
57+
aes *Support.AES
58+
}
59+
60+
func NewEncryptor(appId, token, aesKey string) (*Encryptor, error) {
61+
// TODO: 不明白为什么需要等号
62+
aesKey = aesKey + "="
63+
aesKeyByte, err := base64.StdEncoding.DecodeString(aesKey)
64+
if err != nil {
65+
return nil, err
66+
}
67+
return &Encryptor{
68+
appId: appId,
69+
token: token,
70+
aesKey: aesKeyByte,
71+
blockSize: 32,
72+
aes: &Support.AES{},
73+
}, nil
74+
}
75+
76+
// GetToken Get the app token
77+
func (encryptor *Encryptor) GetToken() string {
78+
return encryptor.token
79+
}
80+
81+
func (encryptor *Encryptor) randString(n int) string {
82+
b := make([]byte, n)
83+
for i := range b {
84+
b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))]
85+
}
86+
return string(b)
87+
}
88+
89+
// Encrypt encrypt xml msg and return xml
90+
func (encryptor *Encryptor) Encrypt(msg, nonce, timestamp string) ([]byte, *Support.CryptError) {
91+
92+
randStr := encryptor.randString(16)
93+
var buffer bytes.Buffer
94+
buffer.WriteString(randStr)
95+
96+
msgLenBuf := make([]byte, 4)
97+
binary.BigEndian.PutUint32(msgLenBuf, uint32(len(msg)))
98+
buffer.Write(msgLenBuf)
99+
buffer.WriteString(msg)
100+
101+
// TODO 暂时不知道用途
102+
//buffer.WriteString(wxBizMsgCrypt.receiverId)
103+
104+
tmpCiphertext, err := encryptor.aes.Encrypt(buffer.Bytes(), encryptor.aesKey, encryptor.aesKey[:aes.BlockSize])
105+
if err != nil {
106+
return nil, (*Support.CryptError)(err)
107+
}
108+
ciphertext := string(tmpCiphertext)
109+
110+
signature := encryptor.Signature(encryptor.token, timestamp, nonce, ciphertext)
111+
112+
msg4Send := &WeComReplyMsg{
113+
Encrypt: CDATA{Value: ciphertext},
114+
Signature: CDATA{Value: signature},
115+
Timestamp: timestamp,
116+
Nonce: CDATA{Value: nonce},
117+
}
118+
//msg4Send := NewWXBizMsg4Send(ciphertext, signature, timestamp, nonce)
119+
//msg.Marshal()
120+
xmlByte, err2 := xml.Marshal(msg4Send)
121+
if err2 != nil {
122+
return nil, Support.NewCryptError(ErrorXmlBuild, err2.Error())
123+
}
124+
return xmlByte, nil
125+
}
126+
127+
// Decrypt decrypt xml msg and return xml
128+
func (encryptor *Encryptor) Decrypt(content []byte, msgSignature, nonce, timestamp string) ([]byte, *Support.CryptError) {
129+
var msg4Recv WeComRecvMsg
130+
err := xml.Unmarshal(content, &msg4Recv)
131+
132+
if err != nil {
133+
return nil, Support.NewCryptError(ErrorDecryptAes, err.Error())
134+
}
135+
136+
signature := encryptor.Signature(encryptor.token, timestamp, nonce, msg4Recv.Encrypt)
137+
138+
if strings.Compare(signature, msgSignature) != 0 {
139+
return nil, Support.NewCryptError(ErrorCalcSignature, "signature not equal")
140+
}
141+
142+
iv := encryptor.aesKey[:aes.BlockSize]
143+
plaintext, cryptErr := encryptor.aes.Decrypt(msg4Recv.Encrypt, encryptor.aesKey, iv)
144+
//plaintext, cryptErr := wxBizMsgCrypt.cbcDecrypter(msg4Recv.Encrypt)
145+
if nil != cryptErr {
146+
return nil, cryptErr
147+
}
148+
149+
plaintext, cryptErr = encryptor.aes.PKCS7UnPadding(plaintext)
150+
if cryptErr != nil {
151+
return nil, cryptErr
152+
}
153+
154+
textLen := uint32(len(plaintext))
155+
if textLen < 20 {
156+
return nil, Support.NewCryptError(IllegalBuffer, "plain is to small 1")
157+
}
158+
//random := plaintext[:16]
159+
msgLen := binary.BigEndian.Uint32(plaintext[16:20])
160+
if textLen < (20 + msgLen) {
161+
return nil, Support.NewCryptError(IllegalBuffer, "plain is to small 2")
162+
}
163+
164+
msg := plaintext[20 : 20+msgLen]
165+
//receiverId := plaintext[20+msgLen:]
166+
167+
return msg, nil
168+
}
169+
170+
// VerifyUrl Parsing the official WeChat callback validation.
171+
// Wechat Docs: https://work.weixin.qq.com/api/doc/90000/90135/90235
172+
// When adding URLs to the WeChat admin backend, WeChat will trigger a GET request to verify whether the server can process the encrypted information properly, and the message needs to be decrypted and returned.
173+
// 在微信管理后台添加URL的时候,微信会触发一条GET请求用于验证服务器能否正常处理加密信息,需要将消息解密出来返回。
174+
// eg: "/app-callback?msg_signature=1495c4dfd4958d4e5faf618978ae66943a042f87&timestamp=1623292419&nonce=1623324060&echostr=o1XtmVltGmUAqoWee54yd4Q5ZBgrw4%2F9lFo5qdZoVPd1DybzarjuYCfFlR2AFbAcWHwFgmbrVBD%2Bf9910QIF6g%3D%3D"
175+
func (encryptor *Encryptor) VerifyUrl(content string, msgSignature, nonce, timestamp string) ([]byte, *Support.CryptError) {
176+
msg4Recv := &WeComRecvMsg{
177+
Encrypt: content,
178+
}
179+
msg4RecvByte, err := xml.Marshal(msg4Recv)
180+
if err != nil {
181+
return nil, Support.NewCryptError(ErrorXmlBuild, err.Error())
182+
}
183+
return encryptor.Decrypt(msg4RecvByte, msgSignature, nonce, timestamp)
184+
}
185+
186+
// Signature get sha1
187+
func (encryptor *Encryptor) Signature(token, timestamp, nonce, data string) string {
188+
sortArr := []string{token, timestamp, nonce, data}
189+
sort.Strings(sortArr)
190+
var buffer bytes.Buffer
191+
for _, value := range sortArr {
192+
buffer.WriteString(value)
193+
}
194+
195+
sha := sha1.New()
196+
sha.Write(buffer.Bytes())
197+
signature := fmt.Sprintf("%x", sha.Sum(nil))
198+
return signature
199+
}

0 commit comments

Comments
 (0)