parsePublicKeyFromPem method
RSAPublicKey
parsePublicKeyFromPem(
- dynamic pemString
Decode Public key from PEM Format
Given a base64 encoded PEM String with correct headers and footers, return a
RSAPublicKey
PKCS1 RSAPublicKey ::= SEQUENCE { modulus INTEGER, -- n publicExponent INTEGER -- e }
PKCS8 PublicKeyInfo ::= SEQUENCE { algorithm AlgorithmIdentifier, PublicKey BIT STRING }
AlgorithmIdentifier ::= SEQUENCE { algorithm OBJECT IDENTIFIER, parameters ANY DEFINED BY algorithm OPTIONAL }
Implementation
RSAPublicKey parsePublicKeyFromPem(pemString) {
List<int> publicKeyDER = decodePEM(pemString);
ASN1Parser asn1Parser = new ASN1Parser(publicKeyDER as Uint8List);
ASN1Sequence topLevelSeq = asn1Parser.nextObject() as ASN1Sequence;
var modulus, exponent;
// Depending on the first element type, we either have PKCS1 or 2
if (topLevelSeq.elements[0].runtimeType == ASN1Integer) {
modulus = topLevelSeq.elements[0] as ASN1Integer;
exponent = topLevelSeq.elements[1] as ASN1Integer;
} else {
var publicKeyBitString = topLevelSeq.elements[1];
var publicKeyAsn = new ASN1Parser(publicKeyBitString.contentBytes()!);
ASN1Sequence publicKeySeq = publicKeyAsn.nextObject() as ASN1Sequence;
modulus = publicKeySeq.elements[0] as ASN1Integer;
exponent = publicKeySeq.elements[1] as ASN1Integer;
}
RSAPublicKey rsaPublicKey =
RSAPublicKey(modulus.valueAsBigInteger, exponent.valueAsBigInteger);
return rsaPublicKey;
}