优化签名规则

This commit is contained in:
Yun
2025-10-12 18:41:05 +08:00
parent e26d03a35a
commit 70e32e7d8e
9 changed files with 426 additions and 44 deletions
+22 -14
View File
@@ -2,28 +2,39 @@
package bopx
import (
"context"
"crypto/md5"
"fmt"
"sort"
"strings"
"code.yun.ink/pkg/convx"
"github.com/yuninks/loggerx"
)
type platform struct {
ingoreKey []string
signKey string
ingoreKeys []string
logger loggerx.LoggerInterface
}
func NewPlatform(ignoreKey []string, signKey string) *platform {
func NewPlatform(opts ...Option) *platform {
options := newOptions(opts...)
return &platform{
ingoreKey: ignoreKey,
signKey: signKey,
ingoreKeys: options.ignoreKeys,
logger: options.logger,
}
}
func (l *platform) GetSign(params map[string]interface{}) (sign string, err error) {
for _, val := range l.ingoreKey {
func (l *platform) GetSign(ctx context.Context, signKey string, params map[string]any) (sign string, err error) {
if signKey == "" {
return "", ErrorSignKeyIsEmpty
}
if len(params) == 0 {
return "", ErrorSignParamIsEmpty
}
for _, val := range l.ingoreKeys {
delete(params, val)
}
@@ -40,13 +51,10 @@ func (l *platform) GetSign(params map[string]interface{}) (sign string, err erro
if k > 0 {
str = str + "&"
}
val, err := convx.ToString(params[v])
if err != nil {
return "", err
}
val := convx.ToString(params[v])
str = str + v + "=" + val
}
str = str + "&key=" + l.signKey
str = str + "&key=" + signKey
data := []byte(str)
has := md5.Sum(data)
md5str1 := fmt.Sprintf("%x", has) // []byte转16进制
@@ -54,7 +62,7 @@ func (l *platform) GetSign(params map[string]interface{}) (sign string, err erro
return sign, nil
}
func (l *platform) VerifySign(params map[string]interface{}) error {
func (l *platform) VerifySign(ctx context.Context, signKey string, params map[string]interface{}) error {
sign, ok := params["sign"]
if !ok {
return ErrorSignParamNotExist
@@ -63,7 +71,7 @@ func (l *platform) VerifySign(params map[string]interface{}) error {
if !ok {
return ErrorSignMustBeString
}
g_sign, err := l.GetSign(params)
g_sign, err := l.GetSign(ctx, signKey, params)
if err != nil {
return err
}