URL-safe Base64 for Python

A simple module for encoding without padding, fixing Python standard library's flaws.

Replaces the standard library's base64.urlsafe_b64encode and base64.urlsafe_b64decode with a cleaner implementation that returns strings instead of bytes and avoids unnecessary padding.

Features

  • Urlsafe: Uses only characters that are safe for URLs and filenames
  • No padding: Removes trailing = characters for cleaner output
  • String output: Returns proper strings instead of bytes (unlike Python's standard library)
  • Fast: Based on Python stdlib, with constant-time padding restoration

Installation

pip install base64url

Or for your project using uv:

uv add base64url

Usage

import base64url

text = base64url.enc(bytes(4)) # Returns "AAAAAA"
data = base64url.dec(text)     # Recovers the bytes

enc(data: bytes) -> str

Base64 encode bytes to a URL-safe string without padding.

dec(s: str, *, validate=True) -> bytes

Decode URL-safe Base64 into bytes. Padding optional. Set validate=False to silently ignore any unknown characters (Python base64 default behavior).

enc_lines(data: bytes, length: int = 76) -> str

Encode with newlines inserted every length characters.

dec_lines(s: str) -> bytes

Decode formatted input, ignoring all whitespace (but not other unknown characters).

S
Description
Base64 encoding without Python's base64 flaws. No padding, str types.
Readme
47 KiB
Languages
Python 100%