Base64 encoding and decoding functions.

Functions

decode

fn (s: Str): Bytes

Decode s base64 string to a byte array.

Example

encoded "SGVsbG8="
bytes ::hot::base64/decode(encoded)
// bytes = Bytes([72, 101, 108, 108, 111])

decode-url

fn (s: Str): Bytes

Decode URL-safe base64 string (with or without padding) to bytes.

Example

::hot::base64/decode-url("SGVsbG8")
// Bytes([72, 101, 108, 108, 111])

encode

fn (data: Str | Bytes): Str

Encode data to a base64 string. A Str is encoded as its UTF-8 bytes.

Example

::hot::base64/encode("Hello")  // "SGVsbG8="
::hot::base64/encode(Bytes([72, 101, 108, 108, 111]))  // "SGVsbG8=" ("Hello" as bytes)

encode-url

fn (data: Str | Bytes): Str

Encode data to URL-safe base64 string (no padding). Uses - and _ instead of + and /. Safe for URLs and filenames. A Str is encoded as its UTF-8 bytes.

Example

::hot::base64/encode-url("Hello+World/Test")
// "SGVsbG8rV29ybGQvVGVzdA"  (no = padding, - and _ chars)

is-valid

fn (s: Str): Bool

Check if string s is valid base64.

Example

::hot::base64/is-valid("SGVsbG8=")   // true
::hot::base64/is-valid("not valid!") // false