44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
package apicrypt
|
|
|
|
import (
|
|
"PaymentCenter/app/constants/errorcode"
|
|
"PaymentCenter/app/models/appmodel"
|
|
"PaymentCenter/app/utils/encrypt/rsa"
|
|
)
|
|
|
|
func NewRsa(app *appmodel.App) ApiCrypt {
|
|
return &Rsa{
|
|
App: app,
|
|
}
|
|
}
|
|
|
|
func (r *Rsa) Encrypt(decryptData string) (encryptData []byte, errCode int) {
|
|
if r.App.PublicKey == "" {
|
|
return nil, errorcode.AppRsaEncryptKeyNotFound
|
|
}
|
|
publicKeyPEM := `-----BEGIN PUBLIC KEY-----
|
|
` + r.App.PublicKey + `
|
|
-----END PUBLIC KEY-----`
|
|
encryptData, err := rsa.Encrypt(publicKeyPEM, []byte(decryptData))
|
|
if err != nil {
|
|
return nil, errorcode.AppRsaEncryptFail
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (r *Rsa) Decrypt(encryptData string) (decryptData []byte, errCode int) {
|
|
|
|
if r.App.PrivateKey == "" {
|
|
return nil, errorcode.AppRsaDecryptKeyNotFound
|
|
}
|
|
privateKeyPEM := `-----BEGIN RSA PRIVATE KEY-----
|
|
` + r.App.PrivateKey + `
|
|
-----END RSA PRIVATE KEY-----`
|
|
decryptData, err := rsa.Decrypt(privateKeyPEM, encryptData)
|
|
if err != nil || decryptData == nil {
|
|
return nil, errorcode.AppRsaDecryptFail
|
|
}
|
|
return
|
|
}
|