parse method

RSAAsymmetricKey parse(
  1. String key
)

Parses the PEM key no matter it is public or private, it will figure it out.

Implementation

RSAAsymmetricKey parse(String key) {
  final rows = key.split(RegExp(r'\r\n?|\n'));
  final header = rows.first;

  if (header == '-----BEGIN RSA PUBLIC KEY-----') {
    return _parsePublic(_parseSequence(rows));
  }

  if (header == '-----BEGIN PUBLIC KEY-----') {
    return _parsePublic(_pkcs8PublicSequence(_parseSequence(rows)));
  }

  if (header == '-----BEGIN RSA PRIVATE KEY-----') {
    return _parsePrivate(_parseSequence(rows));
  }

  if (header == '-----BEGIN PRIVATE KEY-----') {
    return _parsePrivate(_pkcs8PrivateSequence(_parseSequence(rows)));
  }

  throw FormatException('Unable to parse key, invalid format.', header);
}