decode_base64url#

Decode a URL-safe base64 string, accepting input with or without trailing padding.

Synopsis#

use MIME::Base64 qw(decode_base64url);
my $bytes = decode_base64url($token);

What you get back#

The decoded byte string. - and _ in the input are mapped back to + and / before decoding, and any missing trailing padding is reconstructed. The decoder inherits decode_base64’s tolerance: characters outside the alphabet are silently skipped, and decoding stops at the first padding group.

Examples#

decode_base64url("QWxhZGRpbjpvcGVuIHNlc2FtZQ");

## "Aladdin:open sesame"

decode_base64url("-_8");

## "\xFB\xFF"

decode_base64url("QWxhZGRpbjpvcGVuIHNlc2FtZQ==");   # padding also OK

## "Aladdin:open sesame"

Edge cases#

  • Empty input returns the empty string.

  • Input with stray + or / is accepted (those are valid under standard base64); the URL-safe decoder is a superset of the standard decoder for input characters.

  • Truncated input (length not a multiple of four, no padding) is padded internally and decoded; the last partial group follows the same “1 char → 0 bytes, 2 → 1, 3 → 2” rule as decode_base64.

Differences from upstream#

Fully compatible with upstream MIME::Base64 3.16.

See also#

  • encode_base64url — the inverse operation.

  • decode_base64 — standard variant.