Skip to content

base64

stable

Encode and decode data using standard and URL-safe Base64 alphabets, with byte-level and string-level variants.

use plugin base64::{encode, decode, encode_bytes, …}
8 functions Data Formats
/ filter jk navigate Esc clear
Functions (8)
  1. encode Encode a string to standard Base64
  2. decode Decode a standard Base64 string
  3. encode_bytes Encode raw bytes to standard Base64
  4. decode_bytes Decode Base64 to raw bytes
  5. encode_url_safe Encode a string to URL-safe Base64
  6. decode_url_safe Decode a URL-safe Base64 string
  7. is_valid Check if a string is valid standard Base64
  8. encoded_length Compute the Base64 output length

Encode a string to standard Base64

Encodes a UTF-8 string using the standard Base64 alphabet (A-Z, a-z, 0-9, +, /) with = padding. Use this for embedding text in JSON, headers, or data URIs.

use plugin base64::{encode, decode}

let encoded = encode("Hello, Zolo!")
print(encoded)

let original = decode(encoded)
print(original)

Decode a standard Base64 string

Decodes a standard Base64 string and returns the result as a UTF-8 string. Errors if the input contains invalid characters or the decoded bytes are not valid UTF-8.

use plugin base64::{decode}

let text = decode("SGVsbG8sIFpvbG8h")
print(text)

Encode raw bytes to standard Base64

Encodes a raw bytes value (e.g. from a file read or crypto function) to a standard Base64 string. Use this instead of encode when the input is binary data rather than text.

use plugin base64::{encode_bytes, decode_bytes}

let raw = fs.read_bytes("image.png")
let b64 = encode_bytes(raw)
print("encoded length: {b64}")

Decode Base64 to raw bytes

Decodes a standard Base64 string and returns raw bytes without attempting UTF-8 conversion. Use this for binary payloads such as images, keys, or encrypted data.

use plugin base64::{encode_bytes, decode_bytes}

let original = fs.read_bytes("data.bin")
let encoded = encode_bytes(original)
let recovered = decode_bytes(encoded)

Encode a string to URL-safe Base64

Encodes a string using the URL-safe Base64 alphabet (- instead of +, _ instead of /). Safe to embed in URLs and filenames without percent-encoding.

use plugin base64::{encode_url_safe, decode_url_safe}

let token = encode_url_safe("user:secret-data/path")
print(token)

let back = decode_url_safe(token)
print(back)

Decode a URL-safe Base64 string

Decodes a URL-safe Base64 string and returns the UTF-8 result. Accepts - and _ as the 62nd and 63rd alphabet characters.

use plugin base64::{decode_url_safe}

let payload = decode_url_safe("dXNlcjpzZWNyZXQ=")
print(payload)

Check if a string is valid standard Base64

Returns true if input is a well-formed standard Base64 string (correct alphabet, valid padding, length divisible by 4). Whitespace is ignored.

use plugin base64::{is_valid, encode}

let good = encode("test")
print(is_valid(good))
print(is_valid("not!!base64"))
print(is_valid(""))

Compute the Base64 output length

Returns the number of Base64 characters that would be produced for input_length raw bytes (always a multiple of 4 due to padding). Useful for pre-allocating buffers.

use plugin base64::{encoded_length}

print(encoded_length(0))
print(encoded_length(3))
print(encoded_length(10))
print(encoded_length(1000))
enespt-br