rsaPrivateKeyToFormattedPEM function

String rsaPrivateKeyToFormattedPEM(
  1. RSAPrivateKey privateKey
)

Export a RSAPrivateKey as formatted PEM String which can be reversed using RSAPrivateKey.fromPEM.

Implementation

String rsaPrivateKeyToFormattedPEM(RSAPrivateKey privateKey) {
  final base = rsaPrivateKeyToString(privateKey);
  var formatted = '';
  for (var i = 0; i < base.length; i++) {
    if (i % 64 == 0 && i != 0) {
      formatted += '\n';
    }
    formatted += base[i];
  }
  return '-----BEGIN RSA PRIVATE KEY-----\n$formatted\n-----END RSA PRIVATE KEY-----';
}