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
a7e90556
Commit
a7e90556
authored
Oct 09, 2024
by
dliangx
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
测试
parent
3490b573
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
56 additions
and
43 deletions
+56
-43
handler/pay_result.go
+3
-1
handler/pay_result_test.go
+1
-19
model/http_body.go
+45
-21
nltconst/crypt.go
+7
-2
No files found.
handler/pay_result.go
View file @
a7e90556
...
@@ -22,6 +22,7 @@ func QueryPayResult(ctx context.Context, c *app.RequestContext) {
...
@@ -22,6 +22,7 @@ func QueryPayResult(ctx context.Context, c *app.RequestContext) {
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
=
creq
.
Request
.
Head
.
ServiceSn
cresp
.
Response
.
Head
=
resp
.
Head
cresp
.
Response
.
Head
=
resp
.
Head
c
.
JSON
(
consts
.
StatusOK
,
cresp
)
c
.
JSON
(
consts
.
StatusOK
,
cresp
)
return
return
...
@@ -36,6 +37,7 @@ func QueryPayResult(ctx context.Context, c *app.RequestContext) {
...
@@ -36,6 +37,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
=
creq
.
Request
.
Head
.
ServiceSn
cresp
.
Response
.
Head
=
resp
.
Head
cresp
.
Response
.
Head
=
resp
.
Head
c
.
JSON
(
consts
.
StatusOK
,
cresp
)
c
.
JSON
(
consts
.
StatusOK
,
cresp
)
return
return
...
@@ -58,7 +60,7 @@ func QueryPayResult(ctx context.Context, c *app.RequestContext) {
...
@@ -58,7 +60,7 @@ func QueryPayResult(ctx context.Context, c *app.RequestContext) {
var
count
int
var
count
int
for
rows
.
Next
()
{
for
rows
.
Next
()
{
count
++
count
++
err
:=
rows
.
Scan
(
&
count
,
&
resp
.
Response
.
DuebillNoOrg
,
&
resp
.
Response
.
Status
,
&
resp
.
Response
.
OrderTime
,
&
resp
.
Response
.
FailMsg
)
err
:=
rows
.
Scan
(
&
resp
.
Response
.
DuebillNoOrg
,
&
resp
.
Response
.
Status
,
&
resp
.
Response
.
OrderTime
,
&
resp
.
Response
.
FailMsg
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
err
)
log
.
Println
(
err
)
}
}
...
...
handler/pay_result_test.go
View file @
a7e90556
...
@@ -11,7 +11,7 @@ import (
...
@@ -11,7 +11,7 @@ import (
func
TestAuthEncrypt
(
t
*
testing
.
T
)
{
func
TestAuthEncrypt
(
t
*
testing
.
T
)
{
var
resp
model
.
HttpBodyResp
[
model
.
AuthResp
]
var
resp
model
.
HttpBodyResp
[
model
.
AuthResp
]
resp
.
Head
.
Code
=
"11111"
resp
.
Head
.
Code
=
"11111"
resp
.
Head
.
ServiceSn
=
"
23555555
"
resp
.
Head
.
ServiceSn
=
"
1234567
"
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Response
.
DirectURL
=
"https://baidu.com"
resp
.
Response
.
DirectURL
=
"https://baidu.com"
resp
.
Response
.
PayOrderNo
=
"2239999999"
resp
.
Response
.
PayOrderNo
=
"2239999999"
...
@@ -19,21 +19,3 @@ func TestAuthEncrypt(t *testing.T) {
...
@@ -19,21 +19,3 @@ func TestAuthEncrypt(t *testing.T) {
fmt
.
Println
(
cresp
)
fmt
.
Println
(
cresp
)
fmt
.
Println
(
err
)
fmt
.
Println
(
err
)
}
}
func
TestAuthDecrypt
(
t
*
testing
.
T
)
{
var
resp
model
.
HttpBodyResp
[
model
.
PayResultReq
]
resp
.
Head
.
Code
=
"11111"
resp
.
Head
.
ServiceSn
=
"23555555"
resp
.
Head
.
ServiceTime
=
nltconst
.
GetNowTime
()
resp
.
Response
.
DuebillNoOrg
=
"99999999999"
cresp
,
err
:=
model
.
EncryptAndSign
(
resp
)
if
err
!=
nil
{
fmt
.
Println
(
err
)
}
var
res
model
.
CryptHttpBodyReq
res
.
Request
.
Head
.
ServiceSn
=
cresp
.
Response
.
Head
.
ServiceSn
res
.
Request
.
Request
=
cresp
.
Response
.
Response
res
.
Signature
=
cresp
.
Signature
fmt
.
Println
(
res
)
model
.
VerifyAndDecrypt
[
model
.
AuthResp
](
res
)
}
model/http_body.go
View file @
a7e90556
package
model
package
model
import
(
import
(
"crypto"
"crypto/rand"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"encoding/hex"
"encoding/json"
"encoding/json"
"errors"
"errors"
"fmt"
"log"
"log"
"
strings
"
"
math/big
"
"github.com/tjfoc/gmsm/sm2"
"github.com/tjfoc/gmsm/sm2"
"nlt.com/pf/nltconst"
"nlt.com/pf/nltconst"
...
@@ -45,79 +44,104 @@ type RespHead struct {
...
@@ -45,79 +44,104 @@ type RespHead struct {
ServiceSn
string
`json:"serviceSn"`
ServiceSn
string
`json:"serviceSn"`
}
}
func
hexToPrivateKey
(
hexPrivKey
string
)
(
*
sm2
.
PrivateKey
,
error
)
{
// 解码Hex字符串
privKeyBytes
,
err
:=
hex
.
DecodeString
(
hexPrivKey
)
if
err
!=
nil
{
return
nil
,
err
}
// 创建私钥对象
privateKey
:=
new
(
sm2
.
PrivateKey
)
privateKey
.
PublicKey
.
Curve
=
sm2
.
P256Sm2
()
privateKey
.
D
=
new
(
big
.
Int
)
.
SetBytes
(
privKeyBytes
)
// 计算公钥
privateKey
.
PublicKey
.
X
,
privateKey
.
PublicKey
.
Y
=
privateKey
.
PublicKey
.
Curve
.
ScalarBaseMult
(
privKeyBytes
)
// 验证私钥是否有效
if
!
privateKey
.
PublicKey
.
Curve
.
IsOnCurve
(
privateKey
.
PublicKey
.
X
,
privateKey
.
PublicKey
.
Y
)
{
return
nil
,
fmt
.
Errorf
(
"private key not valid"
)
}
return
privateKey
,
nil
}
func
EncryptAndSign
[
T
any
](
resp
HttpBodyResp
[
T
])
(
CryptHttpBodyResp
,
error
)
{
func
EncryptAndSign
[
T
any
](
resp
HttpBodyResp
[
T
])
(
CryptHttpBodyResp
,
error
)
{
var
cresp
CryptHttpBodyResp
var
cresp
CryptHttpBodyResp
privateKeyBytes
,
_
:=
sm2
.
GenerateKey
(
strings
.
NewReader
(
nltconst
.
SM2_PRIVATE_KEY
)
)
privateKeyBytes
,
_
:=
hexToPrivateKey
(
nltconst
.
SM2_PRIVATE_KEY
)
// 对应的公钥
// 对应的公钥
p
ublicKeyBytes
,
err
:=
hex
.
DecodeString
(
nltconst
.
BANK_PUBLIC
_KEY
)
p
rivateBankey
,
err
:=
hexToPrivateKey
(
nltconst
.
TEST_BANK_PRIVTE
_KEY
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
err
.
Error
()
)
log
.
Println
(
err
)
return
cresp
,
err
return
cresp
,
err
}
}
publicKey
:=
privateBankey
.
PublicKey
publicKey
:=
sm2
.
Decompress
(
publicKeyBytes
)
body
,
err
:=
json
.
Marshal
(
resp
.
Response
)
body
,
err
:=
json
.
Marshal
(
resp
.
Response
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
err
.
Error
())
log
.
Println
(
err
.
Error
())
return
cresp
,
err
return
cresp
,
err
}
}
ciphertext
,
err
:=
sm2
.
Encrypt
(
publicKey
,
body
,
rand
.
Reader
,
sm2
.
C1C2C3
)
ciphertext
,
err
:=
publicKey
.
EncryptAsn1
(
body
,
rand
.
Reader
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
err
)
log
.
Println
(
err
)
return
cresp
,
err
return
cresp
,
err
}
}
cresp
.
Response
.
Head
=
resp
.
Head
cresp
.
Response
.
Head
=
resp
.
Head
cresp
.
Response
.
Response
=
base64
.
RawStdEncoding
.
EncodeToString
(
ciphertext
)
cresp
.
Response
.
Response
=
hex
.
EncodeToString
(
ciphertext
)
jsonResp
,
err
:=
json
.
Marshal
(
cresp
.
Response
)
jsonResp
,
err
:=
json
.
Marshal
(
cresp
.
Response
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
err
)
log
.
Println
(
err
)
return
cresp
,
err
return
cresp
,
err
}
}
fmt
.
Println
(
string
(
jsonResp
))
signature
,
err
:=
privateKeyBytes
.
Sign
(
rand
.
Reader
,
jsonResp
,
nil
)
signature
,
err
:=
privateKeyBytes
.
Sign
(
rand
.
Reader
,
jsonResp
,
crypto
.
BLAKE2b_256
)
cresp
.
Signature
=
hex
.
EncodeToString
(
signature
)
fmt
.
Println
(
cresp
.
Signature
)
cresp
.
Signature
=
base64
.
RawStdEncoding
.
EncodeToString
(
signature
)
return
cresp
,
err
return
cresp
,
err
}
}
func
VerifyAndDecrypt
[
T
any
](
creq
CryptHttpBodyReq
)
(
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
,
_
:=
hexToPrivateKey
(
nltconst
.
SM2_PRIVATE_KEY
)
// 对应的公钥
// 对应的公钥
p
ublicKeyBytes
,
err
:=
hex
.
DecodeString
(
nltconst
.
BANK_PUBLIC
_KEY
)
p
rivateBankey
,
err
:=
hexToPrivateKey
(
nltconst
.
TEST_BANK_PRIVTE
_KEY
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
err
.
Error
())
log
.
Println
(
err
)
return
req
,
err
}
}
publicKey
:=
sm2
.
Decompress
(
publicKeyBytes
)
publicKey
:=
privateBankey
.
PublicKey
signature
,
err
:=
hex
.
DecodeString
(
creq
.
Signature
)
signature
,
err
:=
base64
.
RawStdEncoding
.
DecodeString
(
creq
.
Signature
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
err
)
log
.
Println
(
err
)
return
req
,
err
return
req
,
err
}
}
jsonReq
,
err
:=
json
.
Marshal
(
creq
.
Request
)
jsonReq
,
err
:=
json
.
Marshal
(
creq
.
Request
)
fmt
.
Println
(
creq
.
Request
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
err
)
log
.
Println
(
err
)
return
req
,
err
return
req
,
err
}
}
if
publicKey
.
Verify
(
jsonReq
,
signature
)
{
if
publicKey
.
Verify
(
jsonReq
,
signature
)
{
body
,
err
:=
base64
.
RawStdEncoding
.
DecodeString
(
creq
.
Request
.
Request
)
body
,
err
:=
hex
.
DecodeString
(
creq
.
Request
.
Request
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
err
)
log
.
Println
(
err
)
return
req
,
errors
.
New
(
"解密错误"
)
return
req
,
errors
.
New
(
"解密错误"
)
}
}
tx
,
err
:=
sm2
.
Decrypt
(
privateKeyBytes
,
body
,
sm2
.
C1C
2C3
)
tx
,
err
:=
sm2
.
Decrypt
(
privateKeyBytes
,
body
,
sm2
.
C1C
3C2
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
err
)
log
.
Println
(
err
)
return
req
,
errors
.
New
(
"解密错误"
)
return
req
,
errors
.
New
(
"解密错误"
)
}
}
req
.
Head
=
creq
.
Request
.
Head
req
.
Head
=
creq
.
Request
.
Head
err
=
json
.
Unmarshal
(
tx
,
req
.
Request
)
err
=
json
.
Unmarshal
(
tx
,
&
req
.
Request
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
err
)
log
.
Println
(
err
)
return
req
,
err
return
req
,
err
...
...
nltconst/crypt.go
View file @
a7e90556
package
nltconst
package
nltconst
const
SM2_PUBLIC_KEY
=
"A7CD09260A67113F988F530154AD6A70B2A4DD3E00BD27BB124E7E7051FC0C97E7AC3C5A6CB6C9BB459BEF252761AD1AE727718498CA3130D67CFC84F9B1BB1F"
const
SM2_PUBLIC_KEY
=
"
04
A7CD09260A67113F988F530154AD6A70B2A4DD3E00BD27BB124E7E7051FC0C97E7AC3C5A6CB6C9BB459BEF252761AD1AE727718498CA3130D67CFC84F9B1BB1F"
const
SM2_PRIVATE_KEY
=
"BF6CA99BC05A05C8B4F916A8C6187E5A68207A7B7D89ACC7F478B7E3AFA29454"
const
SM2_PRIVATE_KEY
=
"BF6CA99BC05A05C8B4F916A8C6187E5A68207A7B7D89ACC7F478B7E3AFA29454"
const
BANK_PUBLIC_KEY
=
"0429a440e2fd06f3f0ce3c2fab61a8d1e0b13a1d78d75bac0447ce44cfa263de3525f30c5d8dc9f65120a3d5d09c0692b0e4361cb7a1894d68a6da22b0796b02b0"
// const BANK_PUBLIC_KEY = "0429a440e2fd06f3f0ce3c2fab61a8d1e0b13a1d78d75bac0447ce44cfa263de3525f30c5d8dc9f65120a3d5d09c0692b0e4361cb7a1894d68a6da22b0796b02b0"
const
TEST_BANK_PRIVTE_KEY
=
"308a6311076aa18ec591ce0b300c6f92b1e58438d0ab0962d67b7d163b0e2f8d"
const
TEST_BANK_PUBLIC_KEY
=
"041f41683ee8d5204958db303b16c97a912b1d2a7ee640f767001395ccfcc16c48e9e81ed2b0e540c53a0836040665ef98f2488ee4cc7e9c525d8ecc92e42d62f1"
const
BANK_PUBLIC_KEY
=
"041f41683ee8d5204958db303b16c97a912b1d2a7ee640f767001395ccfcc16c48e9e81ed2b0e540c53a0836040665ef98f2488ee4cc7e9c525d8ecc92e42d62f1"
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