From 58ad2123c9d0270aa76612250a80a3f1093b0cef Mon Sep 17 00:00:00 2001 From: AH-dark Date: Sun, 26 Nov 2023 18:38:23 +0800 Subject: [PATCH] feat: optimized functions --- actions/implements/mapi/service.go | 7 ++-- actions/implements/migrate/service.go | 2 +- database/conn/db.go | 2 +- go.mod | 3 +- go.sum | 10 +++--- {pkg => internal}/log/logger.go | 0 pkg/epay/utils.go | 33 ++++++++++++++++++- .../epay_test.go => epay/utils_test.go} | 5 +-- pkg/utils/crypto.go | 10 ++++-- pkg/utils/epay.go | 32 ------------------ 10 files changed, 54 insertions(+), 50 deletions(-) rename {pkg => internal}/log/logger.go (100%) rename pkg/{utils/epay_test.go => epay/utils_test.go} (80%) delete mode 100644 pkg/utils/epay.go diff --git a/actions/implements/mapi/service.go b/actions/implements/mapi/service.go index ae3e626..efd9aec 100644 --- a/actions/implements/mapi/service.go +++ b/actions/implements/mapi/service.go @@ -6,6 +6,7 @@ import ( "github.com/urfave/cli/v2" + "github.com/AH-dark/bytestring" "github.com/AH-dark/epay-cli/actions/factory" "github.com/AH-dark/epay-cli/pkg/epay" "github.com/AH-dark/epay-cli/pkg/utils" @@ -18,8 +19,8 @@ func NewService() factory.ActionService { return &service{} } -func (svc *service) getSign(c *cli.Context) string { - sign := utils.CalculateEPaySign(map[string]string{ +func (svc *service) getSign(c *cli.Context) []byte { + sign := epay.CalculateEPaySign(map[string]string{ "pid": strconv.Itoa(c.Int("pid")), "type": c.String("type"), "out_trade_no": c.String("trade-no"), @@ -58,7 +59,7 @@ func (svc *service) Do(c *cli.Context) error { ClientIP: c.String("client-ip"), Device: utils.EmptyPtr(epay.DeviceType(c.String("device"))), Param: utils.EmptyPtr(c.String("param")), - Sign: sign, + Sign: bytestring.BytesToString(sign), SignType: "MD5", }) if err != nil { diff --git a/actions/implements/migrate/service.go b/actions/implements/migrate/service.go index 5b7b09e..206f48c 100644 --- a/actions/implements/migrate/service.go +++ b/actions/implements/migrate/service.go @@ -13,7 +13,7 @@ import ( "github.com/AH-dark/epay-cli/actions/factory" "github.com/AH-dark/epay-cli/database/conn" "github.com/AH-dark/epay-cli/database/model" - "github.com/AH-dark/epay-cli/pkg/log" + "github.com/AH-dark/epay-cli/internal/log" "github.com/AH-dark/epay-cli/pkg/utils" ) diff --git a/database/conn/db.go b/database/conn/db.go index dd81ba7..742b55d 100644 --- a/database/conn/db.go +++ b/database/conn/db.go @@ -11,7 +11,7 @@ import ( "gorm.io/gorm" "gorm.io/gorm/schema" - "github.com/AH-dark/epay-cli/pkg/log" + "github.com/AH-dark/epay-cli/internal/log" ) var ( diff --git a/go.mod b/go.mod index 62fd980..a6856aa 100644 --- a/go.mod +++ b/go.mod @@ -3,13 +3,14 @@ module github.com/AH-dark/epay-cli go 1.21 require ( + github.com/AH-dark/bytestring v1.0.0 github.com/imroc/req/v3 v3.42.2 github.com/mitchellh/mapstructure v1.5.0 github.com/onrik/gorm-logrus v0.5.0 github.com/samber/lo v1.38.1 github.com/shopspring/decimal v1.3.1 github.com/sirupsen/logrus v1.9.3 - github.com/stretchr/testify v1.8.1 + github.com/stretchr/testify v1.8.4 github.com/urfave/cli/v2 v2.25.7 gorm.io/driver/mysql v1.5.1 gorm.io/driver/postgres v1.5.4 diff --git a/go.sum b/go.sum index cbda6ff..b7882e0 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/AH-dark/bytestring v1.0.0 h1:q3rHtKds9s0M+5voIy3Cd4IlakjevOYnwO+l2mBzqWo= +github.com/AH-dark/bytestring v1.0.0/go.mod h1:0lWDSOCEAEmcygppUji0iHf8qyXo77gk25Bgd//RySA= github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs= @@ -78,15 +80,11 @@ github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFR github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= -github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs= github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= diff --git a/pkg/log/logger.go b/internal/log/logger.go similarity index 100% rename from pkg/log/logger.go rename to internal/log/logger.go diff --git a/pkg/epay/utils.go b/pkg/epay/utils.go index 75e1934..38c4beb 100644 --- a/pkg/epay/utils.go +++ b/pkg/epay/utils.go @@ -1,12 +1,43 @@ package epay import ( + "crypto/subtle" + "fmt" + "sort" + "strings" + + "github.com/AH-dark/bytestring" + "github.com/samber/lo" + "github.com/AH-dark/epay-cli/pkg/utils" ) // GenerateParams 生成加签参数 func GenerateParams(params map[string]string, secret string) map[string]string { - params["sign"] = utils.CalculateEPaySign(params, secret) + params["sign"] = bytestring.BytesToString(CalculateEPaySign(params, secret)) params["sign_type"] = "MD5" return params } + +func CalculateEPaySign(mapData map[string]string, secret string) []byte { + // sort keys + keys := lo.Keys(mapData) + sort.Strings(keys) + + combinedData := "" + for _, k := range keys { + if k == "sign" || k == "sign_type" || lo.IsEmpty(mapData[k]) { + continue + } + + combinedData += fmt.Sprintf("%s=%s&", k, mapData[k]) + } + + combinedData = strings.TrimSuffix(combinedData, "&") + + return utils.MD5(combinedData + secret) +} + +func CheckEPaySign(mapData map[string]string, secret string, sign []byte) bool { + return subtle.ConstantTimeCompare(CalculateEPaySign(mapData, secret), sign) == 1 +} diff --git a/pkg/utils/epay_test.go b/pkg/epay/utils_test.go similarity index 80% rename from pkg/utils/epay_test.go rename to pkg/epay/utils_test.go index 5363f05..571f161 100644 --- a/pkg/utils/epay_test.go +++ b/pkg/epay/utils_test.go @@ -1,6 +1,7 @@ -package utils +package epay import ( + "github.com/AH-dark/bytestring" "github.com/stretchr/testify/assert" "testing" ) @@ -23,5 +24,5 @@ func TestCalculateEPaySign(t *testing.T) { "sign_type": "md5", } - asserts.Equal(testData["sign"], CalculateEPaySign(testData, "123456")) + asserts.Equal(testData["sign"], bytestring.BytesToString(CalculateEPaySign(testData, "123456"))) } diff --git a/pkg/utils/crypto.go b/pkg/utils/crypto.go index 120b314..71db8d7 100644 --- a/pkg/utils/crypto.go +++ b/pkg/utils/crypto.go @@ -2,15 +2,19 @@ package utils import ( "crypto/md5" - "fmt" + "encoding/hex" + + "github.com/AH-dark/bytestring" ) func MD5(str string) []byte { m := md5.New() m.Write([]byte(str)) - return m.Sum(nil) + dst := make([]byte, 32) + hex.Encode(dst, m.Sum(nil)) + return dst } func MD5String(str string) string { - return fmt.Sprintf("%x", MD5(str)) + return bytestring.BytesToString(MD5(str)) } diff --git a/pkg/utils/epay.go b/pkg/utils/epay.go deleted file mode 100644 index 9c04ec6..0000000 --- a/pkg/utils/epay.go +++ /dev/null @@ -1,32 +0,0 @@ -package utils - -import ( - "fmt" - "sort" - "strings" - - "github.com/samber/lo" -) - -func CalculateEPaySign(mapData map[string]string, secret string) string { - // sort keys - keys := lo.Keys(mapData) - sort.Strings(keys) - - combinedData := "" - for _, k := range keys { - if k == "sign" || k == "sign_type" || lo.IsEmpty(mapData[k]) { - continue - } - - combinedData += fmt.Sprintf("%s=%s&", k, mapData[k]) - } - - combinedData = strings.TrimSuffix(combinedData, "&") - - return MD5String(combinedData + secret) -} - -func CheckEPaySign(mapData map[string]string, secret string, sign string) bool { - return CalculateEPaySign(mapData, secret) == sign -}