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
c92d6aa7
Commit
c92d6aa7
authored
Sep 29, 2024
by
dliangx
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加对方的公钥
parent
3f041f35
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
10 deletions
+37
-10
model/http_body.go
+36
-10
nltconst/crypt.go
+1
-0
No files found.
model/http_body.go
View file @
c92d6aa7
...
@@ -2,6 +2,8 @@ package model
...
@@ -2,6 +2,8 @@ package model
import
(
import
(
"crypto/rand"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"encoding/json"
"encoding/json"
"errors"
"errors"
"log"
"log"
...
@@ -12,13 +14,13 @@ import (
...
@@ -12,13 +14,13 @@ import (
)
)
type
CryptHttpBodyReq
struct
{
type
CryptHttpBodyReq
struct
{
Request
HttpBodyReq
[
[]
byte
]
Request
HttpBodyReq
[
string
]
Signature
[]
byte
Signature
string
}
}
type
CryptHttpBodyResp
struct
{
type
CryptHttpBodyResp
struct
{
Response
HttpBodyResp
[
[]
byte
]
Response
HttpBodyResp
[
string
]
Signature
[]
byte
Signature
string
}
}
type
HttpBodyReq
[
T
any
]
struct
{
type
HttpBodyReq
[
T
any
]
struct
{
...
@@ -47,7 +49,13 @@ func EncryptAndSign[T any](resp HttpBodyResp[T]) (CryptHttpBodyResp, error) {
...
@@ -47,7 +49,13 @@ func EncryptAndSign[T any](resp HttpBodyResp[T]) (CryptHttpBodyResp, error) {
privateKeyBytes
,
_
:=
sm2
.
GenerateKey
(
strings
.
NewReader
(
nltconst
.
SM2_PRIVATE_KEY
))
privateKeyBytes
,
_
:=
sm2
.
GenerateKey
(
strings
.
NewReader
(
nltconst
.
SM2_PRIVATE_KEY
))
// 对应的公钥
// 对应的公钥
publicKey
:=
&
privateKeyBytes
.
PublicKey
publicKeyBytes
,
err
:=
hex
.
DecodeString
(
nltconst
.
BANK_PUBLIC_KEY
)
if
err
!=
nil
{
log
.
Println
(
err
.
Error
())
return
cresp
,
err
}
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
())
...
@@ -59,7 +67,7 @@ func EncryptAndSign[T any](resp HttpBodyResp[T]) (CryptHttpBodyResp, error) {
...
@@ -59,7 +67,7 @@ func EncryptAndSign[T any](resp HttpBodyResp[T]) (CryptHttpBodyResp, error) {
return
cresp
,
err
return
cresp
,
err
}
}
cresp
.
Response
.
Head
=
resp
.
Head
cresp
.
Response
.
Head
=
resp
.
Head
cresp
.
Response
.
Response
=
ciphertext
cresp
.
Response
.
Response
=
base64
.
RawStdEncoding
.
EncodeToString
(
ciphertext
)
jsonResp
,
err
:=
json
.
Marshal
(
resp
)
jsonResp
,
err
:=
json
.
Marshal
(
resp
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -77,22 +85,40 @@ func EncryptAndSign[T any](resp HttpBodyResp[T]) (CryptHttpBodyResp, error) {
...
@@ -77,22 +85,40 @@ func EncryptAndSign[T any](resp HttpBodyResp[T]) (CryptHttpBodyResp, error) {
log
.
Println
(
err
)
log
.
Println
(
err
)
return
cresp
,
err
return
cresp
,
err
}
}
cresp
.
Signature
=
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
,
_
:=
sm2
.
GenerateKey
(
strings
.
NewReader
(
nltconst
.
SM2_PRIVATE_KEY
))
signature
:=
creq
.
Signature
// 对应的公钥
publicKeyBytes
,
err
:=
hex
.
DecodeString
(
nltconst
.
BANK_PUBLIC_KEY
)
if
err
!=
nil
{
log
.
Println
(
err
.
Error
())
}
publicKey
:=
sm2
.
Decompress
(
publicKeyBytes
)
signature
,
err
:=
base64
.
RawStdEncoding
.
DecodeString
(
creq
.
Signature
)
if
err
!=
nil
{
log
.
Println
(
err
)
return
req
,
err
}
r
,
s
,
err
:=
sm2
.
SignDataToSignDigit
(
signature
)
r
,
s
,
err
:=
sm2
.
SignDataToSignDigit
(
signature
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
err
)
log
.
Println
(
err
)
return
req
,
err
return
req
,
err
}
}
uid
:=
[]
byte
(
"tk"
)
uid
:=
[]
byte
(
"tk"
)
if
sm2
.
Sm2Verify
(
&
privateKeyBytes
.
PublicKey
,
creq
.
Signature
,
uid
,
r
,
s
)
{
if
sm2
.
Sm2Verify
(
publicKey
,
signature
,
uid
,
r
,
s
)
{
tx
,
err
:=
sm2
.
Decrypt
(
privateKeyBytes
,
creq
.
Request
.
Request
,
sm2
.
C1C2C3
)
body
,
err
:=
base64
.
RawStdEncoding
.
DecodeString
(
creq
.
Request
.
Request
)
if
err
!=
nil
{
log
.
Println
(
err
)
return
req
,
errors
.
New
(
"解密错误"
)
}
tx
,
err
:=
sm2
.
Decrypt
(
privateKeyBytes
,
body
,
sm2
.
C1C2C3
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Println
(
err
)
log
.
Println
(
err
)
return
req
,
errors
.
New
(
"解密错误"
)
return
req
,
errors
.
New
(
"解密错误"
)
...
...
nltconst/crypt.go
View file @
c92d6aa7
...
@@ -2,3 +2,4 @@ package nltconst
...
@@ -2,3 +2,4 @@ package nltconst
const
SM2_PUBLIC_KEY
=
"A7CD09260A67113F988F530154AD6A70B2A4DD3E00BD27BB124E7E7051FC0C97E7AC3C5A6CB6C9BB459BEF252761AD1AE727718498CA3130D67CFC84F9B1BB1F"
const
SM2_PUBLIC_KEY
=
"A7CD09260A67113F988F530154AD6A70B2A4DD3E00BD27BB124E7E7051FC0C97E7AC3C5A6CB6C9BB459BEF252761AD1AE727718498CA3130D67CFC84F9B1BB1F"
const
SM2_PRIVATE_KEY
=
"BF6CA99BC05A05C8B4F916A8C6187E5A68207A7B7D89ACC7F478B7E3AFA29454"
const
SM2_PRIVATE_KEY
=
"BF6CA99BC05A05C8B4F916A8C6187E5A68207A7B7D89ACC7F478B7E3AFA29454"
const
BANK_PUBLIC_KEY
=
"0429a440e2fd06f3f0ce3c2fab61a8d1e0b13a1d78d75bac0447ce44cfa263de3525f30c5d8dc9f65120a3d5d09c0692b0e4361cb7a1894d68a6da22b0796b02b0"
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