encodeRSAPublicKeyToPemPkcs1 static method

String encodeRSAPublicKeyToPemPkcs1(
  1. RSAPublicKey rsaPublicKey
)

Enode the given rsaPublicKey to PEM format using the PKCS#1 standard.

The ASN1 structure is decripted at tools.ietf.org/html/rfc8017#page-53.

RSAPublicKey ::= SEQUENCE {
  modulus           INTEGER,  -- n
  publicExponent    INTEGER   -- e
}

Implementation

static String encodeRSAPublicKeyToPemPkcs1(RSAPublicKey rsaPublicKey) {
  var topLevelSeq = ASN1Sequence();
  topLevelSeq.add(ASN1Integer(rsaPublicKey.modulus));
  topLevelSeq.add(ASN1Integer(rsaPublicKey.exponent));

  var dataBase64 = base64.encode(topLevelSeq.encode());
  var chunks = _chunk(dataBase64, 64);

  return '$BEGIN_RSA_PUBLIC_KEY\n${chunks.join('\n')}\n$END_RSA_PUBLIC_KEY';
}