API Reference

Main API Reference

pybase64.b64encode(s: Buffer, altchars: str | Buffer | None = None, *, padded: bool = True, wrapcol: int = 0) bytes[source]

Encode bytes using the standard Base64 alphabet.

Argument s is a bytes-like object to encode.

Optional altchars must be a byte string of length 2 which specifies an alternative alphabet for the ‘+’ and ‘/’ characters. This allows an application to e.g. generate url or filesystem safe Base64 strings.

Optional padded specifies whether to pad the encoded data with the ‘=’ character to a size multiple of 4.

Optional wrapcol specifies after how many characters the output should be split with a newline character (b'\n'). The value is rounded down to the nearest multiple of 4. If wrapcol is 0 (the default), no newlines are added.

The result is returned as a bytes object.

pybase64.b64encode_as_string(s: Buffer, altchars: str | Buffer | None = None, *, padded: bool = True, wrapcol: int = 0) str[source]

Encode bytes using the standard Base64 alphabet.

Argument s is a bytes-like object to encode.

Optional altchars must be a byte string of length 2 which specifies an alternative alphabet for the ‘+’ and ‘/’ characters. This allows an application to e.g. generate url or filesystem safe Base64 strings.

Optional padded specifies whether to pad the encoded data with the ‘=’ character to a size multiple of 4.

Optional wrapcol specifies after how many characters the output should be split with a newline character ('\n'). The value is rounded down to the nearest multiple of 4. If wrapcol is 0 (the default), no newlines are added.

The result is returned as a str object.

pybase64.b64decode(s: str | Buffer, altchars: str | Buffer | None = None, validate: bool | Literal[_Unspecified.UNSPECIFIED] = _Unspecified.UNSPECIFIED, *, padded: bool = True, ignorechars: Buffer | Literal[_Unspecified.UNSPECIFIED] = _Unspecified.UNSPECIFIED, canonical: bool = False) bytes[source]

Decode bytes encoded with the standard Base64 alphabet.

Argument s is a bytes-like object or ASCII string to decode.

Optional altchars must be a bytes-like object or ASCII string of length 2 which specifies the alternative alphabet used instead of the ‘+’ and ‘/’ characters.

If ignorechars is specified, it should be a bytes-like object containing characters to ignore from the input when validate is True. If ignorechars contains the pad character '=', the pad characters presented before the end of the encoded data and the excess pad characters will be ignored. The default value of validate is True if ignorechars is specified, False otherwise.

If validate is False, characters that are neither in the normal base-64 alphabet nor the alternative alphabet are discarded prior to the padding check. If validate is True, these non-alphabet characters in the input result in a binascii.Error.

If padded is False, padding in the input is not required or not allowed when validate is True and ignorechars does not contain the pad character '='.

If canonical is True, non-zero padding bits are rejected.

The result is returned as a bytes object.

A binascii.Error is raised if s is incorrectly padded.

pybase64.b64decode_as_bytearray(s: str | Buffer, altchars: str | Buffer | None = None, validate: bool | Literal[_Unspecified.UNSPECIFIED] = _Unspecified.UNSPECIFIED, *, padded: bool = True, ignorechars: Buffer | Literal[_Unspecified.UNSPECIFIED] = _Unspecified.UNSPECIFIED, canonical: bool = False) bytearray[source]

Decode bytes encoded with the standard Base64 alphabet.

Argument s is a bytes-like object or ASCII string to decode.

Optional altchars must be a bytes-like object or ASCII string of length 2 which specifies the alternative alphabet used instead of the ‘+’ and ‘/’ characters.

If ignorechars is specified, it should be a bytes-like object containing characters to ignore from the input when validate is True. If ignorechars contains the pad character '=', the pad characters presented before the end of the encoded data and the excess pad characters will be ignored. The default value of validate is True if ignorechars is specified, False otherwise.

If validate is False, characters that are neither in the normal base-64 alphabet nor the alternative alphabet are discarded prior to the padding check. If validate is True, these non-alphabet characters in the input result in a binascii.Error.

If padded is False, padding in the input is not required or not allowed when validate is True and ignorechars does not contain the pad character '='.

If canonical is True, non-zero padding bits are rejected.

The result is returned as a bytearray object.

A binascii.Error is raised if s is incorrectly padded.

Helpers API Reference

pybase64.standard_b64encode(s: Buffer) bytes[source]

Encode bytes using the standard Base64 alphabet.

Argument s is a bytes-like object to encode.

The result is returned as a bytes object.

pybase64.standard_b64decode(s: str | Buffer) bytes[source]

Decode bytes encoded with the standard Base64 alphabet.

Argument s is a bytes-like object or ASCII string to decode.

The result is returned as a bytes object.

A binascii.Error is raised if the input is incorrectly padded.

Characters that are not in the standard alphabet are discarded prior to the padding check.

pybase64.urlsafe_b64encode(s: Buffer, *, padded: bool = True) bytes[source]

Encode bytes using the URL- and filesystem-safe Base64 alphabet.

Argument s is a bytes-like object to encode.

The result is returned as a bytes object.

The alphabet uses ‘-’ instead of ‘+’ and ‘_’ instead of ‘/’.

The result can still contain = if padded is true (default).

pybase64.urlsafe_b64decode(s: str | Buffer, *, padded: bool = False) bytes[source]

Decode bytes using the URL- and filesystem-safe Base64 alphabet.

Argument s is a bytes-like object or ASCII string to decode.

The result is returned as a bytes object.

If padded is False, padding in the input is not required. Otherwise, a binascii.Error is raised if the input is incorrectly padded.

Characters that are not in the URL-safe base-64 alphabet, and are not a plus ‘+’ or slash ‘/’, are discarded prior to the padding check.

The alphabet uses ‘-’ instead of ‘+’ and ‘_’ instead of ‘/’.

Legacy API Reference

pybase64.encodebytes(s: Buffer) bytes[source]

Encode bytes into a bytes object with newlines (b’n’) inserted after every 76 bytes of output, and ensuring that there is a trailing newline, as per RFC 2045 (MIME).

Argument s is a bytes-like object to encode.

The result is returned as a bytes object.

Information API Reference

pybase64.get_version() str[source]

Return pybase64 version as a str object.

The result reports if the C extension is used or not. e.g. 1.0.0 (C extension active - AVX2)

pybase64.get_license_text() str[source]

Return pybase64 license information as a str object.

The result includes libbase64 license information as well.