Base 64 encoding represents a common scheme for encoding binary into ASCII string format using radix 64. The base64 module is part of the standard library, which means it installs along with Python. Understanding of bytes and strings is critical to this topic and can be reviewed here. This topic explains how to use the various features and number bases of the base64 module.
Parameter | Description |
---|---|
base64.b64encode(s, altchars=None) | |
s | A bytes-like object |
altchars | A bytes-like object of length 2+ of characters to replace the '+' and '=' characters when creating the Base64 alphabet. Extra characters are ignored. |
base64.b64decode(s, altchars=None, validate=False) | |
s | A bytes-like object |
altchars | A bytes-like object of length 2+ of characters to replace the '+' and '=' characters when creating the Base64 alphabet. Extra characters are ignored. |
validate | If valide is True, the characters not in the normal Base64 alphabet or the alternative alphabet are not discarded before the padding check |
base64.standard_b64encode(s) | |
s | A bytes-like object |
base64.standard_b64decode(s) | |
s | A bytes-like object |
base64.urlsafe_b64encode(s) | |
s | A bytes-like object |
base64.urlsafe_b64decode(s) | |
s | A bytes-like object |
b32encode(s) | |
s | A bytes-like object |
b32decode(s) | |
s | A bytes-like object |
base64.b16encode(s) | |
s | A bytes-like object |
base64.b16decode(s) | |
s | A bytes-like object |
base64.a85encode(b, foldspaces=False, wrapcol=0, pad=False, adobe=False) | |
b | A bytes-like object |
foldspaces | If foldspaces is True, the character 'y' will be used instead of 4 consecutive spaces. |
wrapcol | The number characters before a newline (0 implies no newlines) |
pad | If pad is True, the bytes are padded to a multiple of 4 before encoding |
adobe | If adobe is True, the encoded sequened with be framed with '<~' and ''~>' as used with Adobe products |
base64.a85decode(b, foldspaces=False, adobe=False, ignorechars=b'\t\n\r\v') | |
b | A bytes-like object |
foldspaces | If foldspaces is True, the character 'y' will be used instead of 4 consecutive spaces. |
adobe | If adobe is True, the encoded sequened with be framed with '<~' and ''~>' as used with Adobe products |
ignorechars | A bytes-like object of characters to ignore in the encoding process |
base64.b85encode(b, pad=False) | |
b | A bytes-like object |
pad | If pad is True, the bytes are padded to a multiple of 4 before encoding |
base64.b85decode(b) | |
b | A bytes-like object |
Up until Python 3.4 came out, base64 encoding and decoding functions only worked with bytes
or bytearray
types. Now these functions accept any bytes-like object.