echo-go/crypto/rsa.go
Jake Walker a258da3678
Some checks failed
continuous-integration/drone/push Build is failing
Initial commit
2022-08-03 18:22:47 +01:00

24 lines
503 B
Go

package crypto
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/base64"
"encoding/pem"
)
func RsaEncrypt(pubKey []byte, text []byte) (string, error) {
block, _ := pem.Decode(pubKey)
key, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return "", err
}
k := key.(*rsa.PublicKey)
ciphertext, err := rsa.EncryptOAEP(sha1.New(), rand.Reader, k, text, nil)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(ciphertext), nil
}