decodeBase64Url function

Uint8List decodeBase64Url(
  1. String value
)

Conversion between JSON Web Keys (RFC 7517) and the DER encodings OpenSSL reads, in pure Dart.

A JWK carries an RSA key as base64url big-endian integers; OpenSSL wants PKCS#1 or SubjectPublicKeyInfo. Translating between the two is pure structure work — no FFI and no crypto — which is why it lives here rather than in a mixin, and why it is cheap enough to do at startup.

Only RSA keys are handled. An OpenID Connect identity provider signing with RS256 — the common case, and what this exists for — is RSA; EC and OKP JWKs throw rather than silently producing a key that cannot verify anything. Decodes base64url, the encoding every JWK integer uses.

Padding is restored before decoding, so input both with and without = works — RFC 7515 §2 requires it omitted, but not every implementation obeys (package:jose emits it), and rejecting those keys would help nobody.

Implementation

/// Decodes base64url, the encoding every JWK integer uses.
///
/// Padding is restored before decoding, so input both with and without `=`
/// works — RFC 7515 §2 requires it omitted, but not every implementation obeys
/// (`package:jose` emits it), and rejecting those keys would help nobody.
Uint8List decodeBase64Url(String value) {
  final normalized = value.replaceAll('-', '+').replaceAll('_', '/');
  final padded = normalized.padRight((normalized.length + 3) & ~3, '=');
  return Uint8List.fromList(base64.decode(padded));
}