Differences between revisions 3 and 5 (spanning 2 versions)
Revision 3 as of 2023-06-15 18:28:26
Size: 3725
Comment:
Revision 5 as of 2023-10-10 20:34:24
Size: 2943
Comment:
Deletions are marked like this. Additions are marked like this.
Line 11: Line 11:
== Example == == Usage ==

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

To go between an [[Email/Base64|base64]]-encoded `bytes`-like object and `bytes` object, try:
Line 23: Line 27:
== Usage == === Ascii85 ===
Line 25: Line 29:
Throughout this document, a `bytes`-like object is any of `bytes`, `bytearray`, `array.array`, or a strictly ASCII `str`. Use '''`a85encode()`''' and '''`a85decode()`''' to go between an Ascii85-encoded `bytes`-like object and `bytes`.
Line 31: Line 35:
=== A85Decode === === Base16 ===
Line 33: Line 37:
Decode an Ascii85-encoded `bytes`-like object into the original `bytes`. Use '''`b16encode()`''' and '''`b16decode()`''' to go between an base16-encoded `bytes`-like object and `bytes`.

A '''`binascii.Error`''' is raised by `b16decode()` if the `bytes`-like object is incorrectly padded or includes characters that are invalid for base16-encoding.
Line 39: Line 45:
=== A85Encode === === Base32 ===
Line 41: Line 47:
Encode a `bytes`-like object into Ascii85-encoded `bytes`. For the conventional (RFC 3548, RFC 4648) base32 alphabet (A-Z, 2-7), use '''`b32encode()`''' and '''`b32decode()`''' to go between an base32-encoded `bytes`-like object and `bytes`.

For the alternative 'base32hex' (RFC 2938, RFC 4648) alphabet (0-9, A-V) use '''`b32hexencode()`''' and '''`b32hexdecode()`'''.

A `binascii.Error` is raised by `b32decode` and `b32hexdecode` if the `bytes`-like object is incorrectly padded or includes characters that are invalid for base32-encoding.
Line 47: Line 57:
=== B16Decode === === Base64 ===
Line 49: Line 59:
Decode a base16-encoded `bytes`-like object into the original `bytes`. See the example above.
Line 51: Line 61:
A `binascii.Error` is raised if the `bytes`-like object is incorrectly padded or includes characters that are invalid for base16-encoding. A `binascii.Error` is raised by `b64decode` 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 by `b64decode` if the `bytes`-like object includes characters that are invalid for base64-encoding.



==== Alternative Characters ====

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`''' and '''`standard_b64decode`''' disallows this functionality
 * '''`urlsafe_b64encode`''' and '''`urlsafe_b64decode`''' hardcodes the alternative characters of `-` and `_`
Line 57: Line 76:
=== B16Encode ===
Line 59: Line 77:
Encode a `bytes`-like object into base16-encoded `bytes`. === Base85 ===
Line 61: Line 79:
----



=== 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`.
Use '''`b85encode()`''' and '''`b85decode()`''' to go between an base85-encoded `bytes`-like object and `bytes`.

Python Base64

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


Usage

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

To go between an base64-encoded bytes-like object and bytes object, try:

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


Ascii85

Use a85encode() and a85decode() to go between an Ascii85-encoded bytes-like object and bytes.


Base16

Use b16encode() and b16decode() to go between an base16-encoded bytes-like object and bytes.

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


Base32

For the conventional (RFC 3548, RFC 4648) base32 alphabet (A-Z, 2-7), use b32encode() and b32decode() to go between an base32-encoded bytes-like object and bytes.

For the alternative 'base32hex' (RFC 2938, RFC 4648) alphabet (0-9, A-V) use b32hexencode() and b32hexdecode().

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


Base64

See the example above.

A binascii.Error is raised by b64decode 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 by b64decode if the bytes-like object includes characters that are invalid for base64-encoding.

Alternative Characters

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 and standard_b64decode disallows this functionality

  • urlsafe_b64encode and urlsafe_b64decode hardcodes the alternative characters of - and _


Base85

Use b85encode() and b85decode() to go between an base85-encoded bytes-like object and 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)