荣耀支付增加验单

This commit is contained in:
ayflying
2025-07-29 10:51:42 +08:00
parent d1a7ba8119
commit ccc50a7dd0
2 changed files with 38 additions and 3 deletions

30
package/pay/func.go Normal file
View File

@@ -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
}

View File

@@ -8,6 +8,7 @@ import (
"encoding/base64" "encoding/base64"
"encoding/pem" "encoding/pem"
"errors" "errors"
"github.com/ayflying/utility_go/package/pay"
) )
type Pay struct { type Pay struct {
@@ -15,8 +16,11 @@ type Pay struct {
AppId string `json:"appId"` AppId string `json:"appId"`
} }
func New() *Pay { func New(pay *Pay) *Pay {
return &Pay{} return &Pay{
AppId: pay.AppId,
PubKey: pay.PubKey,
}
} }
// VerifyRSASignature 验证RSA数字签名 // VerifyRSASignature 验证RSA数字签名
@@ -31,8 +35,9 @@ func (p *Pay) VerifyRSASignature(data []byte, sign string) (bool, error) {
return false, errors.New("签名解码失败: " + err.Error()) return false, errors.New("签名解码失败: " + err.Error())
} }
pubkey := pay.FormatPublicKey(p.PubKey)
// 解析PEM格式的公钥 // 解析PEM格式的公钥
block, _ := pem.Decode([]byte(p.PubKey)) block, _ := pem.Decode([]byte(pubkey))
if block == nil { if block == nil {
return false, errors.New("无效的PEM格式公钥") return false, errors.New("无效的PEM格式公钥")
} }