加密解密
This commit is contained in:
parent
6e6e01a823
commit
13dce2d1c3
app
constants
http
services
|
@ -29,9 +29,21 @@ const (
|
|||
MerchantNotFound = 1100
|
||||
|
||||
// app
|
||||
AppNotFound = 1200
|
||||
AppDisabled = 1201
|
||||
AppIpNotAllow = 1202
|
||||
AppNotFound = 1200
|
||||
AppDisabled = 1201
|
||||
AppIpNotAllow = 1202
|
||||
AppRsaDecryptKeyNotFound = 1300
|
||||
AppRsaDecryptFail = 1301
|
||||
AppRsaEncryptKeyNotFound = 1302
|
||||
AppRsaEncryptFail = 1303
|
||||
AppSM2DecryptKeyNotFound = 1310
|
||||
AppSM2DecryptFail = 1311
|
||||
AppSM2EncryptKeyNotFound = 1312
|
||||
AppSM2EncryptFail = 1313
|
||||
AppSM4DecryptKeyNotFound = 1320
|
||||
AppSM4DecryptFail = 1321
|
||||
AppSM4EncryptKeyNotFound = 1322
|
||||
AppSM4EncryptFail = 1323
|
||||
)
|
||||
|
||||
var MsgEN = map[int]string{
|
||||
|
@ -54,6 +66,21 @@ var MsgZH = map[int]string{
|
|||
AppNotFound: "app_id未找到",
|
||||
AppDisabled: "app通道关闭",
|
||||
AppIpNotAllow: "ip不在白名单内",
|
||||
|
||||
AppRsaDecryptKeyNotFound: "密匙缺失,无法进行Rsa解密",
|
||||
AppRsaDecryptFail: "Rsa解密失败",
|
||||
AppRsaEncryptKeyNotFound: "密匙缺失,无法进行Rsa加密",
|
||||
AppRsaEncryptFail: "Rsa加密失败",
|
||||
|
||||
AppSM2DecryptKeyNotFound: "密匙缺失,无法进行sm2解密",
|
||||
AppSM2DecryptFail: "sm2解密失败",
|
||||
AppSM2EncryptKeyNotFound: "密匙缺失,无法进行sm2加密",
|
||||
AppSM2EncryptFail: "sm2加密失败",
|
||||
|
||||
AppSM4DecryptKeyNotFound: "密匙缺失,无法进行sm4解密",
|
||||
AppSM4DecryptFail: "sm4解密失败",
|
||||
AppSM4EncryptKeyNotFound: "密匙缺失,无法进行sm4加密",
|
||||
AppSM4EncryptFail: "sm4加密失败",
|
||||
}
|
||||
var MsgMap map[string]map[int]string = map[string]map[int]string{"en": MsgZH}
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package pojo
|
||||
|
||||
const (
|
||||
Rsa int32 = iota + 1
|
||||
Sm2
|
||||
Sm4
|
||||
RSA int32 = iota + 1
|
||||
SM2
|
||||
SM4
|
||||
)
|
||||
|
|
|
@ -5,6 +5,12 @@ type PayCommonBody struct {
|
|||
Timestamp int64 `json:"timestamp" validate:"required"`
|
||||
}
|
||||
|
||||
type RequestBody struct {
|
||||
AppId int64 `json:"app_id" validate:"required"`
|
||||
Timestamp int64 `json:"timestamp" validate:"required"`
|
||||
Data string `json:"data" validate:"required"`
|
||||
}
|
||||
|
||||
type PayWeb struct {
|
||||
PayCommonBody
|
||||
PayChannel int64 `json:"private_key_path"`
|
||||
|
|
|
@ -109,11 +109,11 @@ func ValidateRequest() gin.HandlerFunc {
|
|||
|
||||
func ValidatePayRequest() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
com, err := utils.SonicApiDataToStruct(controllers.GetRequest(c), &front.PayCommonBody{})
|
||||
commonData, err := utils.SonicApiDataToStruct(controllers.GetRequest(c), &front.RequestBody{})
|
||||
if err != nil {
|
||||
controllers.ErrWithCode(c, errorcode.ParamError)
|
||||
}
|
||||
comStruct := com.(*front.PayCommonBody)
|
||||
commonDataStruct := commonData.(*front.RequestBody)
|
||||
//判断时间
|
||||
//now := time.Now().UnixNano() / 1000000
|
||||
//if comStruct.Timestamp > now || (config.GetConf().TimeOut != 0 && (now-comStruct.Timestamp) > config.GetConf().TimeOut) {
|
||||
|
@ -121,7 +121,7 @@ func ValidatePayRequest() gin.HandlerFunc {
|
|||
// return
|
||||
//}
|
||||
//获取app信息
|
||||
app, errCode := services.AppFindOne(entities.IdRequest{Id: comStruct.AppId})
|
||||
app, errCode := services.AppFindOne(entities.IdRequest{Id: commonDataStruct.AppId})
|
||||
if errCode != errorcode.Success {
|
||||
controllers.ErrWithCode(c, errCode)
|
||||
return
|
||||
|
@ -137,7 +137,17 @@ func ValidatePayRequest() gin.HandlerFunc {
|
|||
controllers.ErrWithCode(c, appCheck.GetCode())
|
||||
return
|
||||
}
|
||||
|
||||
//解密
|
||||
cryptFunc := appCheck.Crypt()
|
||||
if cryptFunc == nil {
|
||||
controllers.ErrWithCode(c, appCheck.GetCode())
|
||||
}
|
||||
data, errCode := cryptFunc(app).Decrypt(commonDataStruct.Data)
|
||||
if errCode != errorcode.Success {
|
||||
controllers.ErrWithCode(c, errCode)
|
||||
return
|
||||
}
|
||||
c.Set("request", data)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"PaymentCenter/app/constants/errorcode"
|
||||
"PaymentCenter/app/constants/pojo"
|
||||
"PaymentCenter/app/models/appmodel"
|
||||
"PaymentCenter/app/services/apicrypt"
|
||||
"PaymentCenter/app/utils"
|
||||
"strings"
|
||||
)
|
||||
|
@ -35,9 +36,11 @@ func (a *AppCheck) Check() *AppCheck {
|
|||
|
||||
if a.App.Status == pojo.STATUS_DISABLED {
|
||||
a.Code = errorcode.AppDisabled
|
||||
return a
|
||||
}
|
||||
if a.App.DeleteTime.Location() == nil {
|
||||
a.Code = errorcode.AppNotFound
|
||||
return a
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
@ -45,3 +48,14 @@ func (a *AppCheck) Check() *AppCheck {
|
|||
func (a *AppCheck) GetCode() int {
|
||||
return a.Code
|
||||
}
|
||||
|
||||
func (a *AppCheck) Crypt() (cryptFunc func(app *appmodel.App) apicrypt.ApiCrypt) {
|
||||
var (
|
||||
ok bool
|
||||
)
|
||||
if cryptFunc, ok = apicrypt.ApiCryptMap[a.App.KeyType]; !ok {
|
||||
a.Code = errorcode.AppNotFound
|
||||
return nil
|
||||
}
|
||||
return cryptFunc
|
||||
}
|
||||
|
|
|
@ -1,17 +1,38 @@
|
|||
package apicrypt
|
||||
|
||||
import "PaymentCenter/app/models/appmodel"
|
||||
import (
|
||||
"PaymentCenter/app/constants/errorcode"
|
||||
"PaymentCenter/app/models/appmodel"
|
||||
"PaymentCenter/app/utils/encrypt/rsa"
|
||||
)
|
||||
|
||||
func NewRsa(app *appmodel.App) ApiDecrypt {
|
||||
func NewRsa(app *appmodel.App) ApiCrypt {
|
||||
return &Rsa{
|
||||
App: app,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Rsa) Encrypt(decryptData interface{}) (encryptData string, err error) {
|
||||
func (r *Rsa) Encrypt(decryptData string) (encryptData []byte, errCode int) {
|
||||
if r.App.MerchantPublicKey == "" {
|
||||
return nil, errorcode.AppRsaEncryptKeyNotFound
|
||||
}
|
||||
//
|
||||
encryptData, err := rsa.Encrypt(r.App.MerchantPublicKey, []byte(decryptData))
|
||||
if err != nil {
|
||||
return nil, errorcode.AppRsaEncryptFail
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (r *Rsa) Decrypt(encryptData string) (decryptData map[string]interface{}, err error) {
|
||||
func (r *Rsa) Decrypt(encryptData string) (decryptData []byte, errCode int) {
|
||||
if r.App.PrivateKey == "" {
|
||||
return nil, errorcode.AppRsaDecryptKeyNotFound
|
||||
}
|
||||
|
||||
decryptData, err := rsa.Decrypt(r.App.PrivateKey, encryptData)
|
||||
if err != nil {
|
||||
return nil, errorcode.AppRsaDecryptFail
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -1,9 +1,39 @@
|
|||
package apicrypt
|
||||
|
||||
func (r *SM2) Encrypt(decryptData interface{}) (encryptData string, err error) {
|
||||
import (
|
||||
"PaymentCenter/app/constants/errorcode"
|
||||
"PaymentCenter/app/models/appmodel"
|
||||
"PaymentCenter/app/utils/encrypt/sm2"
|
||||
)
|
||||
|
||||
func NewSm2(app *appmodel.App) ApiCrypt {
|
||||
return &SM2{
|
||||
App: app,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *SM2) Encrypt(decryptData string) (encryptData []byte, errCode int) {
|
||||
if r.App.MerchantPublicKey == "" {
|
||||
return nil, errorcode.AppSM2EncryptKeyNotFound
|
||||
}
|
||||
//
|
||||
encryptDataString, err := sm2.SM2Encrypt(decryptData, r.App.PrivateKey)
|
||||
if err != nil {
|
||||
return nil, errorcode.AppSM2EncryptFail
|
||||
}
|
||||
encryptData = []byte(encryptDataString)
|
||||
return
|
||||
}
|
||||
|
||||
func (r *SM2) Decrypt(encryptData string) (decryptData map[string]interface{}, err error) {
|
||||
func (r *SM2) Decrypt(encryptData string) (decryptData []byte, errCode int) {
|
||||
if r.App.PrivateKey == "" || r.App.PublicKey == "" {
|
||||
return nil, errorcode.AppSM2DecryptKeyNotFound
|
||||
}
|
||||
|
||||
decryptDataString, err := sm2.SM2Decrypt(encryptData, r.App.PublicKey, r.App.PrivateKey)
|
||||
if err != nil {
|
||||
return nil, errorcode.AppSM2DecryptFail
|
||||
}
|
||||
decryptData = []byte(decryptDataString)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -1,9 +1,41 @@
|
|||
package apicrypt
|
||||
|
||||
func (r *SM4) Encrypt(decryptData interface{}) (encryptData string, err error) {
|
||||
import (
|
||||
"PaymentCenter/app/constants/errorcode"
|
||||
"PaymentCenter/app/models/appmodel"
|
||||
"PaymentCenter/app/utils/encrypt/sm4"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func NewSm4(app *appmodel.App) ApiCrypt {
|
||||
return &SM4{
|
||||
App: app,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *SM4) Encrypt(decryptData string) (encryptData []byte, errCode int) {
|
||||
if r.App.MerchantPublicKey == "" || r.App.PrivateKey == "" {
|
||||
return nil, errorcode.AppSM4DecryptKeyNotFound
|
||||
}
|
||||
|
||||
encryptDataString, err := sm4.Sm4Encrypt(strconv.FormatInt(r.App.Id, 10), r.App.PrivateKey, r.App.MerchantPublicKey, decryptData, "", true)
|
||||
if err != nil {
|
||||
return nil, errorcode.AppSM4EncryptFail
|
||||
}
|
||||
encryptData = []byte(encryptDataString)
|
||||
return
|
||||
}
|
||||
|
||||
func (r *SM4) Decrypt(encryptData string) (decryptData map[string]interface{}, err error) {
|
||||
func (r *SM4) Decrypt(encryptData string) (decryptData []byte, errCode int) {
|
||||
if r.App.PrivateKey == "" || r.App.MerchantPublicKey == "" {
|
||||
return nil, errorcode.AppSM4DecryptKeyNotFound
|
||||
}
|
||||
|
||||
decryptDataString, err := sm4.Sm4Decrypt(strconv.FormatInt(r.App.Id, 10), r.App.PrivateKey, r.App.MerchantPublicKey, encryptData, true)
|
||||
if err != nil {
|
||||
return nil, errorcode.AppSM4DecryptFail
|
||||
}
|
||||
decryptData = []byte(decryptDataString)
|
||||
return
|
||||
|
||||
}
|
||||
|
|
|
@ -1,11 +1,14 @@
|
|||
package apicrypt
|
||||
|
||||
import "PaymentCenter/app/models/appmodel"
|
||||
import (
|
||||
"PaymentCenter/app/constants/pojo"
|
||||
"PaymentCenter/app/models/appmodel"
|
||||
)
|
||||
|
||||
type (
|
||||
ApiDecrypt interface {
|
||||
Encrypt(decryptData interface{}) (encryptData string, err error)
|
||||
Decrypt(encryptData string) (decryptData map[string]interface{}, err error)
|
||||
ApiCrypt interface {
|
||||
Encrypt(decryptData string) (encryptData []byte, errCode int)
|
||||
Decrypt(encryptData string) (decryptData []byte, errCode int)
|
||||
}
|
||||
|
||||
Rsa struct {
|
||||
|
@ -20,3 +23,9 @@ type (
|
|||
App *appmodel.App
|
||||
}
|
||||
)
|
||||
|
||||
var ApiCryptMap = map[int32]func(app *appmodel.App) ApiCrypt{
|
||||
pojo.RSA: NewRsa,
|
||||
pojo.SM2: NewSm2,
|
||||
pojo.SM4: NewSm4,
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue