Skip to content

Commit 76c8995

Browse files
authored
Merge pull request #612 from ArtisanCloud/develop
Develop
2 parents f7ca472 + 1bd6680 commit 76c8995

File tree

3 files changed

+15
-25
lines changed

3 files changed

+15
-25
lines changed

src/kernel/support/signer.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import (
99
"crypto/sha256"
1010
"crypto/x509"
1111
"encoding/base64"
12+
"encoding/hex"
1213
"encoding/pem"
1314
"errors"
1415
"fmt"
15-
"github.com/ArtisanCloud/PowerLibs/v3/object"
1616
"os"
1717
"strings"
1818
"time"
19+
20+
"github.com/ArtisanCloud/PowerLibs/v3/object"
1921
)
2022

2123
// 请求报文签名相关常量
@@ -189,9 +191,9 @@ func SignSHA256WithRSA(source string, privateKey *rsa.PrivateKey) (signature str
189191
return base64.StdEncoding.EncodeToString(signatureByte), nil
190192
}
191193

192-
func SignSHA256WithHMac(sessionKey []byte, input string) ([]byte, error) {
194+
func SignSHA256WithHMac(sessionKey []byte, input string) (string, error) {
193195
if len(sessionKey) == 0 {
194-
return nil, errors.New("session key is empty")
196+
return "", errors.New("session key is empty")
195197
}
196198

197199
// Create a new HMAC SHA256 object
@@ -200,11 +202,11 @@ func SignSHA256WithHMac(sessionKey []byte, input string) ([]byte, error) {
200202
// Write the input string to the HMAC object
201203
inputBytes := []byte(input)
202204
if _, err := hmac.Write(inputBytes); err != nil {
203-
return nil, err
205+
return "", err
204206
}
205207

206208
// Get the HMAC signature
207209
signature := hmac.Sum(nil)
208210

209-
return signature, nil
211+
return hex.EncodeToString(signature), nil
210212
}

src/kernel/support/signer_test.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import (
55
"crypto/rsa"
66
"crypto/sha256"
77
"crypto/x509"
8+
"encoding/hex"
89
"encoding/pem"
910
"errors"
1011
"fmt"
11-
"github.com/go-playground/assert/v2"
1212
"strings"
1313
"testing"
14+
15+
"github.com/go-playground/assert/v2"
1416
)
1517

1618
const (
@@ -109,19 +111,6 @@ func TestSha256WithRsa(t *testing.T) {
109111
}
110112
}
111113

112-
// 辅助函数:比较两个字节切片是否相等
113-
func compareByteSlices(a, b []byte) bool {
114-
if len(a) != len(b) {
115-
return false
116-
}
117-
for i, v := range a {
118-
if v != b[i] {
119-
return false
120-
}
121-
}
122-
return true
123-
}
124-
125114
// 测试函数
126115
func TestSignSHA256WithHMac(t *testing.T) {
127116
testCases := []struct {
@@ -173,7 +162,7 @@ func TestSignSHA256WithHMac(t *testing.T) {
173162
// 1. 计算预期结果
174163
expectedHmac := hmac.New(sha256.New, tc.sessionKey)
175164
expectedHmac.Write([]byte(tc.input))
176-
expectedSig := expectedHmac.Sum(nil)
165+
expectedSig := hex.EncodeToString(expectedHmac.Sum(nil))
177166

178167
// 2. 调用 SignSHA256WithHMac 函数
179168
sig, err := SignSHA256WithHMac(tc.sessionKey, tc.input)
@@ -188,7 +177,7 @@ func TestSignSHA256WithHMac(t *testing.T) {
188177
} else {
189178
if err != nil {
190179
t.Errorf("Unexpected error: %v", err)
191-
} else if !compareByteSlices(sig, expectedSig) {
180+
} else if sig != expectedSig {
192181
t.Errorf("Signature mismatch. Expected: %x, Got: %x", expectedSig, sig)
193182
}
194183
}

src/miniProgram/auth/client.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package auth
22

33
import (
44
"context"
5+
56
"github.com/ArtisanCloud/PowerLibs/v3/object"
67
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
78
response2 "github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/response"
@@ -57,9 +58,8 @@ func (comp *Client) CheckSession(ctx context.Context, openId string, sessionKey
5758
}
5859
params := &object.StringMap{
5960
"appid": config.GetString("app_id", ""),
60-
"secret": config.GetString("secret", ""),
6161
"openid": openId,
62-
"signature": string(sign),
62+
"signature": sign,
6363
"sig_method": "hmac_sha256",
6464
}
6565

@@ -82,9 +82,8 @@ func (comp *Client) ResetUserSessionKey(ctx context.Context, openId string, sess
8282
}
8383
params := &object.StringMap{
8484
"appid": config.GetString("app_id", ""),
85-
"secret": config.GetString("secret", ""),
8685
"openid": openId,
87-
"signature": string(sign),
86+
"signature": sign,
8887
"sig_method": "hmac_sha256",
8988
}
9089

0 commit comments

Comments
 (0)