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.
- Urlsafe: Uses only characters that are safe for URLs and filenames
- No padding: Removes trailing
=characters for a cleaner, actually urlsafe output - String output: Returns Base64 as str instead of bytes
- Fast: Based on Python stdlib, with constant-time padding restoration
pip install base64urlOr for your project using UV:
uv add base64urlimport base64url
text = base64url.enc(bytes(4)) # Returns "AAAAAA"
data = base64url.dec(text) # Recovers the bytesEncode bytes to a urlsafe string without padding.
Decode standard or urlsafe Base64 into bytes. Padding optional. Raises binascii.Error (ValueError) on invalid input. Set validate=False to silently ignore unknown characters (Python base64 default behavior).
Encode with newlines inserted every length characters. Does not add a newline at the end.
Decode formatted input, ignoring whitespace. Raises binascii.Error on invalid characters.