encodePublicKeyToPKIX function

Uint8List encodePublicKeyToPKIX(
  1. RSAPublicKey publicKey
)

Implementation

Uint8List encodePublicKeyToPKIX(RSAPublicKey publicKey) {
  var topLevel = ASN1Sequence();

  var seqType = ASN1Sequence();
  try {
    var id = ASN1ObjectIdentifier.fromBytes(Uint8List.fromList(
        [0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01]));
    seqType.add(id);
  } catch (ee) {}
  seqType.add(ASN1Null());
  topLevel.add(seqType);

  var seqPubKey = ASN1Sequence();
  seqPubKey.add(ASN1Integer(publicKey.modulus));
  seqPubKey.add(ASN1Integer(publicKey.exponent));
  var pubKeySeqBS = seqPubKey.encode();
  var seqPubKeyBS = ASN1BitString(stringValues: pubKeySeqBS);
  topLevel.add(seqPubKeyBS);

  var bytes = topLevel.encode();
  return bytes;
}