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
871afdc6
Commit
871afdc6
authored
Sep 26, 2024
by
dliangx
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
加解密测试
parent
66d904c4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
79 additions
and
32 deletions
+79
-32
handler/auth.go
+27
-9
handler/pay_result.go
+25
-9
model/http_body.go
+25
-14
nltconst/const.go
+2
-0
No files found.
handler/auth.go
View file @
871afdc6
...
...
@@ -14,17 +14,32 @@ import (
func
Auth
(
ctx
context
.
Context
,
c
*
app
.
RequestContext
)
{
var
err
error
var
creq
model
.
CryptHttpBodyReq
[
string
]
var
creq
model
.
CryptHttpBodyReq
[
[]
byte
]
var
req
model
.
HttpBodyReq
[
model
.
AuthReq
]
var
resp
model
.
HttpBodyResp
[
model
.
AuthResp
]
err
=
c
.
BindAndValidate
(
&
req
)
req
=
model
.
VerifyAndDecrypt
[
model
.
AuthReq
](
creq
)
fmt
.
Println
(
req
)
if
err
!=
nil
{
resp
.
Head
.
Code
=
nltconst
.
PARAM_ERROR
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
c
.
JSON
(
consts
.
StatusOK
,
model
.
EncryptAndSign
(
resp
))
cresp
,
_
:=
model
.
EncryptAndSign
(
resp
)
c
.
JSON
(
consts
.
StatusOK
,
cresp
)
return
}
req
,
err
=
model
.
VerifyAndDecrypt
[
model
.
AuthReq
](
creq
)
fmt
.
Println
(
req
)
if
err
!=
nil
{
if
err
.
Error
()
==
"解密错误"
{
resp
.
Head
.
Code
=
nltconst
.
DECRYPT_ERROR
}
else
if
err
.
Error
()
==
"验签错误"
{
resp
.
Head
.
Code
=
nltconst
.
VERIFY_ERROR
}
else
{
resp
.
Head
.
Code
=
nltconst
.
SYS_ERROR
}
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
cresp
,
_
:=
model
.
EncryptAndSign
(
resp
)
c
.
JSON
(
consts
.
StatusOK
,
cresp
)
return
}
rows
,
err
:=
db
.
DB
.
Query
(
"select farm_name from loan_orderinfo where id_num = ?"
,
&
req
.
Request
.
IdNo
)
...
...
@@ -33,7 +48,8 @@ func Auth(ctx context.Context, c *app.RequestContext) {
resp
.
Head
.
Code
=
nltconst
.
SYS_ERROR
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
c
.
JSON
(
consts
.
StatusOK
,
model
.
EncryptAndSign
(
resp
))
cresp
,
_
:=
model
.
EncryptAndSign
(
resp
)
c
.
JSON
(
consts
.
StatusOK
,
cresp
)
return
}
...
...
@@ -49,7 +65,8 @@ func Auth(ctx context.Context, c *app.RequestContext) {
resp
.
Head
.
Code
=
string
(
nltconst
.
PERSON_INFO_NO_MATCH
)
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
c
.
JSON
(
consts
.
StatusOK
,
model
.
EncryptAndSign
(
resp
))
cresp
,
_
:=
model
.
EncryptAndSign
(
resp
)
c
.
JSON
(
consts
.
StatusOK
,
cresp
)
return
}
...
...
@@ -59,7 +76,8 @@ func Auth(ctx context.Context, c *app.RequestContext) {
resp
.
Head
.
Code
=
nltconst
.
SYS_ERROR
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
c
.
JSON
(
consts
.
StatusOK
,
model
.
EncryptAndSign
(
resp
))
cresp
,
_
:=
model
.
EncryptAndSign
(
resp
)
c
.
JSON
(
consts
.
StatusOK
,
cresp
)
return
}
...
...
@@ -67,7 +85,7 @@ func Auth(ctx context.Context, c *app.RequestContext) {
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
resp
.
Response
.
DirectURL
=
nltconst
.
DURL
+
"?param="
+
req
.
Request
.
DuebillNoOrg
cresp
,
_
:=
model
.
EncryptAndSign
(
resp
)
c
.
JSON
(
consts
.
StatusOK
,
cresp
)
c
.
JSON
(
consts
.
StatusOK
,
model
.
EncryptAndSign
(
resp
))
return
}
handler/pay_result.go
View file @
871afdc6
...
...
@@ -14,15 +14,29 @@ import (
func
QueryPayResult
(
ctx
context
.
Context
,
c
*
app
.
RequestContext
)
{
var
err
error
var
creq
model
.
CryptHttpBodyReq
[
string
]
var
creq
model
.
CryptHttpBodyReq
[
[]
byte
]
var
req
model
.
HttpBodyReq
[
model
.
PayResultReq
]
var
resp
model
.
HttpBodyResp
[
model
.
PayResultResp
]
err
=
c
.
BindAndValidate
(
&
creq
)
req
=
model
.
VerifyAndDecrypt
[
model
.
PayResultReq
](
creq
)
if
err
!=
nil
{
resp
.
Head
.
Code
=
nltconst
.
PARAM_ERROR
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
c
.
JSON
(
consts
.
StatusOK
,
model
.
EncryptAndSign
(
resp
))
cresp
,
_
:=
model
.
EncryptAndSign
(
resp
)
c
.
JSON
(
consts
.
StatusOK
,
cresp
)
return
}
req
,
err
=
model
.
VerifyAndDecrypt
[
model
.
PayResultReq
](
creq
)
if
err
!=
nil
{
if
err
.
Error
()
==
"解密错误"
{
resp
.
Head
.
Code
=
nltconst
.
DECRYPT_ERROR
}
else
if
err
.
Error
()
==
"验签错误"
{
resp
.
Head
.
Code
=
nltconst
.
VERIFY_ERROR
}
else
{
resp
.
Head
.
Code
=
nltconst
.
SYS_ERROR
}
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
cresp
,
_
:=
model
.
EncryptAndSign
(
resp
)
c
.
JSON
(
consts
.
StatusOK
,
cresp
)
return
}
...
...
@@ -36,7 +50,8 @@ func QueryPayResult(ctx context.Context, c *app.RequestContext) {
resp
.
Head
.
Code
=
nltconst
.
SYS_ERROR
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
c
.
JSON
(
consts
.
StatusOK
,
model
.
EncryptAndSign
(
resp
))
cresp
,
_
:=
model
.
EncryptAndSign
(
resp
)
c
.
JSON
(
consts
.
StatusOK
,
cresp
)
return
}
var
count
int
...
...
@@ -53,7 +68,8 @@ func QueryPayResult(ctx context.Context, c *app.RequestContext) {
resp
.
Head
.
Code
=
nltconst
.
NODATA
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
c
.
JSON
(
consts
.
StatusOK
,
model
.
EncryptAndSign
(
resp
))
cresp
,
_
:=
model
.
EncryptAndSign
(
resp
)
c
.
JSON
(
consts
.
StatusOK
,
cresp
)
return
}
...
...
@@ -63,14 +79,14 @@ func QueryPayResult(ctx context.Context, c *app.RequestContext) {
resp
.
Head
.
Code
=
nltconst
.
SYS_ERROR
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
c
.
JSON
(
consts
.
StatusOK
,
model
.
EncryptAndSign
(
resp
))
cresp
,
_
:=
model
.
EncryptAndSign
(
resp
)
c
.
JSON
(
consts
.
StatusOK
,
cresp
)
return
}
resp
.
Head
.
Code
=
string
(
nltconst
.
SUCCESS
)
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceSn
=
req
.
Head
.
ServiceSn
c
.
JSON
(
consts
.
StatusOK
,
model
.
EncryptAndSign
(
resp
))
return
cresp
,
_
:=
model
.
EncryptAndSign
(
resp
)
c
.
JSON
(
consts
.
StatusOK
,
cresp
)
}
model/http_body.go
View file @
871afdc6
...
...
@@ -3,6 +3,7 @@ package model
import
(
"crypto/rand"
"encoding/json"
"errors"
"log"
"strings"
...
...
@@ -12,12 +13,12 @@ import (
type
CryptHttpBodyReq
[
T
any
]
struct
{
Request
HttpBodyReq
[
T
]
Signature
string
Signature
[]
byte
}
type
CryptHttpBodyResp
[
T
any
]
struct
{
Response
HttpBodyResp
[
T
]
Signature
string
Signature
[]
byte
}
type
HttpBodyReq
[
T
any
]
struct
{
...
...
@@ -41,8 +42,8 @@ type RespHead struct {
ServiceSn
string
`json:"serviceSn"`
}
func
EncryptAndSign
[
T
any
](
resp
HttpBodyResp
[
T
])
CryptHttpBodyResp
[
string
]
{
var
cresp
CryptHttpBodyResp
[
string
]
func
EncryptAndSign
[
T
any
](
resp
HttpBodyResp
[
T
])
(
CryptHttpBodyResp
[[]
byte
],
error
)
{
var
cresp
CryptHttpBodyResp
[
[]
byte
]
privateKeyBytes
,
_
:=
sm2
.
GenerateKey
(
strings
.
NewReader
(
nltconst
.
SM2_PRIVATE_KEY
))
// 对应的公钥
...
...
@@ -50,49 +51,59 @@ func EncryptAndSign[T any](resp HttpBodyResp[T]) CryptHttpBodyResp[string] {
body
,
err
:=
json
.
Marshal
(
resp
.
Response
)
if
err
!=
nil
{
log
.
Println
(
err
.
Error
())
return
cresp
,
err
}
ciphertext
,
err
:=
sm2
.
Encrypt
(
publicKey
,
body
,
rand
.
Reader
,
sm2
.
C1C2C3
)
if
err
!=
nil
{
log
.
Println
(
err
)
return
cresp
,
err
}
cresp
.
Response
.
Head
=
resp
.
Head
cresp
.
Response
.
Response
=
string
(
ciphertext
)
cresp
.
Response
.
Response
=
ciphertext
response
,
err
:=
json
.
Marshal
(
resp
)
jsonResp
,
err
:=
json
.
Marshal
(
resp
)
if
err
!=
nil
{
log
.
Println
(
err
)
return
cresp
,
err
}
uid
:=
[]
byte
(
"tk"
)
r
,
s
,
err
:=
sm2
.
Sm2Sign
(
privateKeyBytes
,
response
,
uid
,
rand
.
Reader
)
r
,
s
,
err
:=
sm2
.
Sm2Sign
(
privateKeyBytes
,
jsonResp
,
uid
,
rand
.
Reader
)
if
err
!=
nil
{
log
.
Println
(
err
)
return
cresp
,
err
}
rBytes
:=
r
.
Bytes
()
sBytes
:=
s
.
Bytes
()
signature
:=
append
(
rBytes
,
sBytes
...
)
cresp
.
Signature
=
s
tring
(
signature
)
return
cresp
cresp
.
Signature
=
s
ignature
return
cresp
,
err
}
func
VerifyAndDecrypt
[
T
any
](
creq
CryptHttpBodyReq
[
string
])
HttpBodyReq
[
T
]
{
func
VerifyAndDecrypt
[
T
any
](
creq
CryptHttpBodyReq
[
[]
byte
])
(
HttpBodyReq
[
T
],
error
)
{
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
)
return
req
,
err
}
uid
:=
[]
byte
(
"tk"
)
if
sm2
.
Sm2Verify
(
&
privateKeyBytes
.
PublicKey
,
[]
byte
(
creq
.
Request
.
Request
)
,
uid
,
r
,
s
)
{
if
sm2
.
Sm2Verify
(
&
privateKeyBytes
.
PublicKey
,
creq
.
Signature
,
uid
,
r
,
s
)
{
tx
,
err
:=
sm2
.
Decrypt
(
privateKeyBytes
,
[]
byte
(
creq
.
Request
.
Request
),
sm2
.
C1C2C3
)
if
err
!=
nil
{
log
.
Println
(
err
)
return
req
,
errors
.
New
(
"解密错误"
)
}
req
.
Head
=
creq
.
Request
.
Head
err
=
json
.
Unmarshal
(
tx
,
req
.
Request
)
if
err
!=
nil
{
log
.
Println
(
err
)
return
req
,
err
}
return
req
return
req
,
err
}
else
{
return
req
,
errors
.
New
(
"验签错误"
)
}
return
req
}
nltconst/const.go
View file @
871afdc6
...
...
@@ -15,6 +15,8 @@ const (
PERSON_INFO_NO_MATCH
=
"W380002"
NO_ACCESS_BUSI
=
"W380003"
DATA_LOADING
=
"W380004"
VERIFY_ERROR
=
"W380005"
DECRYPT_ERROR
=
"W380006"
)
func
(
c
NLTStatus
)
String
()
string
{
...
...
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