Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

base64

The base64 module turns bytes into Base64 text and back: Basic authentication headers, data URLs, binary data inside JSON, the parts of a JWT.

Common tasks

import "base64"
import "fs"
import "json"

# Basic authentication
var credentials = base64.encode("{user}:{password}")
var headers = {"Authorization": "Basic {credentials}"}

# Binary data inside JSON
var payload = json.encode({"name": "logo.png", "data": base64.encode(fs.read_bytes("logo.png"))})

# And back
var image = base64.decode(json.decode(payload)["data"])
fs.write("logo-copy.png", image)

# The middle part of a JWT: URL alphabet, no padding
var claims = json.decode(base64.decode(token.split(".")[1], url_safe=true).decode())

examples/base64.goblin runs each of these tasks.

Module members

MemberReturnsBehaviour
encode(data, url_safe=false, padding=true)StringBase64 text of data (a String's UTF-8 bytes, Bytes, or a readable read to its end). url_safe=true uses - and _ instead of + and /. padding=false leaves off the trailing =
decode(data, url_safe=false)BytesThe bytes data encodes: Base64 text as a String, Bytes, or a readable read to its end. Trailing = padding may be present or absent, but when present it must be complete. Line breaks inside data are ignored. "" decodes to empty Bytes

decode returns Bytes because Base64 carries binary data; call .decode() on the result when it is known to be text.

Errors

FailureRaises
data is not valid Base64 in the chosen alphabetParseError decode() invalid Base64 at position 5, the position being the character index in data; the error carries it as e.position, an Int
data has the wrong typeTypeError
Reading a readable data failsthe error the readable raised, such as IOError

Left out

Base32, Ascii85 and quoted-printable encodings.