{"id":4314,"library":"unpaddedbase64","title":"Unpadded Base64","description":"The `unpaddedbase64` library provides functionality to encode and decode Base64 strings that explicitly omit the '=' padding characters. While RFC 4648 specifies that Base64 should be padded to a multiple of 4 bytes, many protocols choose to leave out this padding. The current version is 2.1.0, released in March 2021, and the library has an infrequent release cadence.","status":"active","version":"2.1.0","language":"en","source_language":"en","source_url":"https://github.com/matrix-org/python-unpaddedbase64","tags":["base64","encoding","decoding","unpadded","rfc4648"],"install":[{"cmd":"pip install unpaddedbase64","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"unpaddedbase64","correct":"import unpaddedbase64"}],"quickstart":{"code":"import unpaddedbase64\n\n# Encode a byte string to unpadded Base64\ndata_bytes = b'\\x00\\x01\\x02'\nencoded_string = unpaddedbase64.encode_base64(data_bytes)\nprint(f\"Encoded: {encoded_string}\")\nassert encoded_string == 'AAEC'\n\n# Decode an unpadded Base64 string back to bytes\ndecoded_bytes = unpaddedbase64.decode_base64('AAEC')\nprint(f\"Decoded: {decoded_bytes}\")\nassert decoded_bytes == data_bytes\n\n# Example with a string that would typically have padding\nlong_data = b'This is a test string.'\nencoded_long = unpaddedbase64.encode_base64(long_data)\nprint(f\"Encoded long: {encoded_long}\")\n# assert encoded_long == 'VGhpcyBpcyBhIHRlc3Qgc3RyaW5nLg' (original would be 'VGhpcyBpcyBhIHRlc3Qgc3RyaW5nLg==')\n\ndecoded_long = unpaddedbase64.decode_base64(encoded_long)\nprint(f\"Decoded long: {decoded_long}\")\nassert decoded_long == long_data","lang":"python","description":"This quickstart demonstrates how to encode byte strings into unpadded Base64 and decode them back using `unpaddedbase64.encode_base64` and `unpaddedbase64.decode_base64`."},"warnings":[{"fix":"Always use `unpaddedbase64.decode_base64()` to decode strings encoded by this library or other unpadded sources. If you need to convert to a padded string for `base64.b64decode()`, you can manually add padding: `padded_s = s + '=' * (-len(s) % 4)`.","message":"Python's built-in `base64` module by default expects and sometimes requires '=' padding for decoding. Using `base64.b64decode()` with an unpadded string (which this library produces) will often result in a `binascii.Error: Incorrect padding`. This library is specifically designed to handle unpadded Base64.","severity":"gotcha","affected_versions":"All versions of `unpaddedbase64` when interacting with Python's standard `base64` module."},{"fix":"If URL-safe characters are needed, either manually replace `+` with `-` and `/` with `_` (and vice-versa for decoding) on the output of `unpaddedbase64`, or consider using `base64.urlsafe_b64encode()` if padding is acceptable, then strip padding if necessary.","message":"This library handles the *unpadded* aspect of Base64 but does not automatically implement the URL-safe Base64 alphabet. Standard Base64 uses `+` and `/`, which need percent-encoding in URLs. URL-safe Base64 (RFC 4648 §5) replaces these with `-` and `_`. If you require both unpadded and URL-safe encoding, you'll need to handle the character substitution yourself or use `base64.urlsafe_b64encode/decode` (and be mindful of its padding behavior).","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}