Differences between revisions 1 and 2
Revision 1 as of 2023-04-11 15:26:55
Size: 3645
Comment:
Revision 2 as of 2023-06-15 00:25:08
Size: 3735
Comment:
Deletions are marked like this. Additions are marked like this.
Line 151: Line 151:
[[https://pymotw.com/3/base64/index.html|Python Module of the Day article for base64]]

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:

  • standard_b64decode disallows this functionality

  • urlsafe_b64decode hardcodes the alternative characters of - and _

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:

  • standard_b64encode disallows this functionality

  • urlsafe_b64encode hardcodes the alternative characters of - and _


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

Python Module of the Day article for base64


CategoryRicottone

Python/Base64 (last edited 2023-10-10 20:34:24 by DominicRicottone)