encodePublicKeyToPemPKCS1 method

String encodePublicKeyToPemPKCS1(
  1. RSAPublicKey publicKey
)

Encode Public key to PEM Format

Given RSAPublicKey returns a base64 encoded String with standard PEM headers and footers

Implementation

String encodePublicKeyToPemPKCS1(RSAPublicKey publicKey) {
  var topLevel = new ASN1Sequence();

  topLevel.add(ASN1Integer(publicKey.modulus!));
  topLevel.add(ASN1Integer(publicKey.exponent!));

  var dataBase64 = base64.encode(topLevel.encodedBytes);
  return """-----BEGIN RSA PUBLIC KEY-----\r\n$dataBase64\r\n-----END RSA PUBLIC KEY-----""";
}