Skip to content

Base64

Standard library namespace · Always available, with no import

Base64 writes binary data using only letters, digits, and a few symbols, so it can travel where text can: in an email, a web address, or a JSON file. Base64 converts Bytes to that text and back.

const text = Base64.encode("Hello, world!".to_bytes())
print(text)
print(Base64.decode(text).to_string())
Output
SGVsbG8sIHdvcmxkIQ==
Hello, world!

Base64 is a way of writing data, not of protecting it: anyone can decode it. For a checksum, see Digest.

Converting
Base64.encode(bytes, url_safe:) Bytes written as Base64 text
Base64.decode(text, url_safe:), Base64.decode_maybe(text, url_safe:) Base64 text read back into bytes

Base64.encode(bytes: Bytes, url_safe: Bool = false): String

The bytes written as Base64. Normally that uses the letters, the digits, +, and /, and ends with = signs to fill out the last group. With url_safe: true, it uses - and _ in place of + and /, and leaves off the = signs, so the result can go in a web address.

const data = Bytes.from_list([251, 255, 254])
print(Base64.encode(data))
print(Base64.encode(data, url_safe: true))
print(Base64.encode("H".to_bytes()))
print(Base64.encode("H".to_bytes(), url_safe: true))
Output
+//+
-__-
SA==
SA

Base64.decode(text: String, url_safe: Bool = false): Bytes

The bytes that Base64 text stands for. It accepts the text with or without its = signs, and ignores spaces, tabs, and line breaks, so text wrapped over several lines reads as one.

print(Base64.decode("SGVs bG8=\n").to_string())
print(Base64.decode("SGVsbG8").to_string())
Output
Hello
Hello

Say which alphabet to expect. Text that uses the other one is an error, and the message says which to choose:

try {
Base64.decode("-_-_")
}
catch error: EncodingError {
print(error.message)
}
Output
Base64 text has `-` at index 0, which belongs to URL-safe Base64

Raises an EncodingError for a character that isn’t in the alphabet, a length that can’t be right, or an ending that doesn’t match the data. The message gives the position of the problem.

Base64.decode_maybe(text: String, url_safe: Bool = false): Bytes?

Like decode, but gives nothing instead of raising an error. It suits text a person typed.

print(Base64.decode_maybe("not base64!"))
print(Base64.decode_maybe("SGk=").or("?".to_bytes()).to_string())
Output
nothing
Hi