From ccc50a7dd069ebe511312ec89b279dc9f25c47e6 Mon Sep 17 00:00:00 2001 From: ayflying Date: Tue, 29 Jul 2025 10:51:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8D=A3=E8=80=80=E6=94=AF=E4=BB=98=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E9=AA=8C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package/pay/func.go | 30 ++++++++++++++++++++++++++++++ package/pay/honor/honor.go | 11 ++++++++--- 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 package/pay/func.go diff --git a/package/pay/func.go b/package/pay/func.go new file mode 100644 index 0000000..719de75 --- /dev/null +++ b/package/pay/func.go @@ -0,0 +1,30 @@ +package pay + +import "strings" + +func FormatPublicKey(publicKey string) (pKey string) { + var buffer strings.Builder + buffer.WriteString("-----BEGIN PUBLIC KEY-----\n") + rawLen := 64 + keyLen := len(publicKey) + raws := keyLen / rawLen + temp := keyLen % rawLen + if temp > 0 { + raws++ + } + start := 0 + end := start + rawLen + for i := 0; i < raws; i++ { + if i == raws-1 { + buffer.WriteString(publicKey[start:]) + } else { + buffer.WriteString(publicKey[start:end]) + } + buffer.WriteByte('\n') + start += rawLen + end = start + rawLen + } + buffer.WriteString("-----END PUBLIC KEY-----\n") + pKey = buffer.String() + return +} diff --git a/package/pay/honor/honor.go b/package/pay/honor/honor.go index 0dddac9..36dff37 100644 --- a/package/pay/honor/honor.go +++ b/package/pay/honor/honor.go @@ -8,6 +8,7 @@ import ( "encoding/base64" "encoding/pem" "errors" + "github.com/ayflying/utility_go/package/pay" ) type Pay struct { @@ -15,8 +16,11 @@ type Pay struct { AppId string `json:"appId"` } -func New() *Pay { - return &Pay{} +func New(pay *Pay) *Pay { + return &Pay{ + AppId: pay.AppId, + PubKey: pay.PubKey, + } } // VerifyRSASignature 验证RSA数字签名 @@ -31,8 +35,9 @@ func (p *Pay) VerifyRSASignature(data []byte, sign string) (bool, error) { return false, errors.New("签名解码失败: " + err.Error()) } + pubkey := pay.FormatPublicKey(p.PubKey) // 解析PEM格式的公钥 - block, _ := pem.Decode([]byte(p.PubKey)) + block, _ := pem.Decode([]byte(pubkey)) if block == nil { return false, errors.New("无效的PEM格式公钥") }