45 lines
970 B
Go
45 lines
970 B
Go
package encrypt
|
|
|
|
import (
|
|
"voucher/internal/pkg/cmb/encrypt/encrypt_way/sm2"
|
|
"voucher/internal/pkg/cmb/encrypt/errcode"
|
|
)
|
|
|
|
func NewSm2(app *AppEncrypt) ApiCrypt {
|
|
return &SM2{
|
|
App: app,
|
|
}
|
|
}
|
|
|
|
func (r *SM2) Encrypt(data string) (encryptData []byte, err error) {
|
|
|
|
if r.App.PrivateKey == "" {
|
|
return nil, errcode.AppKeyNotFound
|
|
}
|
|
|
|
encryptDataString, err := sm2.SM2Encrypt(data, r.App.PrivateKey)
|
|
if err != nil {
|
|
return nil, errcode.AppEncryptFail
|
|
}
|
|
encryptData = []byte(encryptDataString)
|
|
return
|
|
}
|
|
|
|
func (r *SM2) Decrypt(encryptData string) (decryptData []byte, err error) {
|
|
defer func() {
|
|
if e := recover(); e != nil {
|
|
err = errcode.AppDecryptFail
|
|
}
|
|
}()
|
|
if r.App.PrivateKey == "" || r.App.PublicKey == "" {
|
|
return nil, errcode.AppKeyNotFound
|
|
}
|
|
|
|
decryptDataString, err := sm2.SM2Decrypt(encryptData, r.App.PublicKey, r.App.PrivateKey)
|
|
if err != nil {
|
|
return nil, errcode.AppDecryptFail
|
|
}
|
|
decryptData = []byte(decryptDataString)
|
|
return
|
|
}
|