From b924d00b650f0b31d47487634f0ecc16002485a4 Mon Sep 17 00:00:00 2001 From: jiaoboxiang Date: Wed, 9 Nov 2022 18:38:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=BE=B3=E9=96=80=E5=9C=8B=E9=9A=9B=E9=8A=80?= =?UTF-8?q?=E8=A1=8C=E6=94=AF=E4=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/luso_bank/macao_luso.go | 4 +++- test/luso_pay_test.go | 15 +++++++++++- utils/RsaSign.go | 48 ++++++++++++++++++++++++++++++++++++- 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/lib/luso_bank/macao_luso.go b/lib/luso_bank/macao_luso.go index c48ab98..b2c6826 100644 --- a/lib/luso_bank/macao_luso.go +++ b/lib/luso_bank/macao_luso.go @@ -2,6 +2,7 @@ package luso_bank import ( zhios_pay_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/utils" + "crypto" "encoding/base64" "fmt" "github.com/syyongx/php2go" @@ -14,6 +15,7 @@ const ( TerminalNo = "98036140" PrivateKeyStr = "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDSYivjR7nXUazy\nf+F1i/nvuy/doiHRsiTSwavHIwKF88kzM95Eh5wGiXq6TXwFPhB5yeV8l3tFQxeu\nPycZeKmp0VBge+U+/UhR1rQwEX9U9LeKhVdjbNU0U8kNpO8qX/QqHSS7MCRILL2a\nS4F5U/wvYGkbqJ+qMUNmEMGwEkiVuDaO/Q4I3lMPNtLO+mizgq9jggEkVh+xG50p\nFATWd6CJh0mddBBgGG3/aG1ybV/IYRRP0sS+dbYKmKghbK2d/LztIM8ba7lA16H9\nxLY8oy8SvwGqkq2Gea9K2AIEjociac4i0Q/Ka2U881rJCrDUMg3gDhIundaa971M\nglS5cRe9AgMBAAECggEBALe9P6FHIlqYHZjI8QqtToL+E6bYk/rFP/3FYZYQ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Pa9e3VGTARJG5WP6nx8BPVx71Er/wquBLLquoH0XlTmpdj80QF+s\nHlnKE9CfXVu23ci2S0WalbX/8CYGlO5iCct9o0KR0inrXKfqFzuSKkpg6JyXVMk0\nMT1+EUSD25ATIRsNGFWigNHL\n-----END PRIVATE KEY-----" MchtStoreNm = "世寶商城" + PUBLICKEY = "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0mIr40e511Gs8n/hdYv5\n77sv3aIh0bIk0sGrxyMChfPJMzPeRIecBol6uk18BT4QecnlfJd7RUMXrj8nGXip\nqdFQYHvlPv1IUda0MBF/VPS3ioVXY2zVNFPJDaTvKl/0Kh0kuzAkSCy9mkuBeVP8\nL2BpG6ifqjFDZhDBsBJIlbg2jv0OCN5TDzbSzvpos4KvY4IBJFYfsRudKRQE1neg\niYdJnXQQYBht/2htcm1fyGEUT9LEvnW2CpioIWytnfy87SDPG2u5QNeh/cS2PKMv\nEr8BqpKthnmvStgCBI6HImnOItEPymtlPPNayQqw1DIN4A4SLp3Wmve9TIJUuXEX\nvQIDAQAB\n-----END PUBLIC KEY-----" ) type PayParam struct { @@ -56,7 +58,7 @@ func (payParam *PayParam) SetData(dataParam DataParam) *signParam { func (s *signParam) SetSign(privateKeyStr string) error { dataParamBase64Str := base64.StdEncoding.EncodeToString([]byte(s.PayParam.Data)) - signStr, err := zhios_pay_utils.GetSign(privateKeyStr, dataParamBase64Str) + signStr, err := zhios_pay_utils.GetSignV2(privateKeyStr, dataParamBase64Str, crypto.SHA1) if err != nil { return err } diff --git a/test/luso_pay_test.go b/test/luso_pay_test.go index c05f5c6..78b1c54 100644 --- a/test/luso_pay_test.go +++ b/test/luso_pay_test.go @@ -3,6 +3,8 @@ package test import ( luso "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/lib/luso_bank" zhios_pay_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/utils" + "crypto" + "log" "testing" ) @@ -31,9 +33,20 @@ func TestLusoPay(t *testing.T) { if err != nil { t.Error(err) } - resp, err := client.PayParam.Send(false) + resp, err := client.PayParam.Send(false, false, "create") if err != nil { t.Error(err) } t.Logf("回調信息:%s", resp) } + +func TestRsa2Rsa2PriSign(t *testing.T) { + //原内容 + str := "eyJoZWFkIjp7Im1jaHRObyI6IjMwMDExIiwicmVxVGltZSI6IjIwMjIxMTA5MTUwNjQ1IiwibXNnSWQiOiI1MjE0NTAxNjY3OTc3NjA1MzgyMDIyMTEwOTE1MDY0NSIsImF0dGFjaCI6IiIsImNlcnRJZCI6IjAxIiwidmVyc2lvbiI6IjEuMC4wIn0sInJlcUJvZHkiOnsiZXh0TXNnICI6IntcIm1pZFwiOlwiMTIzNDU2XCIsXCJvcmRfdHlwZVwiOlwibWFsbF9nb29kc1wifSIsIm1jaHRPcmRlcklkIjoiNTIxNDUwMTY2Nzk3NzYwNTM4IiwibWRzZURlc2MgIjoi5LiW5a+25ZWG5Z+OLeiHqueHn+izvOeJqSIsIm5vdGlmeVVybCAiOiJodHRwOi8vaW5hcGkuaXpoeWluLmNuOjgwL3BheS9sdXNvL2NhbGxiYWNrIiwicGF5U2NlbmUgIjoiQVNfTVdFQiIsInN1Yk1jaHRObSAiOiLkuJblr7bllYbln44iLCJ0cmFuc0Ftb3VudCAiOiIxMDAuMDAiLCJ0cmFuc0N1ciAiOiJNT1AifX0=" + //生成签名 + sig, _ := zhios_pay_utils.GetSignV2(luso.PrivateKeyStr, str, crypto.SHA1) + log.Println(sig) + //验证原内容与签名是否一致 + res := zhios_pay_utils.Rsa2PubCheckSign(str, sig, luso.PUBLICKEY, crypto.SHA1) + log.Println(res) +} diff --git a/utils/RsaSign.go b/utils/RsaSign.go index 97ef35b..d139346 100644 --- a/utils/RsaSign.go +++ b/utils/RsaSign.go @@ -4,7 +4,11 @@ import ( "crypto" "crypto/rand" "crypto/rsa" + "crypto/sha1" + "crypto/x509" "encoding/base64" + "encoding/pem" + "errors" "fmt" "golang.org/x/crypto/ssh" "sort" @@ -28,7 +32,49 @@ func GetSign(privateKeyStr, data string) (string, error) { return base64.StdEncoding.EncodeToString(sign), nil } -//规则: +func GetSignV2(privateKeyStr, data string, hash crypto.Hash) (string, error) { + privateKey, err := ssh.ParseRawPrivateKey([]byte(privateKeyStr)) + if err != nil { + return "", err + } + h := crypto.Hash.New(hash) + h.Write([]byte(data)) + hashed := h.Sum(nil) + sign, err := rsa.SignPKCS1v15(rand.Reader, privateKey.(*rsa.PrivateKey), hash, hashed) + if err != nil { + return "", nil + } + return base64.StdEncoding.EncodeToString(sign), nil +} + +// RSA2公钥验证签名 +func Rsa2PubCheckSign(signContent, sign, publicKey string, hash crypto.Hash) bool { + hashed := sha1.Sum([]byte(signContent)) + pubKey, err := ParsePublicKey(publicKey) + if err != nil { + return false + } + sig, _ := base64.StdEncoding.DecodeString(sign) + err = rsa.VerifyPKCS1v15(pubKey, hash, hashed[:], sig) + if err != nil { + return false + } + return true +} + +// 解析公钥 +func ParsePublicKey(publicKey string) (*rsa.PublicKey, error) { + block, _ := pem.Decode([]byte(publicKey)) + if block == nil { + return nil, errors.New("公钥信息错误!") + } + pubKey, err := x509.ParsePKIXPublicKey(block.Bytes) + if err != nil { + return nil, err + } + return pubKey.(*rsa.PublicKey), nil +} + //◆ 参数名ASCII码从小到大排序(字典序); //◆ 如果参数的值为空不参与签名; //◆ 参数名区分大小写;