On this page
base64
Prev: atexit | Up: stdlib_module | Next: bdb
Rel: email, json, mailbox, mimetypes, binascii
Ref: docs.python.org — base64
Tags: #public #python3 #stdlib
Import
import base64
Overview
Binary-to-text encodings (Base64, URL-safe Base64, Base32, Base16/hex, Base85, and Ascii85) for embedding raw bytes inside JSON, URLs, HTTP headers, or text protocols. Use urlsafe_b64encode() for URLs and filenames; remember that Base64 is an encoding, not encryption.
Quick Start
import base64
raw = b"hello world"
encoded = base64.b64encode(raw).decode("ascii")
assert encoded == "aGVsbG8gd29ybGQ="
assert base64.b64decode(encoded) == raw
Implementation Walkthrough
1. URL-Safe Base64 (No + or / Characters, Optional Padding Stripping)
JWTs, query parameters, and filenames require urlsafe_b64encode (substituting - and _):
import base64
def encode_url_token(data: bytes) -> str:
return base64.urlsafe_b64encode(data).rstrip(b"=").decode("ascii")
def decode_url_token(token: str) -> bytes:
padding = "=" * (-len(token) % 4)
return base64.urlsafe_b64decode(token + padding)
tok = encode_url_token(b"\xff\xfe\xfd")
assert decode_url_token(tok) == b"\xff\xfe\xfd"
2. Strict Validation on Untrusted Input (validate=True)
import base64, binascii
try:
base64.b64decode("not@valid!base64", validate=True)
except binascii.Error:
pass # rejected invalid non-alphabet characters
Gotchas & Boundaries
- Functions accept and return
bytes, notstr: Encode text to UTF-8 bytes first (s.encode("utf-8")) and.decode("ascii")after encoding if you need astr. - Pass
validate=Truetob64decode()on untrusted input: Without it, non-Base64 characters are silently discarded. - Base64 expands size by ~33% (4 ASCII chars per 3 bytes); avoid it for large binary files when raw binary transport is available.
- Base64 provides zero confidentiality: Anyone can decode it instantly.
Common Functions
- base64.a85decode() —
a85decode(b, *, foldspaces=False, adobe=False, ignorechars=b' \t\n\r\x0b')— Decode the Ascii85 encoded bytes-like object or ASCII string b. - base64.a85encode() —
a85encode(b, *, foldspaces=False, wrapcol=0, pad=False, adobe=False)— Encode bytes-like object b using Ascii85 and return a bytes object. - base64.b16decode() —
b16decode(s, casefold=False)— Decode the Base16 encoded bytes-like object or ASCII string s. - base64.b16encode() —
b16encode(s)— Encode the bytes-like object s using Base16 and return a bytes object. - base64.b32decode() —
b32decode(s, casefold=False, map01=None)— Decode the base32 encoded bytes-like object or ASCII string s. - base64.b32encode() —
b32encode(s)— Encode the bytes-like objects using base32 and return a bytes object. - base64.b32hexdecode() —
b32hexdecode(s, casefold=False)— Decode the base32hex encoded bytes-like object or ASCII string s. - base64.b32hexencode() —
b32hexencode(s)— Encode the bytes-like objects using base32hex and return a bytes object. - base64.b64decode() —
b64decode(s, altchars=None, validate=False)— Decode the Base64 encoded bytes-like object or ASCII string s. - base64.b64encode() —
b64encode(s, altchars=None)— Encode the bytes-like object s using Base64 and return a bytes object. - base64.b85decode() —
b85decode(b)— Decode the base85-encoded bytes-like object or ASCII string b The result is returned as a bytes object. - base64.b85encode() —
b85encode(b, pad=False)— Encode bytes-like object b in base85 format and return a bytes object. - base64.decode() —
decode(input, output)— Decode a file; input and output are binary files. - base64.decodebytes() —
decodebytes(s)— Decode a bytestring of base-64 data into a bytes object. - base64.encode() —
encode(input, output)— Encode a file; input and output are binary files. - base64.encodebytes() —
encodebytes(s)— Encode a bytestring into a bytes object containing multiple lines of base-64 data. - base64.standard_b64decode() —
standard_b64decode(s)— Decode bytes encoded with the standard Base64 alphabet. - base64.standard_b64encode() —
standard_b64encode(s)— Encode bytes-like object s using the standard Base64 alphabet. - base64.urlsafe_b64decode() —
urlsafe_b64decode(s)— Decode bytes using the URL- and filesystem-safe Base64 alphabet. - base64.urlsafe_b64encode() —
urlsafe_b64encode(s)— Encode bytes using the URL- and filesystem-safe Base64 alphabet. - base64.z85decode() —
z85decode(s)— Decode the z85-encoded bytes-like object or ASCII string b The result is returned as a bytes object. - base64.z85encode() —
z85encode(s)— Encode bytes-like object b in z85 format and return a bytes object.
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/atexit
- tuts/python3/stdlib_module/base64/a85decode
- tuts/python3/stdlib_module/base64/a85encode
- tuts/python3/stdlib_module/base64/b16decode
- tuts/python3/stdlib_module/base64/b16encode
- tuts/python3/stdlib_module/base64/b32decode
- tuts/python3/stdlib_module/base64/b32encode
- tuts/python3/stdlib_module/base64/b32hexdecode
- tuts/python3/stdlib_module/base64/b32hexencode
- tuts/python3/stdlib_module/base64/b64decode
- tuts/python3/stdlib_module/base64/b64encode
- tuts/python3/stdlib_module/base64/b85decode
- tuts/python3/stdlib_module/base64/b85encode
- tuts/python3/stdlib_module/base64/decode
- tuts/python3/stdlib_module/base64/decodebytes
- tuts/python3/stdlib_module/base64/encode
- tuts/python3/stdlib_module/base64/encodebytes
- tuts/python3/stdlib_module/base64/standard_b64decode
- tuts/python3/stdlib_module/base64/standard_b64encode
- tuts/python3/stdlib_module/base64/urlsafe_b64decode
- tuts/python3/stdlib_module/base64/urlsafe_b64encode
- tuts/python3/stdlib_module/base64/z85decode
- tuts/python3/stdlib_module/base64/z85encode
- tuts/python3/stdlib_module/bdb
- tuts/python3/stdlib_module/binascii
- tuts/python3/stdlib_module/email
- tuts/python3/stdlib_module/ftplib
- tuts/python3/stdlib_module/html
- tuts/python3/stdlib_module/http
- tuts/python3/stdlib_module/imaplib
- tuts/python3/stdlib_module/ipaddress
- tuts/python3/stdlib_module/json
- tuts/python3/stdlib_module/mailbox
- tuts/python3/stdlib_module/mimetypes
- tuts/python3/stdlib_module/nturl2path
- tuts/python3/stdlib_module/poplib
- tuts/python3/stdlib_module/quopri
- tuts/python3/stdlib_module/smtplib
- tuts/python3/stdlib_module/socketserver
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/urllib
- tuts/python3/stdlib_module/uuid
- tuts/python3/stdlib_module/webbrowser
- tuts/python3/stdlib_module/wsgiref
- tuts/python3/stdlib_module/xml
- tuts/python3/stdlib_module/xmlrpc
Related notes
- tuts/python3/stdlib_module/atexit Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/base64/a85decode Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/base64/a85encode Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/base64/b16decode Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/base64/b16encode Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/base64/b32decode Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/base64/b32encode Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/base64/b32hexdecode Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/base64/b32hexencode Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/base64/b64decode Linked here; links to this page; shared tags: #stdlib