March Docs

Base64

Base64 module: standard, URL-safe, and MIME Base64 encoding/decoding.

This module wraps the runtime's base64_encode/base64_decode builtins and adds URL-safe (RFC 4648 §5) and MIME (RFC 2045) variants.

Standard Base64 uses the alphabet A–Z a–z 0–9 + / with = padding. URL-safe Base64 replaces + with - and / with _ and omits padding. MIME Base64 wraps lines at 76 characters with CRLF line endings.

All decode functions return Ok(Bytes) on success, Err(:invalid) on bad input.

Functions

fnencodeencode(data)#

Encode a Bytes value as standard Base64 (RFC 4648 §4, with = padding).

let b = Bytes.from_string("Hello") Base64.encode(b) -- "SGVsbG8="

fndecodedecode(s)#

Decode a standard Base64 string to Bytes.

Returns Ok(Bytes) on success or Err(:invalid) if the input is not valid Base64 (wrong length, bad characters, or bad padding).

Base64.decode("SGVsbG8=") -- Ok(Bytes for "Hello") Base64.decode("!!") -- Err(:invalid)

fnurl_encodeurl_encode(data)#

Encode a Bytes value as URL-safe Base64 (RFC 4648 §5).

Uses - and _ instead of + and /, and omits trailing = padding. The result is safe to embed in URLs and filenames without escaping.

let b = Bytes.from_string("Hello") Base64.url_encode(b) -- "SGVsbG8"

fnurl_decodeurl_decode(s)#

Decode a URL-safe Base64 string (- and _, no padding) to Bytes.

Padding is added automatically before decoding.

Base64.url_decode("SGVsbG8") -- Ok(Bytes for "Hello") Base64.url_decode("!!") -- Err(:invalid)

fnmime_encodemime_encode(data)#

Encode a Bytes value as MIME Base64 (RFC 2045).

Lines are at most 76 characters long, separated by CRLF (\\r\\n). Suitable for encoding binary attachments in email or multipart bodies.

Base64.mime_encode(Bytes.from_string("Hello World")) -- "SGVsbG8gV29ybGQ=" (fits in one line for short inputs)