@@ -28,6 +28,10 @@ func RSAGenerateKey(bits int, out io.Writer) error {
2828// RSAGeneratePublicKey generate RSA public key
2929func RSAGeneratePublicKey (priKey []byte , out io.Writer ) error {
3030 block , _ := pem .Decode (priKey )
31+ if block == nil {
32+ return errors .New ("key is invalid format" )
33+ }
34+
3135 // x509 parse
3236 privateKey , err := x509 .ParsePKCS1PrivateKey (block .Bytes )
3337 if err != nil {
@@ -47,6 +51,10 @@ func RSAGeneratePublicKey(priKey []byte, out io.Writer) error {
4751// RSAEncrypt RSA encrypt
4852func RSAEncrypt (src , pubKey []byte ) ([]byte , error ) {
4953 block , _ := pem .Decode (pubKey )
54+ if block == nil {
55+ return nil , errors .New ("key is invalid format" )
56+ }
57+
5058 // x509 parse
5159 publicKeyInterface , err := x509 .ParsePKIXPublicKey (block .Bytes )
5260 if err != nil {
@@ -69,6 +77,10 @@ func RSAEncrypt(src, pubKey []byte) ([]byte, error) {
6977// RSADecrypt RSA decrypt
7078func RSADecrypt (src , priKey []byte ) ([]byte , error ) {
7179 block , _ := pem .Decode (priKey )
80+ if block == nil {
81+ return nil , errors .New ("key is invalid format" )
82+ }
83+
7284 // x509 parse
7385 privateKey , err := x509 .ParsePKCS1PrivateKey (block .Bytes )
7486 if err != nil {
@@ -86,6 +98,10 @@ func RSADecrypt(src, priKey []byte) ([]byte, error) {
8698// RSASign RSA sign, use crypto.SHA256
8799func RSASign (src []byte , priKey []byte ) ([]byte , error ) {
88100 block , _ := pem .Decode (priKey )
101+ if block == nil {
102+ return nil , errors .New ("key is invalid format" )
103+ }
104+
89105 // x509 parse
90106 privateKey , err := x509 .ParsePKCS1PrivateKey (block .Bytes )
91107 if err != nil {
@@ -110,6 +126,10 @@ func RSASign(src []byte, priKey []byte) ([]byte, error) {
110126// RSAVerify RSA Verify
111127func RSAVerify (src , sign , pubKey []byte ) error {
112128 block , _ := pem .Decode (pubKey )
129+ if block == nil {
130+ return errors .New ("key is invalid format" )
131+ }
132+
113133 // x509 parse
114134 publicKeyInterface , err := x509 .ParsePKIXPublicKey (block .Bytes )
115135 if err != nil {
0 commit comments