```{index} single: MIME::Base64; Perl module ``` # MIME::Base64 ```{pperl-module-badges} 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_base64` form); - 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_length` and `decoded_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. ## Functions ### Standard encoding #### [`encode_base64`](Base64/encode_base64) Encode a byte string as standard RFC 2045 base64, optionally wrapping at 76 characters. #### [`decode_base64`](Base64/decode_base64) Decode a base64 string back to the original byte sequence, tolerating stray whitespace and line breaks. ### URL-safe variant #### [`encode_base64url`](Base64/encode_base64url) Encode bytes using the URL- and filename-safe base64 alphabet, with no padding and no line breaks. #### [`decode_base64url`](Base64/decode_base64url) Decode a URL-safe base64 string, accepting input with or without trailing padding. ### Length helpers #### [`encoded_base64_length`](Base64/encoded_base64_length) Return the length `encode_base64` would produce, without doing the encoding. #### [`decoded_base64_length`](Base64/decoded_base64_length) Return the length `decode_base64` would produce, without doing the decoding. ```{toctree} :hidden: :maxdepth: 1 Base64/encode_base64 Base64/decode_base64 Base64/encode_base64url Base64/decode_base64url Base64/encoded_base64_length Base64/decoded_base64_length ```