pybase64
pybase64 is a Python library that provides a fast Base64 encoding/decoding implementation by wrapping the highly optimized `libbase64` C library. It maintains an active development status with regular updates to support new Python versions and performance enhancements. The current version is 1.4.3, and new releases often coincide with Python's release cycle.
Warnings
- breaking pybase64 dropped support for Python 3.6 and 3.7 with the release of version 1.4.0. Users on these Python versions must either upgrade their Python environment or remain on pybase64 versions prior to 1.4.0.
- gotcha For the fastest decoding, especially with trusted input, it is recommended to use the `b64decode` function with the `validate=True` argument. This avoids character validation overhead.
- gotcha Prior to version 1.3.2, `pybase64` might have produced incorrect outcomes when decoding non C-contiguous buffers, particularly in PyPy environments. This was fixed in v1.3.2.
- deprecated A deprecation warning related to `argparse.FileType` usage was fixed in version 1.4.2. If you use `pybase64` within command-line tools that rely on `argparse.FileType`, older versions might emit warnings.
- gotcha Unlike some legacy `base64` module functions, `pybase64`'s core `b64encode` and `b64decode` functions primarily work with and return `bytes` objects. If string or bytearray output is required, use `b64encode_as_string` (returns `str`) or `b64decode_as_bytearray` (returns `bytearray`) introduced in v1.1.0.
Install
-
pip install pybase64
Imports
- pybase64
import pybase64
- b64encode
pybase64.b64encode
- b64decode
pybase64.b64decode
- standard_b64encode
pybase64.standard_b64encode
- urlsafe_b64encode
pybase64.urlsafe_b64encode
Quickstart
import pybase64
original_data = b'Hello, world! This is a test string for pybase64.'
# Standard Base64 encoding
encoded_standard = pybase64.standard_b64encode(original_data)
print(f"Standard Encoded: {encoded_standard}")
# Standard Base64 decoding
decoded_standard = pybase64.standard_b64decode(encoded_standard)
print(f"Standard Decoded: {decoded_standard}\n")
# URL-safe Base64 encoding
encoded_urlsafe = pybase64.urlsafe_b64encode(original_data)
print(f"URL-safe Encoded: {encoded_urlsafe}")
# URL-safe Base64 decoding with validation for best performance
decoded_urlsafe = pybase64.urlsafe_b64decode(encoded_urlsafe, validate=True)
print(f"URL-safe Decoded: {decoded_urlsafe}")
assert original_data == decoded_standard
assert original_data == decoded_urlsafe