44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
package encrypt
|
|
|
|
import (
|
|
"voucher/internal/pkg/cmb/encrypt/encrypt_way/sm4"
|
|
"voucher/internal/pkg/cmb/encrypt/errcode"
|
|
)
|
|
|
|
func NewSm4(app *AppEncrypt) ApiCrypt {
|
|
return &SM4{
|
|
App: app,
|
|
}
|
|
}
|
|
|
|
func (r *SM4) Encrypt(data string) (encryptData []byte, err error) {
|
|
if r.App.MerchantPublicKey == "" || r.App.PrivateKey == "" {
|
|
return nil, errcode.AppKeyNotFound
|
|
}
|
|
encryptDataString, err := sm4.Sm4Encrypt(r.App.UniKEY, r.App.PrivateKey, r.App.MerchantPublicKey, data, "", true)
|
|
if err != nil {
|
|
return nil, errcode.AppEncryptFail
|
|
}
|
|
encryptData = []byte(encryptDataString)
|
|
return
|
|
}
|
|
|
|
func (r *SM4) Decrypt(encryptData string) (decryptData []byte, err error) {
|
|
defer func() {
|
|
if e := recover(); e != nil {
|
|
err = errcode.AppDecryptFail
|
|
}
|
|
}()
|
|
if r.App.PrivateKey == "" || r.App.MerchantPublicKey == "" {
|
|
return nil, errcode.AppKeyNotFound
|
|
}
|
|
|
|
decryptDataString, err := sm4.Sm4Decrypt(r.App.UniKEY, r.App.PrivateKey, r.App.MerchantPublicKey, encryptData, true)
|
|
if err != nil {
|
|
return nil, errcode.AppDecryptFail
|
|
}
|
|
decryptData = []byte(decryptDataString)
|
|
return
|
|
|
|
}
|