v1.0.0 module file added to repo.

This commit is contained in:
2026-02-15 22:23:26 +00:00
parent 0d930ca247
commit 823cc324c4
+26
View File
@@ -0,0 +1,26 @@
"""URL-safe Base64 encoding without padding.
This format uses only characters that are safe for URLs, filenames etc.
Refer to Python's urlsafe_b64encode and base64.urlsafe_b64decode for more details.
Fixes Python base64 module's faults:
- Padding "==" is optional and not produced by b64url.enc
- Base64 type is str (stdlib is retarded and uses bytes)
- Otherwise identical to urlsafe_b64encode/urlsafe_b64decode
"""
from base64 import urlsafe_b64decode as _decode
from base64 import urlsafe_b64encode as _encode
__all__ = ["enc", "dec"]
def enc(data: bytes) -> str:
"""Base64 encode bytes to a URL-safe string without padding."""
return _encode(data).decode("ascii").rstrip("=")
def dec(s: str) -> bytes:
"""Decode URL-safe Base64 into bytes. Padding optional."""
# Testing whether needs padding is slower, this is the fastest way and constant time.
return _decode(s + "=" * (4 - (len(s) % 4)))