Skip to content

Commit

Permalink
Merge pull request #76 from ArtisanCloud/feature/httpRequest
Browse files Browse the repository at this point in the history
feat(fix): rsa encypt with public key
  • Loading branch information
Matrix-X authored Apr 2, 2023
2 parents d2a9911 + d207000 commit 1a09c0e
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions security/encryption/rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (encryptor *RSAEncryptor) SavePrivateKeyByPath(path string) (err error) {
if err != nil {
return err
}
err = privateKeyFile.Close()
//err = privateKeyFile.Close()

return err
}
Expand Down Expand Up @@ -115,7 +115,7 @@ func (encryptor *RSAEncryptor) SavePublicKeyByPath(path string) (err error) {
if err != nil {
return err
}
err = publicKeyFile.Close()
//err = publicKeyFile.Close()

return err
}
Expand Down Expand Up @@ -207,14 +207,15 @@ func (encryptor *RSAEncryptor) ParseRSAPublicKeyFromPEM(key []byte) (*rsa.Public

// Encrypt
// hash recommend use sha256.New() as hash
func (encryptor *RSAEncryptor) Encrypt( data []byte) ([]byte, error) {
hash:=encryptor.Hash.New()
return rsa.EncryptOAEP(hash, rand.Reader, &(encryptor.PrivateKey.PublicKey), data, nil)
func (encryptor *RSAEncryptor) Encrypt(data []byte) ([]byte, error) {
//hash := encryptor.Hash.New()
return rsa.EncryptOAEP(encryptor.Hash.New(), rand.Reader, encryptor.PublicKey, data, nil)
//return rsa.EncryptOAEP(hash, rand.Reader, &(encryptor.PrivateKey.PublicKey), data, nil)
}

// Decryption
// optHash recommend use crypto.SHA256 as hash
func (encryptor *RSAEncryptor) Decryption(ciphertext []byte) (plainText []byte, err error) {
optHash:=encryptor.Hash
optHash := encryptor.Hash
return encryptor.PrivateKey.Decrypt(nil, ciphertext, &rsa.OAEPOptions{Hash: optHash})
}

0 comments on commit 1a09c0e

Please sign in to comment.