MIME::Base64#
Encode and decode base64 strings - the standard MIME flavour plus the URL-safe variant and cheap length helpers.
MIME::Base64 turns arbitrary byte strings into the 65-character ASCII subset defined by RFC 2045 ([A-Za-z0-9+/=]) and back. Most callers reach for it for one of a handful of jobs:
writing MIME-style bodies where encoded output must be wrapped at 76 characters per line (the default
encode_base64form);producing a single unwrapped token for an HTTP header, a credential, or a config file -
encode_base64($bytes, "");building URL-safe tokens (JWT payloads, signed cookies, opaque identifiers) where
+and/would need percent-escaping -encode_base64url;sizing a buffer without paying the cost of the full encode or decode -
encoded_base64_lengthanddecoded_base64_length.
Line-length wrapping#
encode_base64 breaks the output into lines of at most 76 characters, each terminated by the caller’s end-of-line sequence (default "\n"). Pass "" as the second argument to suppress wrapping entirely; pass "\r\n" for CRLF output suitable for raw MIME transport. The URL-safe encoder never wraps and never pads.
Decode leniency#
decode_base64 is deliberately forgiving. Any character outside the 65-character alphabet is silently skipped, so embedded whitespace, line breaks, and MIME boilerplate pass through harmlessly. Decoding stops at the first = padding group - data after padding is never consumed. The URL-safe decoder accepts input with or without padding and translates -/_ back to +// before running the same state machine.
Who calls this#
Most real-world consumers are Authen::* (HTTP Basic auth), Email::MIME and friends (message bodies), JWT/JOSE libraries (via the URL-safe variant), and anything that needs to round-trip binary through a text channel. encode_base64 and decode_base64 are exported by default; the URL-safe and length helpers are exported on request.
URL-safe variant#
encode_base64url($bytes) encodes with the URL- and filename-safe alphabet (- and _ in place of + and /), strips the trailing = padding and never wraps, so the result drops into URLs, cookies and JWT segments unescaped. decode_base64url($token) maps - and _ back, restores the padding and decodes; input with or without padding, or with + and /, is accepted.
This is MIME::Base64 from MIME-Base64 3.16_01; MIME/Base64.pm is upstream’s. encode_base64 and decode_base64 are exported by default.
Synopsis#
use MIME::Base64 qw(encode_base64 decode_base64 encode_base64url decode_base64url);
my $enc = encode_base64("Aladdin:open sesame");
print $enc; # QWxhZGRpbjpvcGVuIHNlc2FtZQ==\n
print decode_base64($enc), "\n"; # Aladdin:open sesame
print encode_base64("\xff\xfe binary", ''), "\n"; # //4gYmluYXJ5 - one line
my $token = encode_base64url("\xfb\xff"); # -_8: no + / or = padding
print length decode_base64url($token), "\n"; # 2
Strings must be bytes: a character above 0xFF dies “Wide character”; encode text first (encode_base64(Encode::encode_utf8($text))).
Differences from upstream#
One, where perl 5.44 with Base64.xs fails (t/81-xs-native/MIME-Base64/130-or-better.t): encoded_base64_length and decoded_base64_length given a number or undef measure its string value, as for any other value. perl5 reads a length that number never had - encoded_base64_length(12345) gave 485286422 - or crashes on undef.
Functions#
Standard encoding#
encode_base64#
Encode a byte string as standard RFC 2045 base64, optionally wrapping at 76 characters.
decode_base64#
Decode a base64 string back to the original byte sequence, tolerating stray whitespace and line breaks.
Length helpers#
encoded_base64_length#
Return the length encode_base64 would produce, without doing the encoding.
decoded_base64_length#
Return the length decode_base64 would produce, without doing the decoding.
Other Functions#
encode_base64url#
Encode bytes using the URL- and filename-safe base64 alphabet, with no padding and no line breaks.
decode_base64url#
Decode a URL-safe base64 string, accepting input with or without trailing padding.