Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
feat: optimized functions
Browse files Browse the repository at this point in the history
  • Loading branch information
AH-dark committed Nov 26, 2023
1 parent bb38070 commit 58ad212
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 50 deletions.
7 changes: 4 additions & 3 deletions actions/implements/mapi/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"),
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion actions/implements/migrate/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
2 changes: 1 addition & 1 deletion database/conn/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down Expand Up @@ -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=
Expand Down
File renamed without changes.
33 changes: 32 additions & 1 deletion pkg/epay/utils.go
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 3 additions & 2 deletions pkg/utils/epay_test.go → pkg/epay/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils
package epay

import (
"github.com/AH-dark/bytestring"
"github.com/stretchr/testify/assert"
"testing"
)
Expand All @@ -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")))
}
10 changes: 7 additions & 3 deletions pkg/utils/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
32 changes: 0 additions & 32 deletions pkg/utils/epay.go

This file was deleted.

0 comments on commit 58ad212

Please sign in to comment.