Commit 9212bdf5 by dliangx

add common test

parent 8b89145e
......@@ -24,3 +24,26 @@ func TestAuthEncrypt(t *testing.T) {
fmt.Println(string(jsonResp))
}
func TestEncrypt(t *testing.T) {
var req model.HttpBody[model.ReqHead, model.AuthReq]
req.Head.BusinessChannel = "spdbmbank"
req.Head.RequestTime = "20550623072501"
req.Head.VersionId = "V1.0"
req.Head.ChannelId = "APP-01"
req.Head.ServiceSn = "00175506230725010158298153"
req.Head.Sid = "00175506230725010158298153"
req.Head.ServiceId = "pfyhcxzfjg"
req.Body.Addr = ""
req.Body.UserName = "龙一"
req.Body.IdNo = "420982198604026032"
req.Body.DuebillNoOrg = "1234567890"
req.Body.LoanAmt = "2000000"
req.Body.Term = "12"
req.Body.CellPhone = "1311111111"
req.Body.Addr = "234567890"
req.Body.JumURL = "www.baidu.com"
model.Ecas[model.ReqHead, model.AuthReq](req)
}
......@@ -33,9 +33,29 @@ type HttpBodyResp[T any] struct {
Response T `json:"body"`
}
type HttpBody[T any, V any] struct {
Head T `json:"head"`
Body V `json:"body"`
}
type CryptReq[T any] struct {
Request T `json:"request"`
Signature string `json:"signature"`
}
type CryptResp[T any] struct {
Response T `json:"response"`
Signature string `json:"signature"`
}
type ReqHead struct {
RequestTime string `json:"requestTime"`
ServiceSn string `json:"serviceSn"`
RequestTime string `json:"requestTime"`
VersionId string `json:"versionId"`
ServiceId string `json:"serviceId"`
ServiceSn string `json:"serviceSn"`
ChannelId string `json:"channelId"`
Sid string `json:"sid"`
BusinessChannel string `json:"businessChannel"`
}
type RespHead struct {
......@@ -66,6 +86,46 @@ func hexToPrivateKey(hexPrivKey string) (*sm2.PrivateKey, error) {
return privateKey, nil
}
func Ecas[T any, V any](req HttpBody[T, V]) (cresp CryptReq[HttpBody[T, string]], err error) {
privateKeyBytes, _ := hexToPrivateKey(nltconst.TEST_BANK_PRIVTE_KEY)
// 对应的公钥
privateBankey, err := hexToPrivateKey(nltconst.SM2_PRIVATE_KEY)
if err != nil {
log.Println(err)
return cresp, err
}
publicKey := privateBankey.PublicKey
body, err := json.Marshal(req.Body)
if err != nil {
log.Println(err.Error())
return cresp, err
}
ciphertext, err := sm2.Encrypt(&publicKey, body, rand.Reader, sm2.C1C3C2)
if err != nil {
log.Println(err)
return cresp, err
}
cresp.Request.Head = req.Head
cresp.Request.Body = hex.EncodeToString(ciphertext)
jsonResp, err := json.Marshal(cresp.Request)
if err != nil {
log.Println(err)
return cresp, err
}
fmt.Println(string(jsonResp))
signature, err := privateKeyBytes.Sign(rand.Reader, jsonResp, nil)
cresp.Signature = hex.EncodeToString(signature)
crespjson, _ := json.Marshal(cresp)
fmt.Println(string(crespjson))
return cresp, err
}
func EncryptAndSign[T any](resp HttpBodyResp[T]) (CryptHttpBodyResp, error) {
var cresp CryptHttpBodyResp
privateKeyBytes, _ := hexToPrivateKey(nltconst.SM2_PRIVATE_KEY)
......
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