-
-
Save parzibyte/65d768e36aed405d05d1577befc0618d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func ParseRsaPrivateKeyFromPemStr(privPEM string) (*rsa.PrivateKey, error) { | |
block, _ := pem.Decode([]byte(privPEM)) | |
if block == nil { | |
return nil, errors.New("failed to parse PEM block containing the key") | |
} | |
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes) | |
if err != nil { | |
return nil, err | |
} | |
return priv, nil | |
} | |
func ParseRsaPublicKeyFromPemStr(pubPEM string) (*rsa.PublicKey, error) { | |
block, _ := pem.Decode([]byte(pubPEM)) | |
if block == nil { | |
return nil, errors.New("failed to parse PEM block containing the key") | |
} | |
pub, err := x509.ParsePKIXPublicKey(block.Bytes) | |
if err != nil { | |
return nil, err | |
} | |
switch pub := pub.(type) { | |
case *rsa.PublicKey: | |
return pub, nil | |
default: | |
break // fall through | |
} | |
return nil, errors.New("Key type is not RSA") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment