diff --git a/go.mod b/go.mod index ceb4463..eb081ba 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module plugins go 1.22.2 -require gitea.cdlsxd.cn/BaseSystem/plugin v0.0.0-20240731020946-65ddbab65854 +require gitea.cdlsxd.cn/BaseSystem/plugin v0.0.0-20240821033242-d9c3e7ad88f6 require ( github.com/fatih/color v1.7.0 // indirect @@ -20,7 +20,6 @@ require ( github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77 // indirect github.com/oklog/run v1.0.0 // indirect github.com/pkg/errors v0.9.1 // indirect - github.com/wechatpay-apiv3/wechatpay-go v0.2.20 // indirect golang.org/x/crypto v0.26.0 // indirect golang.org/x/net v0.22.0 // indirect golang.org/x/sys v0.23.0 // indirect diff --git a/go.sum b/go.sum index e8e7bd2..7a7267b 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,9 @@ gitea.cdlsxd.cn/BaseSystem/plugin v0.0.0-20240731020946-65ddbab65854 h1:+lG+QB1dfA70NSF35pN3qZCxfIJ0n+1JMS02gM0+vzI= gitea.cdlsxd.cn/BaseSystem/plugin v0.0.0-20240731020946-65ddbab65854/go.mod h1:59zYRFcPur2HizJEIfttVOMY0AscollDYKX0yvPJS7k= -github.com/agiledragon/gomonkey v2.0.2+incompatible/go.mod h1:2NGfXu1a80LLr2cmWXGBDaHEjb1idR6+FVlX5T3D9hw= +gitea.cdlsxd.cn/BaseSystem/plugin v0.0.0-20240821033242-d9c3e7ad88f6 h1:L+z3IjcLEbj9UJAQCZv1YA1HqIrZZxBlZ9I4tQF6dXY= +gitea.cdlsxd.cn/BaseSystem/plugin v0.0.0-20240821033242-d9c3e7ad88f6/go.mod h1:59zYRFcPur2HizJEIfttVOMY0AscollDYKX0yvPJS7k= github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA= github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= @@ -45,17 +45,9 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -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.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -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/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/wechatpay-apiv3/wechatpay-go v0.2.20 h1:gS8oFn1bHGnyapR2Zb4aqTV6l4kJWgbtqjCq6k1L9DQ= -github.com/wechatpay-apiv3/wechatpay-go v0.2.20/go.mod h1:A254AUBVB6R+EqQFo3yTgeh7HtyqRRtN2w9hQSOrd4Q= golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc= @@ -72,7 +64,5 @@ google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY= google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/plugins/union_pay_cpn/go.mod b/plugins/union_pay_cpn/go.mod index 9c88075..9796977 100644 --- a/plugins/union_pay_cpn/go.mod +++ b/plugins/union_pay_cpn/go.mod @@ -2,13 +2,15 @@ module plugins/union_pay_cpn go 1.22.2 +replace plugins/utils => ../../utils + require ( gitea.cdlsxd.cn/BaseSystem/plugin v0.0.0-20240821033242-d9c3e7ad88f6 github.com/carlmjohnson/requests v0.23.4 github.com/go-playground/validator/v10 v10.22.0 github.com/hashicorp/go-plugin v1.6.1 github.com/stretchr/testify v1.9.0 - github.com/tjfoc/gmsm v1.4.1 + plugins/utils v0.0.0-00010101000000-000000000000 ) require ( @@ -26,6 +28,7 @@ require ( github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77 // indirect github.com/oklog/run v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/tjfoc/gmsm v1.4.1 // indirect golang.org/x/crypto v0.21.0 // indirect golang.org/x/net v0.22.0 // indirect golang.org/x/sys v0.18.0 // indirect diff --git a/plugins/union_pay_cpn/internal/transform.go b/plugins/union_pay_cpn/internal/transform.go index 91eddd3..2506091 100644 --- a/plugins/union_pay_cpn/internal/transform.go +++ b/plugins/union_pay_cpn/internal/transform.go @@ -4,8 +4,8 @@ import ( "encoding/json" "gitea.cdlsxd.cn/BaseSystem/plugin/proto" "plugins/union_pay_cpn/internal/po" - "plugins/union_pay_cpn/internal/utils" "plugins/union_pay_cpn/internal/vo" + "plugins/utils/union_pay" "strings" ) @@ -36,7 +36,7 @@ func (c *Config) orderReq(in *proto.OrderRequest) (*po.OrderReq, error) { var e OrderExtra _ = json.Unmarshal(in.Order.Extra, &e) - mobile, err := utils.Encrypt([]byte(in.Order.Account), []byte(c.KEY), []byte(c.IV)) + mobile, err := union_pay.Encrypt([]byte(in.Order.Account), []byte(c.KEY), []byte(c.IV)) if err != nil { return nil, err } @@ -104,11 +104,11 @@ func notifyReq(in *proto.NotifyRequest) *po.Notify { _ = json.Unmarshal(in.Headers, &h) var body po.Body _ = json.NewDecoder(strings.NewReader(string(in.Body))).Decode(&body) - body.CouponNum = utils.ConvertToInt(body.CouponNum) - body.OrderAt = utils.ConvertToInt(body.OrderAt) - body.DiscountAt = utils.ConvertToInt(body.DiscountAt) + body.CouponNum = union_pay.ConvertToInt(body.CouponNum) + body.OrderAt = union_pay.ConvertToInt(body.OrderAt) + body.DiscountAt = union_pay.ConvertToInt(body.DiscountAt) for i, cdInfo := range body.CouponCdInfos { - cdInfo.SubAcctOperAt = utils.ConvertToInt(cdInfo.SubAcctOperAt) + cdInfo.SubAcctOperAt = union_pay.ConvertToInt(cdInfo.SubAcctOperAt) body.CouponCdInfos[i] = cdInfo } return &po.Notify{ diff --git a/plugins/union_pay_cpn/internal/util.go b/plugins/union_pay_cpn/internal/util.go index a220710..6f60b21 100644 --- a/plugins/union_pay_cpn/internal/util.go +++ b/plugins/union_pay_cpn/internal/util.go @@ -5,8 +5,8 @@ import ( sdkutils "gitea.cdlsxd.cn/BaseSystem/plugin/utils" "net/http" "plugins/union_pay_cpn/internal/po" - "plugins/union_pay_cpn/internal/utils" "plugins/union_pay_cpn/internal/vo" + "plugins/utils/union_pay" "strings" "time" ) @@ -27,8 +27,8 @@ func headers(config *Config, req po.Req, bizMethod string) map[string][]string { milliseconds := now.Unix()*1000 + int64(now.Nanosecond())/1e6 h.Add("reqTs", fmt.Sprintf("%d", milliseconds)) - rehash := utils.Sha(vo.Version, config.AppId, bizMethod, req.GetReId(), string(req.ToJson())) - signValue, err := utils.Sign(rehash, []byte(sdkutils.NewPrivate().Build(config.Prk))) + rehash := union_pay.Sha(vo.Version, config.AppId, bizMethod, req.GetReId(), string(req.ToJson())) + signValue, err := union_pay.Sign(rehash, []byte(sdkutils.NewPrivate().Build(config.Prk))) if err != nil { return nil } @@ -46,9 +46,9 @@ func verify(config *Config, req *po.Notify, notifyBizMethod string) error { if req.Headers.BizMethod != notifyBizMethod { return fmt.Errorf("业务方法不匹配") } - rehash := utils.Sha(req.Headers.Version, config.AppId, req.Headers.BizMethod, req.GetReId(), string(req.ToJson())) + rehash := union_pay.Sha(req.Headers.Version, config.AppId, req.Headers.BizMethod, req.GetReId(), string(req.ToJson())) lowerStr := strings.ToLower(rehash) - if utils.Verify(lowerStr, req.Headers.Sign, []byte(sdkutils.NewPublic().Build(config.Npk))) { + if union_pay.Verify(lowerStr, req.Headers.Sign, []byte(sdkutils.NewPublic().Build(config.Npk))) { return nil } return fmt.Errorf("验签失败") diff --git a/plugins/union_pay_cpn/internal/utils/sign.go b/plugins/union_pay_cpn/internal/utils/sign.go deleted file mode 100644 index 324663a..0000000 --- a/plugins/union_pay_cpn/internal/utils/sign.go +++ /dev/null @@ -1,68 +0,0 @@ -package utils - -import ( - "crypto" - "crypto/rand" - "crypto/rsa" - "crypto/sha256" - "crypto/x509" - "encoding/base64" - "encoding/pem" - "errors" - "fmt" -) - -func Sign(data string, privateKeyPEM []byte) (string, error) { - block, _ := pem.Decode(privateKeyPEM) - if block == nil { - return "", errors.New("failed to parse PEM block containing the private key") - } - - privyKey, err := x509.ParsePKCS1PrivateKey(block.Bytes) - if err != nil { - return "", fmt.Errorf("failed to parse DER encoded private key: %v", err) - } - - hashed := sha256.Sum256([]byte(data)) - signature, err := rsa.SignPKCS1v15(rand.Reader, privyKey, crypto.SHA256, hashed[:]) - if err != nil { - return "", fmt.Errorf("failed to sign: %v", err) - } - - return base64.StdEncoding.EncodeToString(signature), nil -} - -func Verify(data, signature string, publicKeyPEM []byte) bool { - block, _ := pem.Decode(publicKeyPEM) - if block == nil { - fmt.Println("failed to parse PEM block containing the public key") - return false - } - - pubKey, err := x509.ParsePKIXPublicKey(block.Bytes) - if err != nil { - fmt.Println("error parsing public key:", err) - return false - } - - rsaPubKey, ok := pubKey.(*rsa.PublicKey) - if !ok { - fmt.Println("not an RSA public key") - return false - } - - hashed := sha256.Sum256([]byte(data)) - sig, err := base64.StdEncoding.DecodeString(signature) - if err != nil { - fmt.Println("error decoding signature:", err) - return false - } - - err = rsa.VerifyPKCS1v15(rsaPubKey, crypto.SHA256, hashed[:], sig) - if err != nil { - fmt.Println("signature verification error:", err) - return false - } - - return true -} diff --git a/plugins/union_pay_cpn/internal/utils/sign_test.go b/plugins/union_pay_cpn/internal/utils/sign_test.go deleted file mode 100644 index 53879d0..0000000 --- a/plugins/union_pay_cpn/internal/utils/sign_test.go +++ /dev/null @@ -1,23 +0,0 @@ -package utils - -import ( - sdkutils "gitea.cdlsxd.cn/BaseSystem/plugin/utils" - "testing" -) - -var privateKeyPEM = sdkutils.NewPrivate().Build("MIIEogIBAAKCAQEAllAo3AbVfNg1iDT3CisgueIm2Pya2C6qatF0S8/aAKW8A6HBIuvrk1sb2TjbI0UDVowaNDsPa7nNdZHkQa6CQKf2BwFRy7gB8vAD1YSq23NTwplGe5IpuRe2Cbljh5diQ/+0zwZPZQEvjSMqZJILYVluGBw656JSndRdXHCs608CBib14l35A6D7rSZNd53nM9yXj/WOpLuY/jX0sbKJrBUIhcQcMiCFK/Nx06vhhCGZSWwaj0TTSMEOj9nyU+56MMx+N7527JUnOYMyVbbEx5h1S5sPjntMVVzz8c8FC5MKbgXCgjTt5Depl6YgrvoSCNNXZqnXN2l9dtBxKGSEdQIDAQABAoIBAD7r606/pZqPP0l3MnqVNyvY2X43r/ITBs+UQmSB67Ydqqiyi5C8xW530x6JSGJpP055c3atynTD4Jf4rF46WNgL91dTXBQ1QMubYV9+G6+lhUiOtQyBUOnkXRWQ/3MBEed5IQ0QjSew6WDheZuD7zOfJhD9sELKll7vSO2rVRdi5Kf3pps0K4aeYLvlelHjPtu2EkMyAD4d37QO2q8A/+acOf7wyx1uZ55nobe0jVgQBa2Tuuwhd2JilmsgMvcVxeAJoocyQxPVTU+iMidgZKxyW/fJUaR7V2KgKfP0H2OZbSruD2HyHL7o70+/XBg250msh691oTCXSGWiITFXRDkCgYEA1KF3daSzWK2vTZeNMxj++VRNZfyigNqBwgRZxrSGIUeNGpDdDp7flldM/fO/0RijKnh+nXLBCMZlxffSI7W3jkkjosEq0uyXv6Ez4WlhwHEAE6KrZYQ8VDU3We1LXERu/3SMU16Uq2Vme8J6kpshr7OLGeuTNfsSG79lpHU/OO8CgYEAtPjF/AY/t85u4JV0w6FJ/K7Iya+AyIyZhsgnxsPv5NwVtNsQzTRbXlpgnVmaKUXzLBTBJA2P6Gie35R6kJuOuMnvHjtG7umI5lOUJaqYk5GZEXHxbIKTXUxAbv+WKRHcFwcH54Ppurs7bN6ultWdE+yyU5PvabFRAidL+rV+MNsCgYBkqx2XwSZ4MaLxpXLgYlE1UkhipL17K/iLFuw6O3XVg762eSdvqKBK2YJGKYUDRhraHLo1aQMjWGsO00nWRk5lOxN4FuRjgEhIHdYmPnJdXiKfKUDkizrU020lUkd/o5a5s0BMqFbS3usqZOULTA8i1GpjFzlXMUtdf7xrpGcOWQKBgFJXWuX4Befg8owXFKCqc1qYQF2cEBGv/27XBVon7rAH/4xhnHxx1RzSLGGL0lvzKtikvNhln1PUvmzmRvoi/UZRAw6TVAwCkDqUTrTwsSl7ttS8Lmf75Ycu2aZnfnBDYwvLNCp/18oRxNEdZPzWRvk42k6y4d0KQ9yh+q0hBUC/AoGASgJro5HgKWkSzYx1Fk3rvo7EWeZwXNfWuzIhtCiVDG4+df0ZdybkbjxbaRwL2rbTSBeqSaZw4UwpE1JyF8erfzx0qpHCY5V3ocnA8kg56mX9kE5H9WKjMe1tbuZpba4i08zvM8vpz9gASf3z/WrWWqgpvynxDWpy4xFAqrWC0VQ=") -var publicKeyPEM = sdkutils.NewPublic().Build("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3ErEoBiGNPT3iIt9ZIuf8znScpOpVk8cToo810EGP8AE/Z6M2s7r18kHCLPCBHvxOjcOTJNWwD2bbDnfY5HNHCXSTWwQUDtFpPAsm4orEaB4Qsh6ohTXTcyNhWp9cCIg42i7jVi5Eq7TVzKuVezCXk3XeLltT56wuCVRfe35SNPe8RDakvbE+fl46+yorz0AKXBoqvsgR8q9QKc6HDSUcFEDZkuFCt7PnMek6L0dQdmb7NHQdG2+8RnUAR4wMEVKuGAX5nCp4IeDE4L23658KPijK1Hg6u6dAqKTfCarj4Act+WOjYYCaARcU/rNX8RGthdvLhUc4lT0ALnbmOEj2QIDAQAB") - -func Test_sign_verify(t *testing.T) { - data := "123456" - t.Run("test_sign", func(t *testing.T) { - got, err := Sign(data, []byte(privateKeyPEM)) - if err != nil { - t.Errorf("sign() error = %v", err) - return - } - t.Log(got) - b := Verify(data, got, []byte(publicKeyPEM)) - t.Log(b) - }) -} diff --git a/plugins/union_pay_cpn/internal/utils/sm4.go b/plugins/union_pay_cpn/internal/utils/sm4.go deleted file mode 100644 index b7f5690..0000000 --- a/plugins/union_pay_cpn/internal/utils/sm4.go +++ /dev/null @@ -1,41 +0,0 @@ -package utils - -import ( - "encoding/base64" - "fmt" - "github.com/tjfoc/gmsm/sm4" -) - -func Encrypt(txt, key, iv []byte) (string, error) { - pk := HexToBytes(key) - - err := sm4.SetIV(iv) - if err != nil { - return "", fmt.Errorf("sm4.SetIV err:%v", err) - } - cipherTxt, err := sm4.Sm4Cbc(pk, txt, true) - if err != nil { - return "", fmt.Errorf("sm4.Sm4Cbc err:%v", err) - } - - return base64.StdEncoding.EncodeToString(cipherTxt), nil -} - -func Decrypt(txt string, key []byte, iv []byte) (string, error) { - plainText, err := base64.StdEncoding.DecodeString(txt) - if err != nil { - return "", fmt.Errorf("base64.StdEncoding.DecodeString err:%v", err) - } - pk := HexToBytes(key) - - err = sm4.SetIV(iv) - if err != nil { - return "", fmt.Errorf("sm4.SetIV err:%v", err) - } - cipherTxt, err := sm4.Sm4Cbc(pk, plainText, false) - if err != nil { - return "", fmt.Errorf("sm4.Sm4Cbc err:%v", err) - } - - return string(cipherTxt), nil -} diff --git a/plugins/union_pay_redpack/go.mod b/plugins/union_pay_redpack/go.mod index f58f5c6..30149ba 100644 --- a/plugins/union_pay_redpack/go.mod +++ b/plugins/union_pay_redpack/go.mod @@ -2,13 +2,15 @@ module plugins/union_pay_redpack go 1.22.2 +replace plugins/utils => ../../utils + require ( gitea.cdlsxd.cn/BaseSystem/plugin v0.0.0-20240821033242-d9c3e7ad88f6 github.com/carlmjohnson/requests v0.23.4 github.com/go-playground/validator/v10 v10.22.0 github.com/hashicorp/go-plugin v1.6.1 github.com/stretchr/testify v1.9.0 - github.com/tjfoc/gmsm v1.4.1 + plugins/utils v0.0.0-00010101000000-000000000000 ) require ( @@ -26,6 +28,7 @@ require ( github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77 // indirect github.com/oklog/run v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/tjfoc/gmsm v1.4.1 // indirect golang.org/x/crypto v0.21.0 // indirect golang.org/x/net v0.22.0 // indirect golang.org/x/sys v0.18.0 // indirect diff --git a/plugins/union_pay_redpack/internal/transform.go b/plugins/union_pay_redpack/internal/transform.go index 4b1183a..0c204fb 100644 --- a/plugins/union_pay_redpack/internal/transform.go +++ b/plugins/union_pay_redpack/internal/transform.go @@ -5,8 +5,8 @@ import ( "fmt" "gitea.cdlsxd.cn/BaseSystem/plugin/proto" "plugins/union_pay_redpack/internal/po" - "plugins/union_pay_redpack/internal/utils" "plugins/union_pay_redpack/internal/vo" + "plugins/utils/union_pay" ) type Config struct { @@ -44,7 +44,7 @@ func (c *Config) orderReq(in *proto.OrderRequest) (*po.OrderReq, error) { return nil, fmt.Errorf("order extra json unmarshal error: %v", err) } - mobile, err := utils.Encrypt([]byte(in.Order.Account), []byte(c.KEY), []byte(c.IV)) + mobile, err := union_pay.Encrypt([]byte(in.Order.Account), []byte(c.KEY), []byte(c.IV)) if err != nil { return nil, err } @@ -96,7 +96,7 @@ func (c *Config) queryReq(in *proto.QueryRequest, chNlId string) (*po.QueryReq, if err != nil { return nil, fmt.Errorf("order extra json unmarshal error: %v", err) } - mobile, err := utils.Encrypt([]byte(in.Order.Account), []byte(c.KEY), []byte(c.IV)) + mobile, err := union_pay.Encrypt([]byte(in.Order.Account), []byte(c.KEY), []byte(c.IV)) if err != nil { return nil, err } diff --git a/plugins/union_pay_redpack/internal/util.go b/plugins/union_pay_redpack/internal/util.go index d04c07d..a93eb11 100644 --- a/plugins/union_pay_redpack/internal/util.go +++ b/plugins/union_pay_redpack/internal/util.go @@ -5,8 +5,8 @@ import ( sdkutils "gitea.cdlsxd.cn/BaseSystem/plugin/utils" "net/http" "plugins/union_pay_redpack/internal/po" - "plugins/union_pay_redpack/internal/utils" "plugins/union_pay_redpack/internal/vo" + "plugins/utils/union_pay" "time" ) @@ -26,8 +26,8 @@ func headers(config *Config, req po.Req, bizMethod string) map[string][]string { milliseconds := now.Unix()*1000 + int64(now.Nanosecond())/1e6 h.Add("reqTs", fmt.Sprintf("%d", milliseconds)) - rehash := utils.Sha(vo.Version, config.AppId, bizMethod, req.GetReId(), string(req.ToJson())) - signValue, err := utils.Sign(rehash, []byte(sdkutils.NewPrivate().Build(config.Prk))) + rehash := union_pay.Sha(vo.Version, config.AppId, bizMethod, req.GetReId(), string(req.ToJson())) + signValue, err := union_pay.Sign(rehash, []byte(sdkutils.NewPrivate().Build(config.Prk))) if err != nil { return nil } diff --git a/plugins/union_pay_redpack/internal/utils/sha.go b/plugins/union_pay_redpack/internal/utils/sha.go deleted file mode 100644 index e56e6f7..0000000 --- a/plugins/union_pay_redpack/internal/utils/sha.go +++ /dev/null @@ -1,58 +0,0 @@ -package utils - -import ( - "crypto/sha256" - "fmt" - "strconv" -) - -func Sha(version, appId, bizMethod, reId, body string) string { - s := fmt.Sprintf("version=%s&appId=%s&bizMethod=%s&reqId=%s&body=%s", version, appId, bizMethod, reId, body) - return encodeBySHA256([]byte(s)) -} - -func encodeBySHA256(str []byte) string { - hash := sha256.Sum256(str) - return getFormattedText(hash[:]) -} - -func getFormattedText(bytes []byte) string { - HexDigits := "0123456789abcdef" - - l := len(bytes) - buf := make([]byte, l*2) - for j := 0; j < l; j++ { - buf[j*2] = HexDigits[(bytes[j]>>4)&0x0f] - buf[j*2+1] = HexDigits[bytes[j]&0x0f] - } - - return string(buf) -} - -func ConvertToInt(value interface{}) int { - switch v := value.(type) { - case string: - if intValue, err := strconv.Atoi(v); err == nil { - return intValue - } - case int: - return v - } - return 0 -} - -// HexToBytes 将16进制的字符数组转换成byte数组 -func HexToBytes(hex []byte) []byte { - length := len(hex) / 2 - raw := make([]byte, length) - for i := 0; i < length; i++ { - high, _ := strconv.ParseInt(string(hex[i*2]), 16, 0) - low, _ := strconv.ParseInt(string(hex[i*2+1]), 16, 0) - value := int(high<<4) | int(low) - if value > 127 { - value -= 256 - } - raw[i] = byte(value) - } - return raw -} diff --git a/plugins/union_pay_redpack/internal/utils/sm4_test.go b/plugins/union_pay_redpack/internal/utils/sm4_test.go deleted file mode 100644 index 3bdee32..0000000 --- a/plugins/union_pay_redpack/internal/utils/sm4_test.go +++ /dev/null @@ -1,29 +0,0 @@ -package utils - -import ( - "testing" -) - -var iv = "0123456789123456" -var key = "16fb034dead44ff6f599b1ca373ccfd1" - -func TestEncrypt(t *testing.T) { - text := "13100720242" - e, err := Encrypt([]byte(text), []byte(key), []byte(iv)) - if err != nil { - t.Errorf("sm4加密失败:%s\n", err) - return - } - t.Logf("sm4加密结果:%s\n", e) -} - -func TestDecrypt(t *testing.T) { - //text := "R2wVgntY8aP9QTvCN/bWfw==" - text := "/CdDsyQqyOfS2jXOFjX3Iw==" - d, err := Decrypt(text, []byte(key), []byte(iv)) - if err != nil { - t.Errorf("sm4解密失败:%s\n", err) - return - } - t.Logf("sm4解密结果:%s\n", d) -} diff --git a/utils/go.mod b/utils/go.mod index 3e03653..7085a6c 100644 --- a/utils/go.mod +++ b/utils/go.mod @@ -2,6 +2,10 @@ module plugins/utils go 1.22.2 -require github.com/wechatpay-apiv3/wechatpay-go v0.2.18 +require ( + gitea.cdlsxd.cn/BaseSystem/plugin v0.0.0-20240821033242-d9c3e7ad88f6 + github.com/tjfoc/gmsm v1.4.1 + github.com/wechatpay-apiv3/wechatpay-go v0.2.18 +) require github.com/stretchr/testify v1.8.4 // indirect diff --git a/utils/go.sum b/utils/go.sum index 811180c..47413b4 100644 --- a/utils/go.sum +++ b/utils/go.sum @@ -1,10 +1,36 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +gitea.cdlsxd.cn/BaseSystem/plugin v0.0.0-20240821033242-d9c3e7ad88f6 h1:L+z3IjcLEbj9UJAQCZv1YA1HqIrZZxBlZ9I4tQF6dXY= +gitea.cdlsxd.cn/BaseSystem/plugin v0.0.0-20240821033242-d9c3e7ad88f6/go.mod h1:59zYRFcPur2HizJEIfttVOMY0AscollDYKX0yvPJS7k= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/agiledragon/gomonkey v2.0.2+incompatible h1:eXKi9/piiC3cjJD1658mEE2o3NjkJ5vDLgYjCQu0Xlw= github.com/agiledragon/gomonkey v2.0.2+incompatible/go.mod h1:2NGfXu1a80LLr2cmWXGBDaHEjb1idR6+FVlX5T3D9hw= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= 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= @@ -13,9 +39,56 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO 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/tjfoc/gmsm v1.4.1 h1:aMe1GlZb+0bLjn+cKTPEvvn9oUEBlJitaZiiBwsbgho= +github.com/tjfoc/gmsm v1.4.1/go.mod h1:j4INPkHWMrhJb38G+J6W4Tw0AbuN8Thu3PbdVYhVcTE= github.com/wechatpay-apiv3/wechatpay-go v0.2.18 h1:vj5tvSmnEIz3ZsnFNNUzg+3Z46xgNMJbrO4aD4wP15w= github.com/wechatpay-apiv3/wechatpay-go v0.2.18/go.mod h1:A254AUBVB6R+EqQFo3yTgeh7HtyqRRtN2w9hQSOrd4Q= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= +google.golang.org/grpc v1.31.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/plugins/union_pay_cpn/internal/utils/sha.go b/utils/union_pay/sha.go similarity index 98% rename from plugins/union_pay_cpn/internal/utils/sha.go rename to utils/union_pay/sha.go index e56e6f7..1da0f2e 100644 --- a/plugins/union_pay_cpn/internal/utils/sha.go +++ b/utils/union_pay/sha.go @@ -1,4 +1,4 @@ -package utils +package union_pay import ( "crypto/sha256" diff --git a/plugins/union_pay_redpack/internal/utils/sign.go b/utils/union_pay/sign.go similarity index 98% rename from plugins/union_pay_redpack/internal/utils/sign.go rename to utils/union_pay/sign.go index 324663a..4678bbc 100644 --- a/plugins/union_pay_redpack/internal/utils/sign.go +++ b/utils/union_pay/sign.go @@ -1,4 +1,4 @@ -package utils +package union_pay import ( "crypto" diff --git a/plugins/union_pay_redpack/internal/utils/sign_test.go b/utils/union_pay/sign_test.go similarity index 99% rename from plugins/union_pay_redpack/internal/utils/sign_test.go rename to utils/union_pay/sign_test.go index 53879d0..68beee3 100644 --- a/plugins/union_pay_redpack/internal/utils/sign_test.go +++ b/utils/union_pay/sign_test.go @@ -1,4 +1,4 @@ -package utils +package union_pay import ( sdkutils "gitea.cdlsxd.cn/BaseSystem/plugin/utils" diff --git a/plugins/union_pay_redpack/internal/utils/sm4.go b/utils/union_pay/sm4.go similarity index 98% rename from plugins/union_pay_redpack/internal/utils/sm4.go rename to utils/union_pay/sm4.go index b7f5690..835da6f 100644 --- a/plugins/union_pay_redpack/internal/utils/sm4.go +++ b/utils/union_pay/sm4.go @@ -1,4 +1,4 @@ -package utils +package union_pay import ( "encoding/base64" diff --git a/plugins/union_pay_cpn/internal/utils/sm4_test.go b/utils/union_pay/sm4_test.go similarity index 96% rename from plugins/union_pay_cpn/internal/utils/sm4_test.go rename to utils/union_pay/sm4_test.go index 44f91bd..f37611b 100644 --- a/plugins/union_pay_cpn/internal/utils/sm4_test.go +++ b/utils/union_pay/sm4_test.go @@ -1,4 +1,4 @@ -package utils +package union_pay import ( "testing"