Base64 with the URL-safe alphabet (Effect's encoding/Base64Url):
encode leaves the padding out, encode_padded keeps it, decode
takes either and returns a Result.
Functions#
decode_string#
decode_string(text) -> Result[str, EncodingError]decode, then the bytes as UTF-8 (bad bytes replaced, a leading U+FEFF kept).
| Parameter | Type | Description |
|---|---|---|
text | str |
Returns Result[str, EncodingError]
def decode_string(text: str) -> Result[str, EncodingError]:
"""``decode``, then the bytes as UTF-8 (bad bytes replaced, a leading U+FEFF kept)."""
return decoded_text(decode(text))base64_url_decode#
base64_url_decode(text) -> Result[bytes, EncodingError]CR and LF are ignored; the URL alphabet, unpadded or fully padded. Non-zero trailing bits in the last character are ignored.
| Parameter | Type | Description |
|---|---|---|
text | str |
Returns Result[bytes, EncodingError]
def base64_url_decode(text: str) -> Result[bytes, EncodingError]:
"""CR and LF are ignored; the URL alphabet, unpadded or fully padded.
Non-zero trailing bits in the last character are ignored."""
stripped = _strip_crlf(text)
length = len(stripped)
padded = stripped.endswith("=")
if padded and length % 4:
return _fail(
"Base64Url", stripped, f"Padded length should be a multiple of 4, but is {length}"
)
if not padded and length % 4 == 1:
return _fail(
"Base64Url",
stripped,
f"Unpadded length should not be 1 more than a multiple of 4, but is {length}",
)
if not _BASE64_URL.fullmatch(stripped):
return _fail("Base64Url", stripped, "Invalid input")
data = stripped.rstrip("=")
return Ok(binascii.a2b_base64(data.translate(_TO_STANDARD) + "=" * (-len(data) % 4)))base64_url_encode#
base64_url_encode(value) -> str| Parameter | Type | Description |
|---|---|---|
value | Binary |
Returns str
def base64_url_encode(value: Binary) -> str:
return base64_encode(value).replace("=", "").translate(_TO_URL)base64_url_encode_padded#
base64_url_encode_padded(value) -> str| Parameter | Type | Description |
|---|---|---|
value | Binary |
Returns str
def base64_url_encode_padded(value: Binary) -> str:
return base64_encode(value).translate(_TO_URL)