rsaPublicKeyDerFromJwk function

Uint8List rsaPublicKeyDerFromJwk(
  1. Map<String, Object?> jwk
)

Builds a SubjectPublicKeyInfo DER from the public half of an RSA jwk.

Works for a private JWK too — the extra parameters are simply ignored, so the same key file can produce both halves.

Implementation

Uint8List rsaPublicKeyDerFromJwk(Map<String, Object?> jwk) {
  _requireRsa(jwk);

  final pkcs1 = _derSequence([
    _derInteger(_requireField(jwk, 'n')),
    _derInteger(_requireField(jwk, 'e')),
  ]);

  // BIT STRING with zero unused bits, wrapping the PKCS#1 key.
  final bitString = Uint8List(pkcs1.length + 1)..setRange(1, pkcs1.length + 1, pkcs1);
  return _derSequence([
    Uint8List.fromList(_rsaAlgorithmIdentifier),
    DerValue(0x03, bitString).bytes,
  ]);
}