PaymentCenter/app/services/thirdpay/api/crypt.go

45 lines
1.1 KiB
Go

package api
import (
"PaymentCenter/app/constants/errorcode"
"PaymentCenter/app/models/appmodel"
"PaymentCenter/app/services"
"PaymentCenter/app/services/apicrypt"
"encoding/json"
)
func EnCrypt(app *appmodel.App, data interface{}) ([]byte, int) {
appCheck := services.NewAppCheck(app)
cryptFunc := appCheck.Crypt()
if cryptFunc == nil {
return nil, appCheck.Code
}
dataByte, err := json.Marshal(data)
if err != nil {
return nil, errorcode.AppAesEncryptFail
}
encryptData, errCode := cryptFunc(appCheck.App).Encrypt(string(dataByte))
if errCode != apicrypt.CryptNotError {
return nil, errCode
}
return encryptData, errorcode.Success
}
func DeCrypt(app *appmodel.App, data string, aesKey string) ([]byte, int) {
appCheck := services.NewAppCheck(app)
cryptFunc := appCheck.Crypt()
if cryptFunc == nil {
return nil, appCheck.Code
}
dataByte, errCode := cryptFunc(app).Decrypt(data)
if errCode != apicrypt.CryptNotError {
return nil, errCode
}
if len(dataByte) == 0 {
return nil, errorcode.AppDeEncryptFail
}
return dataByte, errorcode.Success
}