From cab0c7dff68a05d713362fe625fc46922fdc1615 Mon Sep 17 00:00:00 2001 From: Leo Vasanko Date: Thu, 3 Jul 2025 10:20:21 -0600 Subject: [PATCH] A tiny project to avoid a major head ache. --- .gitignore | 4 ++++ README.md | 39 +++++++++++++++++++++++++++++++++++++++ pyproject.toml | 31 +++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 pyproject.toml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..56a5ba1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +__pycache__/ +dist/ +.* +!.gitignore diff --git a/README.md b/README.md new file mode 100644 index 0000000..44dee91 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# 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 + +- **URL safe**: 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 + +```sh +pip install base64url +``` + +Or for your project using [uv](https://docs.astral.sh/uv/): +```sh +uv add base64url + +## Usage + +```python +import base64url + +text = base64url.enc(bytes(4)) # Returns str "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) -> bytes` + +Decode URL-safe Base64 into bytes. Padding optional. \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..df5ffde --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,31 @@ +[project] +name = "base64url" +version = "1.0.0" +description = "Base64 encoding without Python's base64 flaws. No padding, str types." +readme = "README.md" +requires-python = ">=3.8" +dependencies = [] +authors = [ + {name = "Leo Vasanko"}, +] +keywords = ["base64", "b64url", "urlsafe_b64encode", "urlsafe_b64decode"] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Intended Audience :: Developers", + "License :: OSI Approved :: MIT License", + "License :: Public Domain", + "Programming Language :: Python :: 3", + "Topic :: Software Development :: Libraries :: Python Modules", + "Topic :: Utilities", +] + +[project.urls] +Homepage = "https://git.zi.fi/leo/base64url-python" +Repository = "https://github.com/LeoVasanko/base64url-python" + +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[tool.hatch.build.targets.wheel] +packages = ["base64url.py"]