Skip to content

Instantly share code, notes, and snippets.

@naturalett
Created February 23, 2020 23:05
Show Gist options
  • Save naturalett/516def63329f226d4567d1b8d777ec4b to your computer and use it in GitHub Desktop.
Save naturalett/516def63329f226d4567d1b8d777ec4b to your computer and use it in GitHub Desktop.
getRSAPrivateKey
def getRSAPrivateKey(privateKey) {
try {
String privateKeyPEM = readFile privateKey
privateKeyPEM = privateKeyPEM.replace("-----BEGIN CERTIFICATE-----\n", "");
privateKeyPEM = privateKeyPEM.replace("-----END CERTIFICATE-----", "");
byte[] encoded = Base64.decodeBase64(privateKeyPEM);
KeyFactory kf = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
RSAPrivateKey privKey = (RSAPrivateKey) kf.generatePrivate(keySpec);
return privKey
} catch(Exception e){
echo "Exception: ${e}"
error "Failed to create a RSAPrivateKey"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment