This project is a wrapper on libbase64.

It aims to provide a fast base64 implementation for standard base64 encoding/decoding.


pip install pybase64


pybase64 uses the same API as Python base64 “modern interface” (introduced in Python 2.4) for an easy integration.

To get the fastest decoding, it is recommended to use the b64decode() and validate=True when possible.

import pybase64

# Standard encoding
print(pybase64.standard_b64encode(b'>>>foo???'))       # b'Pj4+Zm9vPz8/'
print(pybase64.standard_b64decode(b'Pj4+Zm9vPz8/'))    # b'>>>foo???'
print(pybase64.urlsafe_b64encode(b'>>>foo???'))        # b'Pj4-Zm9vPz8_'
print(pybase64.urlsafe_b64decode(b'Pj4-Zm9vPz8_'))     # b'>>>foo???'
print(pybase64.b64encode(b'>>>foo???', altchars='_:')) # b'Pj4_Zm9vPz8:'
print(pybase64.b64decode(b'Pj4_Zm9vPz8:', altchars='_:', validate=True)) # b'>>>foo???'

Check API for more details.

A command-line tool is also provided. It has encode, decode and benchmark subcommands.

usage: pybase64 [-h] [-v] {benchmark,encode,decode} ...

pybase64 command-line tool.

positional arguments:
                        tool help
    benchmark           -h for usage
    encode              -h for usage
    decode              -h for usage

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         show program's version number and exit


Running Python 3.6.0, Apple LLVM version 8.1.0 (clang-802.0.42), Mac OS X 10.12.6 on an Intel Core i7-4870HQ @ 2.50GHz

0.1.2 (C extension active)
bench: altchars=None, validate=False
pybase64._pybase64.b64encode:     3203.816 MB/s (13,271,472 bytes)
pybase64._pybase64.b64decode:      322.261 MB/s (13,271,472 bytes)
base64.b64encode:                  539.713 MB/s (13,271,472 bytes)
base64.b64decode:                  321.367 MB/s (13,271,472 bytes)
bench: altchars=None, validate=True
pybase64._pybase64.b64encode:     3119.150 MB/s (13,271,472 bytes)
pybase64._pybase64.b64decode:     4389.709 MB/s (13,271,472 bytes)
base64.b64encode:                  585.207 MB/s (13,271,472 bytes)
base64.b64decode:                  101.803 MB/s (13,271,472 bytes)
bench: altchars=b'-_', validate=False
pybase64._pybase64.b64encode:     2298.564 MB/s (13,271,472 bytes)
pybase64._pybase64.b64decode:      276.244 MB/s (13,271,472 bytes)
base64.b64encode:                  313.476 MB/s (13,271,472 bytes)
base64.b64decode:                  229.085 MB/s (13,271,472 bytes)
bench: altchars=b'-_', validate=True
pybase64._pybase64.b64encode:     2379.698 MB/s (13,271,472 bytes)
pybase64._pybase64.b64decode:     2862.796 MB/s (13,271,472 bytes)
base64.b64encode:                  315.344 MB/s (13,271,472 bytes)
base64.b64decode:                   91.621 MB/s (13,271,472 bytes)