b64
Base64 encoding and decoding - standard, URL-safe, MIME line-wrapped, validation, padding manipulation, and convenience shorthands.
Load with: use b64
What this module does
b64 is a pure ilusm Base64 implementation. It handles the three most common
variants: standard RFC 4648 Base64 (alphabet A-Z a-z 0-9 + / with = padding),
URL-safe Base64 (alphabet uses - and _, no padding), and
MIME Base64 (standard encoding broken into lines of configurable length, default 76 characters).
For most uses the convenience shorthands at the bottom are all you need:
bs64n encodes a string, bs64d decodes back to a string,
bs6ha encodes URL-safe, bs64r decodes URL-safe.
Quick example
use b64
# Encode a string
encoded = bs64n("hello world")
prn(encoded) # "aGVsbG8gd29ybGQ="
# Decode it back
prn(bs64d(encoded)) # "hello world"
# URL-safe (no padding, uses - and _)
url = bs6ha("hello world")
prn(url) # "aGVsbG8gd29ybGQ"
# Validate
prn(bs64s(encoded)) # tru
prn(bs64s("not!!base64")) # fls
Functions
Standard Base64 encoder object
b64nc()
Creates a standard Base64 encoder with alphabet ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/, padding =, chunk size 3. This is what all the convenience functions use internally.
b64sa1(encoder, data)
Encodes data (string or byte list) with encoder. Processes 3 bytes at a time - 3 bytes → 4 chars, 2 bytes → 3 chars + 1 padding, 1 byte → 2 chars + 2 padding. Returns the encoded string.
b64dc(encoder, encoded)
Decodes a Base64 string back to a byte list. Strips padding first, then converts character groups back to bytes: 4 chars → 3 bytes, 3 chars → 2 bytes, 2 chars → 1 byte. Errors on invalid Base64 characters.
URL-safe Base64
b64ma()
Creates a URL-safe encoder with alphabet using - and _ instead of + and /, and no padding character.
b64da(encoder, data)
Encodes data in URL-safe Base64. Internally uses a standard encoder, then replaces + → -, / → _, and strips all = padding.
b64rl(padding_needed, encoded)
Decodes URL-safe Base64. Restores - → + and _ → /, re-adds the correct number of = padding characters, then decodes with the standard decoder.
MIME Base64 (line-wrapped)
b64ka(line_length)
Creates a MIME encoder. Default line length is 76 characters with \n line breaks. Uses a standard encoder internally.
b64aa(mime_encoder, data)
Encodes data and splits the result into lines of mime_encoder.line_length characters joined by the line break. Returns the wrapped string.
b64mm(mime_encoder, encoded)
Decodes MIME Base64 by stripping all line breaks first, then decoding normally.
Validation and sanitisation
b64ia()
Creates a validator whose valid character set is A-Z a-z 0-9 + / =.
b64vl(validator, encoded)
Returns tru if every character in encoded is in the valid set, fls on the first invalid character.
b64ba(validator, encoded)
Strips any characters not in the valid set and returns the cleaned string.
Padding utilities
b64la()
Creates a utility object with padding_char: "=" and line_separator: "\n".
b64ha(util, encoded)
Adds = padding to make the string a multiple of 4 characters.
b64ea(util, encoded)
Removes trailing = padding characters.
b64tl(util, encoded, chunk_size)
Splits an encoded string into chunks of chunk_size characters. Returns a list.
b64ga(util, chunks)
Joins a list of chunks back together with util.line_separator between them.
Convenience shorthands
These use module-level default encoder/decoder instances - no setup needed.
bs64n(text)
Encode a string with the default standard encoder.
bs64d(encoded)
Decode a standard Base64 string back to a string (converts byte list via txt.txtch).
bs6ca(bytes)
Encode a byte list with the default standard encoder.
bs6aa(encoded)
Decode a standard Base64 string back to a byte list (raw bytes, no string conversion).
bs6ha(data)
Encode with URL-safe Base64 (no padding, -/_).
bs64r(encoded)
Decode URL-safe Base64.
bs6ga(data, line_length)
Encode with MIME line-wrapping at line_length characters.
bs6fa(encoded)
Decode MIME Base64 (strips line breaks first).
bs64s(encoded)
Validate a Base64 string - returns tru/fls.
bs64c(encoded)
Sanitise a Base64 string by stripping invalid characters.
Notes
- Pure ilusm - no native bindings for encoding/decoding.
bs64dconverts the decoded byte list to a string usingtxt.txtch. If you need raw bytes (e.g. for binary data), usebs6aainstead.- The validator's valid set includes
=, so padded standard Base64 passes. URL-safe Base64 (which uses-and_) will fail the default validator - strip padding and swap characters first if needed.