parsePrivateKeyFromPem static method

RSAPrivateKey parsePrivateKeyFromPem(
  1. String pem
)

Parses an RSA private key from a PEM-encoded string.

  • pem: The PEM-encoded private key string.

Returns an RSAPrivateKey object.

Implementation

static RSAPrivateKey parsePrivateKeyFromPem(String pem) {
  final keyBytes = _decodePem(pem, 'RSA PRIVATE KEY');
  final asn1Parser = ASN1Parser(keyBytes);
  final topLevelSeq = asn1Parser.nextObject() as ASN1Sequence;

  final modulus = topLevelSeq.elements![1] as ASN1Integer;
  final privateExponent = topLevelSeq.elements![3] as ASN1Integer;
  final prime1 = topLevelSeq.elements![4] as ASN1Integer;
  final prime2 = topLevelSeq.elements![5] as ASN1Integer;

  return RSAPrivateKey(
    _bytesToBigInt(modulus.valueBytes!),
    _bytesToBigInt(privateExponent.valueBytes!),
    _bytesToBigInt(prime1.valueBytes!),
    _bytesToBigInt(prime2.valueBytes!),
  );
}