56 lines
1.4 KiB
Go
56 lines
1.4 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
|
|
}
|
|
//aesKey, err := aes.GenerateRandomStringCrypto(16)
|
|
//if err != nil {
|
|
// return "", errorcode.AppAesEncryptFail
|
|
//}
|
|
//aesData, err := aes.Encrypt(dataByte, []byte(aesKey))
|
|
//if err != nil {
|
|
// return "", errorcode.AppAesEncryptFail
|
|
//}
|
|
encryptData, errCode := cryptFunc(appCheck.App).Encrypt(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
|
|
}
|
|
//aesData, err := aes.Decrypt(dataByte, []byte(aesKey))
|
|
//if err != nil {
|
|
// return nil, errorcode.AppAesEncryptFail
|
|
//}
|
|
|
|
return dataByte, errorcode.Success
|
|
}
|