Python Base64

base64 is a module that supports en-/decoding data with Base16, Base32, Base64, and Base85 encodings.


Example

import base64
encoded = base64.b64encode(b'data to be encoded')  # b'ZGF0YSB0byBiZSBlbmNvZGVk'
data = base64.b64decode(encoded)                   # b'data to be encoded'


Usage

Throughout this document, a bytes-like object is any of bytes, bytearray, array.array, or a strictly ASCII str.


A85Decode

Decode an Ascii85-encoded bytes-like object into the original bytes.


A85Encode

Encode a bytes-like object into Ascii85-encoded bytes.


B16Decode

Decode a base16-encoded bytes-like object into the original bytes.

A binascii.Error is raised if the bytes-like object is incorrectly padded or includes characters that are invalid for base16-encoding.


B16Encode

Encode a bytes-like object into base16-encoded bytes.


B32Decode

Decode a base32-encoded bytes-like object into the original bytes.

A binascii.Error is raised if the bytes-like object is incorrectly padded or includes characters that are invalid for base32-encoding.


B32Encode

Encode a bytes-like object into base32-encoded bytes.


B32HexDecode

Decode an extended base32-encoded bytes-like object into the original bytes.

A binascii.Error is raised if the bytes-like object is incorrectly padded or includes characters that are invalid for extended base32-encoding.


B32HexEncode

Encode a bytes-like object into extended base32-encoded bytes.


B64Decode

Decode a base64-encoded bytes-like object into the original bytes.

The keyword-only argument altchars allows for alternative characters to be used in place of + and /. This is primarily useful for embedding data into URLs, as those characters are not allowable in a URL component. A ValueError is raised if altchars is set to anything other than a two-long bytes-like object. Consider instead using one of:

A binascii.Error is raised if the bytes-like object is incorrectly padded for base64-encoding.

If the keyword-only argument validate is set to True, a binascii.Error is raised if the bytes-like object includes characters that are invalid for base64-encoding.


B64Encode

Encode a bytes-like object into base64-encoded bytes.

The keyword-only argument altchars allows for alternative characters to be used in place of + and /. This is primarily useful for embedding data into URLs, as those characters are not allowable in a URL component. A ValueError is raised if altchars is set to anything other than a two-long bytes-like object. Consider instead using one of:


B85Decode

Decode an base85-encoded bytes-like object into the original bytes.


B85Encode

Encode a bytes-like object into base85-encoded bytes.


See also

Python base64 module documentation


CategoryRicottone