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

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).

## Examples

    march> Base64.decode("SGVsbG8=")
    Ok(Bytes([72, 101, 108, 108, 111]))
    march> Base64.decode("!!")
    Err(:invalid)
fnencodeencode(data)#

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

Examples

    march> Base64.encode(Bytes.from_string("Hello"))
    "SGVsbG8="
    march> Base64.encode(Bytes.from_string(""))
    ""
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.

## Examples

    march> Base64.mime_encode(Bytes.from_string("Hello World"))
    "SGVsbG8gV29ybGQ="
fnurl_decodeurl_decode(s)#

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

Padding is added automatically before decoding so callers don't have
to track which variant they have on hand.

## Examples

    march> Base64.url_decode("SGVsbG8")
    Ok(Bytes([72, 101, 108, 108, 111]))
    march> Base64.url_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.

## Examples

    march> Base64.url_encode(Bytes.from_string("Hello"))
    "SGVsbG8"