Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nlt-pufa-interface
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
邓梁
nlt-pufa-interface
Commits
66d904c4
Commit
66d904c4
authored
Sep 25, 2024
by
dliangx
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加报文加解密
parent
1879ee47
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
99 additions
and
13 deletions
+99
-13
go.mod
+1
-0
go.sum
+0
-0
handler/auth.go
+8
-5
handler/pay_result.go
+8
-6
model/http_body.go
+78
-2
nltconst/crypt.go
+4
-0
No files found.
go.mod
View file @
66d904c4
...
@@ -5,6 +5,7 @@ go 1.22.5
...
@@ -5,6 +5,7 @@ go 1.22.5
require (
require (
github.com/cloudwego/hertz v0.9.3
github.com/cloudwego/hertz v0.9.3
github.com/go-sql-driver/mysql v1.8.1
github.com/go-sql-driver/mysql v1.8.1
github.com/tjfoc/gmsm v1.4.1
)
)
require (
require (
...
...
go.sum
View file @
66d904c4
This diff is collapsed.
Click to expand it.
handler/auth.go
View file @
66d904c4
...
@@ -14,15 +14,17 @@ import (
...
@@ -14,15 +14,17 @@ 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
[
string
]
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
(
&
req
)
req
=
model
.
VerifyAndDecrypt
[
model
.
AuthReq
](
creq
)
fmt
.
Println
(
req
)
fmt
.
Println
(
req
)
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
()
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
c
.
JSON
(
consts
.
StatusOK
,
resp
)
c
.
JSON
(
consts
.
StatusOK
,
model
.
EncryptAndSign
(
resp
)
)
return
return
}
}
rows
,
err
:=
db
.
DB
.
Query
(
"select farm_name from loan_orderinfo where id_num = ?"
,
&
req
.
Request
.
IdNo
)
rows
,
err
:=
db
.
DB
.
Query
(
"select farm_name from loan_orderinfo where id_num = ?"
,
&
req
.
Request
.
IdNo
)
...
@@ -31,7 +33,7 @@ func Auth(ctx context.Context, c *app.RequestContext) {
...
@@ -31,7 +33,7 @@ func Auth(ctx context.Context, c *app.RequestContext) {
resp
.
Head
.
Code
=
nltconst
.
SYS_ERROR
resp
.
Head
.
Code
=
nltconst
.
SYS_ERROR
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
c
.
JSON
(
consts
.
StatusOK
,
resp
)
c
.
JSON
(
consts
.
StatusOK
,
model
.
EncryptAndSign
(
resp
)
)
return
return
}
}
...
@@ -47,7 +49,7 @@ func Auth(ctx context.Context, c *app.RequestContext) {
...
@@ -47,7 +49,7 @@ func Auth(ctx context.Context, c *app.RequestContext) {
resp
.
Head
.
Code
=
string
(
nltconst
.
PERSON_INFO_NO_MATCH
)
resp
.
Head
.
Code
=
string
(
nltconst
.
PERSON_INFO_NO_MATCH
)
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
c
.
JSON
(
consts
.
StatusOK
,
resp
)
c
.
JSON
(
consts
.
StatusOK
,
model
.
EncryptAndSign
(
resp
)
)
return
return
}
}
...
@@ -57,7 +59,7 @@ func Auth(ctx context.Context, c *app.RequestContext) {
...
@@ -57,7 +59,7 @@ func Auth(ctx context.Context, c *app.RequestContext) {
resp
.
Head
.
Code
=
nltconst
.
SYS_ERROR
resp
.
Head
.
Code
=
nltconst
.
SYS_ERROR
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
c
.
JSON
(
consts
.
StatusOK
,
resp
)
c
.
JSON
(
consts
.
StatusOK
,
model
.
EncryptAndSign
(
resp
)
)
return
return
}
}
...
@@ -65,6 +67,7 @@ func Auth(ctx context.Context, c *app.RequestContext) {
...
@@ -65,6 +67,7 @@ func Auth(ctx context.Context, c *app.RequestContext) {
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
resp
.
Response
.
DirectURL
=
nltconst
.
DURL
+
"?param="
+
req
.
Request
.
DuebillNoOrg
resp
.
Response
.
DirectURL
=
nltconst
.
DURL
+
"?param="
+
req
.
Request
.
DuebillNoOrg
c
.
JSON
(
consts
.
StatusOK
,
resp
)
c
.
JSON
(
consts
.
StatusOK
,
model
.
EncryptAndSign
(
resp
))
return
return
}
}
handler/pay_result.go
View file @
66d904c4
...
@@ -14,13 +14,15 @@ import (
...
@@ -14,13 +14,15 @@ 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
[
string
]
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
(
&
req
)
err
=
c
.
BindAndValidate
(
&
creq
)
req
=
model
.
VerifyAndDecrypt
[
model
.
PayResultReq
](
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
()
c
.
JSON
(
consts
.
StatusOK
,
resp
)
c
.
JSON
(
consts
.
StatusOK
,
model
.
EncryptAndSign
(
resp
)
)
return
return
}
}
...
@@ -34,7 +36,7 @@ func QueryPayResult(ctx context.Context, c *app.RequestContext) {
...
@@ -34,7 +36,7 @@ func QueryPayResult(ctx context.Context, c *app.RequestContext) {
resp
.
Head
.
Code
=
nltconst
.
SYS_ERROR
resp
.
Head
.
Code
=
nltconst
.
SYS_ERROR
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
c
.
JSON
(
consts
.
StatusOK
,
resp
)
c
.
JSON
(
consts
.
StatusOK
,
model
.
EncryptAndSign
(
resp
)
)
return
return
}
}
var
count
int
var
count
int
...
@@ -51,7 +53,7 @@ func QueryPayResult(ctx context.Context, c *app.RequestContext) {
...
@@ -51,7 +53,7 @@ func QueryPayResult(ctx context.Context, c *app.RequestContext) {
resp
.
Head
.
Code
=
nltconst
.
NODATA
resp
.
Head
.
Code
=
nltconst
.
NODATA
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
c
.
JSON
(
consts
.
StatusOK
,
resp
)
c
.
JSON
(
consts
.
StatusOK
,
model
.
EncryptAndSign
(
resp
)
)
return
return
}
}
...
@@ -61,14 +63,14 @@ func QueryPayResult(ctx context.Context, c *app.RequestContext) {
...
@@ -61,14 +63,14 @@ func QueryPayResult(ctx context.Context, c *app.RequestContext) {
resp
.
Head
.
Code
=
nltconst
.
SYS_ERROR
resp
.
Head
.
Code
=
nltconst
.
SYS_ERROR
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
c
.
JSON
(
consts
.
StatusOK
,
resp
)
c
.
JSON
(
consts
.
StatusOK
,
model
.
EncryptAndSign
(
resp
)
)
return
return
}
}
resp
.
Head
.
Code
=
string
(
nltconst
.
SUCCESS
)
resp
.
Head
.
Code
=
string
(
nltconst
.
SUCCESS
)
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
c
.
JSON
(
consts
.
StatusOK
,
resp
)
c
.
JSON
(
consts
.
StatusOK
,
model
.
EncryptAndSign
(
resp
)
)
return
return
}
}
model/http_body.go
View file @
66d904c4
package
model
package
model
import
(
"crypto/rand"
"encoding/json"
"log"
"strings"
"github.com/tjfoc/gmsm/sm2"
"nlt.com/pf/nltconst"
)
type
CryptHttpBodyReq
[
T
any
]
struct
{
Request
HttpBodyReq
[
T
]
Signature
string
}
type
CryptHttpBodyResp
[
T
any
]
struct
{
Response
HttpBodyResp
[
T
]
Signature
string
}
type
HttpBodyReq
[
T
any
]
struct
{
type
HttpBodyReq
[
T
any
]
struct
{
Head
ReqHead
`json:"head"`
Head
ReqHead
`json:"head"`
Request
T
`json:"
request
"`
Request
T
`json:"
body
"`
}
}
type
HttpBodyResp
[
T
any
]
struct
{
type
HttpBodyResp
[
T
any
]
struct
{
Head
RespHead
`json:"head"`
Head
RespHead
`json:"head"`
Response
T
`json:"
reponse
"`
Response
T
`json:"
body
"`
}
}
type
ReqHead
struct
{
type
ReqHead
struct
{
...
@@ -20,3 +40,59 @@ type RespHead struct {
...
@@ -20,3 +40,59 @@ type RespHead struct {
ServiceTime
string
`json:"serviceTime"`
ServiceTime
string
`json:"serviceTime"`
ServiceSn
string
`json:"serviceSn"`
ServiceSn
string
`json:"serviceSn"`
}
}
func
EncryptAndSign
[
T
any
](
resp
HttpBodyResp
[
T
])
CryptHttpBodyResp
[
string
]
{
var
cresp
CryptHttpBodyResp
[
string
]
privateKeyBytes
,
_
:=
sm2
.
GenerateKey
(
strings
.
NewReader
(
nltconst
.
SM2_PRIVATE_KEY
))
// 对应的公钥
publicKey
:=
&
privateKeyBytes
.
PublicKey
body
,
err
:=
json
.
Marshal
(
resp
.
Response
)
if
err
!=
nil
{
log
.
Println
(
err
.
Error
())
}
ciphertext
,
err
:=
sm2
.
Encrypt
(
publicKey
,
body
,
rand
.
Reader
,
sm2
.
C1C2C3
)
if
err
!=
nil
{
log
.
Println
(
err
)
}
cresp
.
Response
.
Head
=
resp
.
Head
cresp
.
Response
.
Response
=
string
(
ciphertext
)
response
,
err
:=
json
.
Marshal
(
resp
)
uid
:=
[]
byte
(
"tk"
)
r
,
s
,
err
:=
sm2
.
Sm2Sign
(
privateKeyBytes
,
response
,
uid
,
rand
.
Reader
)
if
err
!=
nil
{
log
.
Println
(
err
)
}
rBytes
:=
r
.
Bytes
()
sBytes
:=
s
.
Bytes
()
signature
:=
append
(
rBytes
,
sBytes
...
)
cresp
.
Signature
=
string
(
signature
)
return
cresp
}
func
VerifyAndDecrypt
[
T
any
](
creq
CryptHttpBodyReq
[
string
])
HttpBodyReq
[
T
]
{
var
req
HttpBodyReq
[
T
]
privateKeyBytes
,
_
:=
sm2
.
GenerateKey
(
strings
.
NewReader
(
nltconst
.
SM2_PRIVATE_KEY
))
signature
:=
creq
.
Signature
r
,
s
,
err
:=
sm2
.
SignDataToSignDigit
([]
byte
(
signature
))
if
err
!=
nil
{
log
.
Println
(
err
)
}
uid
:=
[]
byte
(
"tk"
)
if
sm2
.
Sm2Verify
(
&
privateKeyBytes
.
PublicKey
,
[]
byte
(
creq
.
Request
.
Request
),
uid
,
r
,
s
)
{
tx
,
err
:=
sm2
.
Decrypt
(
privateKeyBytes
,
[]
byte
(
creq
.
Request
.
Request
),
sm2
.
C1C2C3
)
if
err
!=
nil
{
log
.
Println
(
err
)
}
req
.
Head
=
creq
.
Request
.
Head
err
=
json
.
Unmarshal
(
tx
,
req
.
Request
)
if
err
!=
nil
{
log
.
Println
(
err
)
}
return
req
}
return
req
}
nltconst/crypt.go
0 → 100644
View file @
66d904c4
package
nltconst
const
SM2_PUBLIC_KEY
=
"A7CD09260A67113F988F530154AD6A70B2A4DD3E00BD27BB124E7E7051FC0C97E7AC3C5A6CB6C9BB459BEF252761AD1AE727718498CA3130D67CFC84F9B1BB1F"
const
SM2_PRIVATE_KEY
=
"BF6CA99BC05A05C8B4F916A8C6187E5A68207A7B7D89ACC7F478B7E3AFA29454"
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment