PaymentCenter/app/services/apicrypt/sm2.go

41 lines
957 B
Go

package apicrypt
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(data string) (encryptData []byte, errCode int) {
if r.App.MerchantPublicKey == "" {
return nil, errorcode.AppSM2EncryptKeyNotFound
}
encryptDataString, err := sm2.SM2Encrypt(data, r.App.MerchantPublicKey)
if err != nil {
return nil, errorcode.AppSM2EncryptFail
}
encryptData = []byte(encryptDataString)
return
}
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
}