parsePublicKeyFromPem static method
Parses an RSA public key from a PEM-encoded string.
pem: The PEM-encoded public key string.
Returns an RSAPublicKey object.
Implementation
static RSAPublicKey parsePublicKeyFromPem(String pem) {
final keyBytes = _decodePem(pem, 'PUBLIC KEY');
final asn1Parser = ASN1Parser(keyBytes);
final topLevelSeq = asn1Parser.nextObject() as ASN1Sequence;
final subjectPublicKey = topLevelSeq.elements![1] as ASN1BitString;
var rsaKeyBytes = subjectPublicKey.valueBytes!;
// Remove leading zero byte if present
if (rsaKeyBytes.isNotEmpty && rsaKeyBytes[0] == 0) {
rsaKeyBytes = rsaKeyBytes.sublist(1);
}
final rsaKeyParser = ASN1Parser(rsaKeyBytes);
final rsaKeySeq = rsaKeyParser.nextObject() as ASN1Sequence;
final modulus = rsaKeySeq.elements![0] as ASN1Integer;
final publicExponent = rsaKeySeq.elements![1] as ASN1Integer;
return RSAPublicKey(
_bytesToBigInt(modulus.valueBytes!),
_bytesToBigInt(publicExponent.valueBytes!),
);
}