decode_base64#

Decode a base64 string back to the original byte sequence, tolerating stray whitespace and line breaks.

Synopsis#

use MIME::Base64;
my $bytes = decode_base64($encoded);

What you get back#

A byte string carrying the decoded octets. The result is binary; treat it as bytes, not text. If the input was the output of encode_base64, the result is byte-for-byte what was encoded.

The decoder silently skips any character outside the 65-character base64 subset, so wrapped lines, CRLFs, leading/trailing whitespace, and MIME header remnants are handled without special preprocessing. Decoding stops at the first = padding group; data appearing after padding is ignored.

Examples#

decode_base64("QWxhZGRpbjpvcGVuIHNlc2FtZQ==");

## "Aladdin:open sesame"

decode_base64("QWxh\nZGRp\nbjpv\ncGVu\nIHNl\nc2Ft\nZQ==\n");

## "Aladdin:open sesame"   (wrapped input decodes identically)

decode_base64("AAAA");

## "\x00\x00\x00"

Edge cases#

  • Empty input returns the empty string.

  • Input of all-whitespace returns the empty string.

  • A truncated final group (1 valid character) produces no output for that group; 2 valid characters produce 1 byte, 3 produce 2 bytes.

  • Characters after the first = padding group are discarded.

  • Non-alphabet characters anywhere in the input are silently skipped, including control characters and punctuation.

Differences from upstream#

Fully compatible with upstream MIME::Base64 3.16.

See also#

  • encode_base64 — the inverse operation.

  • decode_base64url — the URL-safe variant, accepts -/_ and missing padding.

  • decoded_base64_length — size the output without decoding.