Commit 9ee05aca by dliangx

加解密报文类型修改

parent 19a65ea4
...@@ -14,10 +14,10 @@ import ( ...@@ -14,10 +14,10 @@ import (
func Auth(ctx context.Context, c *app.RequestContext) { func Auth(ctx context.Context, c *app.RequestContext) {
var err error var err error
var creq model.CryptHttpBodyReq[[]byte] var creq model.CryptHttpBodyReq
var req model.HttpBodyReq[model.AuthReq] var req model.HttpBodyReq[model.AuthReq]
var resp model.HttpBodyResp[model.AuthResp] var resp model.HttpBodyResp[model.AuthResp]
err = c.BindAndValidate(&req) err = c.BindAndValidate(&creq)
if err != nil { if err != nil {
resp.Head.Code = nltconst.PARAM_ERROR resp.Head.Code = nltconst.PARAM_ERROR
resp.Head.ServiceTime = nltconst.GetNowTime() resp.Head.ServiceTime = nltconst.GetNowTime()
......
...@@ -14,7 +14,7 @@ import ( ...@@ -14,7 +14,7 @@ import (
func QueryPayResult(ctx context.Context, c *app.RequestContext) { func QueryPayResult(ctx context.Context, c *app.RequestContext) {
var err error var err error
var creq model.CryptHttpBodyReq[[]byte] var creq model.CryptHttpBodyReq
var req model.HttpBodyReq[model.PayResultReq] var req model.HttpBodyReq[model.PayResultReq]
var resp model.HttpBodyResp[model.PayResultResp] var resp model.HttpBodyResp[model.PayResultResp]
err = c.BindAndValidate(&creq) err = c.BindAndValidate(&creq)
......
...@@ -30,7 +30,7 @@ func TestAuthDecrypt(t *testing.T) { ...@@ -30,7 +30,7 @@ func TestAuthDecrypt(t *testing.T) {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
var res model.CryptHttpBodyReq[[]byte] var res model.CryptHttpBodyReq
res.Request.Head.ServiceSn = cresp.Response.Head.ServiceSn res.Request.Head.ServiceSn = cresp.Response.Head.ServiceSn
res.Request.Request = cresp.Response.Response res.Request.Request = cresp.Response.Response
res.Signature = cresp.Signature res.Signature = cresp.Signature
......
...@@ -11,13 +11,13 @@ import ( ...@@ -11,13 +11,13 @@ import (
"nlt.com/pf/nltconst" "nlt.com/pf/nltconst"
) )
type CryptHttpBodyReq[T any] struct { type CryptHttpBodyReq struct {
Request HttpBodyReq[T] Request HttpBodyReq[[]byte]
Signature []byte Signature []byte
} }
type CryptHttpBodyResp[T any] struct { type CryptHttpBodyResp struct {
Response HttpBodyResp[T] Response HttpBodyResp[[]byte]
Signature []byte Signature []byte
} }
...@@ -42,8 +42,8 @@ type RespHead struct { ...@@ -42,8 +42,8 @@ type RespHead struct {
ServiceSn string `json:"serviceSn"` ServiceSn string `json:"serviceSn"`
} }
func EncryptAndSign[T any](resp HttpBodyResp[T]) (CryptHttpBodyResp[[]byte], error) { func EncryptAndSign[T any](resp HttpBodyResp[T]) (CryptHttpBodyResp, error) {
var cresp CryptHttpBodyResp[[]byte] var cresp CryptHttpBodyResp
privateKeyBytes, _ := sm2.GenerateKey(strings.NewReader(nltconst.SM2_PRIVATE_KEY)) privateKeyBytes, _ := sm2.GenerateKey(strings.NewReader(nltconst.SM2_PRIVATE_KEY))
// 对应的公钥 // 对应的公钥
...@@ -80,7 +80,7 @@ func EncryptAndSign[T any](resp HttpBodyResp[T]) (CryptHttpBodyResp[[]byte], err ...@@ -80,7 +80,7 @@ func EncryptAndSign[T any](resp HttpBodyResp[T]) (CryptHttpBodyResp[[]byte], err
return cresp, err return cresp, err
} }
func VerifyAndDecrypt[T any](creq CryptHttpBodyReq[[]byte]) (HttpBodyReq[T], error) { func VerifyAndDecrypt[T any](creq CryptHttpBodyReq) (HttpBodyReq[T], error) {
var req HttpBodyReq[T] var req HttpBodyReq[T]
privateKeyBytes, _ := sm2.GenerateKey(strings.NewReader(nltconst.SM2_PRIVATE_KEY)) privateKeyBytes, _ := sm2.GenerateKey(strings.NewReader(nltconst.SM2_PRIVATE_KEY))
signature := creq.Signature signature := creq.Signature
...@@ -91,7 +91,7 @@ func VerifyAndDecrypt[T any](creq CryptHttpBodyReq[[]byte]) (HttpBodyReq[T], err ...@@ -91,7 +91,7 @@ func VerifyAndDecrypt[T any](creq CryptHttpBodyReq[[]byte]) (HttpBodyReq[T], err
} }
uid := []byte("tk") uid := []byte("tk")
if sm2.Sm2Verify(&privateKeyBytes.PublicKey, creq.Signature, uid, r, s) { if sm2.Sm2Verify(&privateKeyBytes.PublicKey, creq.Signature, uid, r, s) {
tx, err := sm2.Decrypt(privateKeyBytes, []byte(creq.Request.Request), sm2.C1C2C3) tx, err := sm2.Decrypt(privateKeyBytes, creq.Request.Request, sm2.C1C2C3)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return req, errors.New("解密错误") return req, errors.New("解密错误")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment