Skip to content

Commit e2a5c1d

Browse files
committed
修复缺陷 && 优化命名
1 parent 7a38096 commit e2a5c1d

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

decrypt.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ type UserInfo struct {
110110
// iv 加密算法的初始向量
111111
func (cli *Client) DecryptUserInfo(sessionKey, rawData, encryptedData, signature, iv string) (*UserInfo, error) {
112112

113-
if encrypt.NewSigner(false, rawData, sessionKey).CompareWith(signature) {
113+
if !encrypt.NewSignable(false, rawData, sessionKey).IsEqual(signature) {
114114
return nil, errors.New("failed to validate signature")
115115
}
116116

encrypt/sign.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,27 @@ import (
77
"strings"
88
)
99

10-
type Signer struct {
10+
type Signable struct {
1111
// 是否需要字典排序
1212
sort bool
1313
// 需要签名的内容
1414
parts []string
1515
}
1616

17-
func NewSigner(sort bool, parts ...string) *Signer {
18-
return &Signer{
17+
func NewSignable(sort bool, parts ...string) *Signable {
18+
return &Signable{
1919
sort: sort,
2020
parts: parts,
2121
}
2222
}
2323

2424
// 对比签名
25-
func (sign *Signer) CompareWith(signature string) bool {
25+
func (sign *Signable) IsEqual(signature string) bool {
2626
return signature == sign.Sign()
2727
}
2828

2929
// 生成签名
30-
func (sign *Signer) Sign() string {
30+
func (sign *Signable) Sign() string {
3131

3232
if sign.sort {
3333
sort.Strings(sign.parts)

server/server.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func (srv *Server) handleRequest(w http.ResponseWriter, r *http.Request, isEncrp
272272
nonce, signature, timestamp := query.Get("nonce"), query.Get("signature"), query.Get("timestamp")
273273

274274
// 检验消息是否来自微信服务器
275-
if encrypt.NewSigner(true, srv.token, timestamp, nonce).CompareWith(signature) {
275+
if !encrypt.NewSignable(true, srv.token, timestamp, nonce).IsEqual(signature) {
276276
return nil, errors.New("failed to validate signature")
277277
}
278278

@@ -658,7 +658,7 @@ func (srv *Server) validateServer(req *http.Request) bool {
658658
signature := query.Get("signature")
659659
timestamp := query.Get("timestamp")
660660

661-
return encrypt.NewSigner(true, nonce, timestamp, srv.token).CompareWith(signature)
661+
return encrypt.NewSignable(true, nonce, timestamp, srv.token).IsEqual(signature)
662662
}
663663

664664
// 加密消息
@@ -679,7 +679,7 @@ func (srv *Server) encryptMsg(message string, timestamp int64) (*EncryptedMsgReq
679679
timestr := strconv.FormatInt(timestamp, 10)
680680

681681
//生成安全签名
682-
signature := encrypt.NewSigner(true, srv.token, timestr, nonce, cipher).Sign()
682+
signature := encrypt.NewSignable(true, srv.token, timestr, nonce, cipher).Sign()
683683
request := EncryptedMsgRequest{
684684
Nonce: nonce,
685685
Encrypt: cipher,

0 commit comments

Comments
 (0)