加密解密
This commit is contained in:
parent
6e6e01a823
commit
13dce2d1c3
|
@ -29,9 +29,21 @@ const (
|
||||||
MerchantNotFound = 1100
|
MerchantNotFound = 1100
|
||||||
|
|
||||||
// app
|
// app
|
||||||
AppNotFound = 1200
|
AppNotFound = 1200
|
||||||
AppDisabled = 1201
|
AppDisabled = 1201
|
||||||
AppIpNotAllow = 1202
|
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{
|
var MsgEN = map[int]string{
|
||||||
|
@ -54,6 +66,21 @@ var MsgZH = map[int]string{
|
||||||
AppNotFound: "app_id未找到",
|
AppNotFound: "app_id未找到",
|
||||||
AppDisabled: "app通道关闭",
|
AppDisabled: "app通道关闭",
|
||||||
AppIpNotAllow: "ip不在白名单内",
|
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}
|
var MsgMap map[string]map[int]string = map[string]map[int]string{"en": MsgZH}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package pojo
|
package pojo
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Rsa int32 = iota + 1
|
RSA int32 = iota + 1
|
||||||
Sm2
|
SM2
|
||||||
Sm4
|
SM4
|
||||||
)
|
)
|
||||||
|
|
|
@ -5,6 +5,12 @@ type PayCommonBody struct {
|
||||||
Timestamp int64 `json:"timestamp" validate:"required"`
|
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 {
|
type PayWeb struct {
|
||||||
PayCommonBody
|
PayCommonBody
|
||||||
PayChannel int64 `json:"private_key_path"`
|
PayChannel int64 `json:"private_key_path"`
|
||||||
|
|
|
@ -109,11 +109,11 @@ func ValidateRequest() gin.HandlerFunc {
|
||||||
|
|
||||||
func ValidatePayRequest() gin.HandlerFunc {
|
func ValidatePayRequest() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
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 {
|
if err != nil {
|
||||||
controllers.ErrWithCode(c, errorcode.ParamError)
|
controllers.ErrWithCode(c, errorcode.ParamError)
|
||||||
}
|
}
|
||||||
comStruct := com.(*front.PayCommonBody)
|
commonDataStruct := commonData.(*front.RequestBody)
|
||||||
//判断时间
|
//判断时间
|
||||||
//now := time.Now().UnixNano() / 1000000
|
//now := time.Now().UnixNano() / 1000000
|
||||||
//if comStruct.Timestamp > now || (config.GetConf().TimeOut != 0 && (now-comStruct.Timestamp) > config.GetConf().TimeOut) {
|
//if comStruct.Timestamp > now || (config.GetConf().TimeOut != 0 && (now-comStruct.Timestamp) > config.GetConf().TimeOut) {
|
||||||
|
@ -121,7 +121,7 @@ func ValidatePayRequest() gin.HandlerFunc {
|
||||||
// return
|
// return
|
||||||
//}
|
//}
|
||||||
//获取app信息
|
//获取app信息
|
||||||
app, errCode := services.AppFindOne(entities.IdRequest{Id: comStruct.AppId})
|
app, errCode := services.AppFindOne(entities.IdRequest{Id: commonDataStruct.AppId})
|
||||||
if errCode != errorcode.Success {
|
if errCode != errorcode.Success {
|
||||||
controllers.ErrWithCode(c, errCode)
|
controllers.ErrWithCode(c, errCode)
|
||||||
return
|
return
|
||||||
|
@ -137,7 +137,17 @@ func ValidatePayRequest() gin.HandlerFunc {
|
||||||
controllers.ErrWithCode(c, appCheck.GetCode())
|
controllers.ErrWithCode(c, appCheck.GetCode())
|
||||||
return
|
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()
|
c.Next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"PaymentCenter/app/constants/errorcode"
|
"PaymentCenter/app/constants/errorcode"
|
||||||
"PaymentCenter/app/constants/pojo"
|
"PaymentCenter/app/constants/pojo"
|
||||||
"PaymentCenter/app/models/appmodel"
|
"PaymentCenter/app/models/appmodel"
|
||||||
|
"PaymentCenter/app/services/apicrypt"
|
||||||
"PaymentCenter/app/utils"
|
"PaymentCenter/app/utils"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -35,9 +36,11 @@ func (a *AppCheck) Check() *AppCheck {
|
||||||
|
|
||||||
if a.App.Status == pojo.STATUS_DISABLED {
|
if a.App.Status == pojo.STATUS_DISABLED {
|
||||||
a.Code = errorcode.AppDisabled
|
a.Code = errorcode.AppDisabled
|
||||||
|
return a
|
||||||
}
|
}
|
||||||
if a.App.DeleteTime.Location() == nil {
|
if a.App.DeleteTime.Location() == nil {
|
||||||
a.Code = errorcode.AppNotFound
|
a.Code = errorcode.AppNotFound
|
||||||
|
return a
|
||||||
}
|
}
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
@ -45,3 +48,14 @@ func (a *AppCheck) Check() *AppCheck {
|
||||||
func (a *AppCheck) GetCode() int {
|
func (a *AppCheck) GetCode() int {
|
||||||
return a.Code
|
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
|
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{
|
return &Rsa{
|
||||||
App: app,
|
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
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,39 @@
|
||||||
package apicrypt
|
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
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,41 @@
|
||||||
package apicrypt
|
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
|
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
|
return
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
package apicrypt
|
package apicrypt
|
||||||
|
|
||||||
import "PaymentCenter/app/models/appmodel"
|
import (
|
||||||
|
"PaymentCenter/app/constants/pojo"
|
||||||
|
"PaymentCenter/app/models/appmodel"
|
||||||
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
ApiDecrypt interface {
|
ApiCrypt interface {
|
||||||
Encrypt(decryptData interface{}) (encryptData string, err error)
|
Encrypt(decryptData string) (encryptData []byte, errCode int)
|
||||||
Decrypt(encryptData string) (decryptData map[string]interface{}, err error)
|
Decrypt(encryptData string) (decryptData []byte, errCode int)
|
||||||
}
|
}
|
||||||
|
|
||||||
Rsa struct {
|
Rsa struct {
|
||||||
|
@ -20,3 +23,9 @@ type (
|
||||||
App *appmodel.App
|
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