base64 — Base16, Base32, Base64, Base85 Data Encodings

Source code:Lib/base64.py


This module provides functions for encoding binary data to printableASCII characters and decoding such encodings back to binary data.It provides encoding and decoding functions for the encodings specified inRFC 4648, which defines the Base16, Base32, and Base64 algorithms,and for the de-facto standard Ascii85 and Base85 encodings.

TheRFC 4648 encodings are suitable for encoding binary data so that it can besafely sent by email, used as parts of URLs, or included as part of an HTTPPOST request. The encoding algorithm is not the same as theuuencode program.

There are two interfaces provided by this module. The modern interfacesupports encodingbytes-like objects to ASCIIbytes, and decodingbytes-like objects orstrings containing ASCII tobytes. Both base-64 alphabetsdefined inRFC 4648 (normal, and URL- and filesystem-safe) are supported.

The legacy interface does not support decoding from strings, but it doesprovide functions for encoding and decoding to and fromfile objects. It only supports the Base64 standard alphabet, and it addsnewlines every 76 characters as perRFC 2045. Note that if you are lookingforRFC 2045 support you probably want to be looking at theemailpackage instead.

Άλλαξε στην έκδοση 3.3:ASCII-only Unicode strings are now accepted by the decoding functions ofthe modern interface.

Άλλαξε στην έκδοση 3.4:Anybytes-like objects are now accepted by allencoding and decoding functions in this module. Ascii85/Base85 support added.

The modern interface provides:

base64.b64encode(s,altchars=None)

Encode thebytes-like objects using Base64 and return the encodedbytes.

Optionalaltchars must be abytes-like object of length 2 whichspecifies an alternative alphabet for the+ and/ characters.This allows an application to e.g. generate URL or filesystem safe Base64strings. The default isNone, for which the standard Base64 alphabet is used.

May assert or raise aValueError if the length ofaltchars is not 2. Raises aTypeError ifaltchars is not abytes-like object.

base64.b64decode(s,altchars=None,validate=False)

Decode the Base64 encodedbytes-like object or ASCII strings and return the decodedbytes.

Optionalaltchars must be abytes-like object or ASCII stringof length 2 which specifies the alternative alphabet used instead of the+ and/ characters.

Abinascii.Error exception is raisedifs is incorrectly padded.

Ifvalidate isFalse (the default), characters that are neitherin the normal base-64 alphabet nor the alternative alphabet arediscarded prior to the padding check. Ifvalidate isTrue,these non-alphabet characters in the input result in abinascii.Error.

For more information about the strict base64 check, seebinascii.a2b_base64()

May assert or raise aValueError if the length ofaltchars is not 2.

base64.standard_b64encode(s)

Encodebytes-like objects using the standard Base64 alphabetand return the encodedbytes.

base64.standard_b64decode(s)

Decodebytes-like object or ASCII strings using the standardBase64 alphabet and return the decodedbytes.

base64.urlsafe_b64encode(s)

Encodebytes-like objects using theURL- and filesystem-safe alphabet, whichsubstitutes- instead of+ and_ instead of/ in thestandard Base64 alphabet, and return the encodedbytes. The resultcan still contain=.

base64.urlsafe_b64decode(s)

Decodebytes-like object or ASCII stringsusing the URL- and filesystem-safealphabet, which substitutes- instead of+ and_ instead of/ in the standard Base64 alphabet, and return the decodedbytes.

base64.b32encode(s)

Encode thebytes-like objects using Base32 and return theencodedbytes.

base64.b32decode(s,casefold=False,map01=None)

Decode the Base32 encodedbytes-like object or ASCII strings andreturn the decodedbytes.

Optionalcasefold is a flag specifyingwhether a lowercase alphabet is acceptable as input. For security purposes,the default isFalse.

RFC 4648 allows for optional mapping of the digit 0 (zero) to the letter O(oh), and for optional mapping of the digit 1 (one) to either the letter I (eye)or letter L (el). The optional argumentmap01 when notNone, specifieswhich letter the digit 1 should be mapped to (whenmap01 is notNone, thedigit 0 is always mapped to the letter O). For security purposes the default isNone, so that 0 and 1 are not allowed in the input.

Abinascii.Error is raised ifs isincorrectly padded or if there are non-alphabet characters present in theinput.

base64.b32hexencode(s)

Similar tob32encode() but uses the Extended Hex Alphabet, as defined inRFC 4648.

Added in version 3.10.

base64.b32hexdecode(s,casefold=False)

Similar tob32decode() but uses the Extended Hex Alphabet, as defined inRFC 4648.

This version does not allow the digit 0 (zero) to the letter O (oh) and digit1 (one) to either the letter I (eye) or letter L (el) mappings, all thesecharacters are included in the Extended Hex Alphabet and are notinterchangeable.

Added in version 3.10.

base64.b16encode(s)

Encode thebytes-like objects using Base16 and return theencodedbytes.

base64.b16decode(s,casefold=False)

Decode the Base16 encodedbytes-like object or ASCII strings andreturn the decodedbytes.

Optionalcasefold is a flag specifying whether alowercase alphabet is acceptable as input. For security purposes, the defaultisFalse.

Abinascii.Error is raised ifs isincorrectly padded or if there are non-alphabet characters present in theinput.

base64.a85encode(b,*,foldspaces=False,wrapcol=0,pad=False,adobe=False)

Encode thebytes-like objectb using Ascii85 and return theencodedbytes.

foldspaces is an optional flag that uses the special short sequence “y”instead of 4 consecutive spaces (ASCII 0x20) as supported by “btoa”. Thisfeature is not supported by the «standard» Ascii85 encoding.

wrapcol controls whether the output should have newline (b'\n')characters added to it. If this is non-zero, each output line will beat most this many characters long, excluding the trailing newline.

pad controls whether the input is padded to a multiple of 4before encoding. Note that thebtoa implementation always pads.

adobe controls whether the encoded byte sequence is framed with<~and~>, which is used by the Adobe implementation.

Added in version 3.4.

base64.a85decode(b,*,foldspaces=False,adobe=False,ignorechars=b'\t\n\r\x0b')

Decode the Ascii85 encodedbytes-like object or ASCII stringb andreturn the decodedbytes.

foldspaces is a flag that specifies whether the “y” short sequenceshould be accepted as shorthand for 4 consecutive spaces (ASCII 0x20).This feature is not supported by the «standard» Ascii85 encoding.

adobe controls whether the input sequence is in Adobe Ascii85 format(i.e. is framed with <~ and ~>).

ignorechars should be abytes-like object or ASCII stringcontaining characters to ignorefrom the input. This should only contain whitespace characters, and bydefault contains all whitespace characters in ASCII.

Added in version 3.4.

base64.b85encode(b,pad=False)

Encode thebytes-like objectb using base85 (as used in e.g.git-style binary diffs) and return the encodedbytes.

Ifpad is true, the input is padded withb'\0' so its length is amultiple of 4 bytes before encoding.

Added in version 3.4.

base64.b85decode(b)

Decode the base85-encodedbytes-like object or ASCII stringb andreturn the decodedbytes. Padding is implicitly removed, ifnecessary.

Added in version 3.4.

base64.z85encode(s)

Encode thebytes-like objects using Z85 (as used in ZeroMQ)and return the encodedbytes. SeeZ85 specification for more information.

Added in version 3.13.

base64.z85decode(s)

Decode the Z85-encodedbytes-like object or ASCII strings andreturn the decodedbytes. SeeZ85 specification for more information.

Added in version 3.13.

The legacy interface:

base64.decode(input,output)

Decode the contents of the binaryinput file and write the resulting binarydata to theoutput file.input andoutput must befile objects.input will be read untilinput.readline() returns anempty bytes object.

base64.decodebytes(s)

Decode thebytes-like objects, which must contain one or morelines of base64 encoded data, and return the decodedbytes.

Added in version 3.1.

base64.encode(input,output)

Encode the contents of the binaryinput file and write the resulting base64encoded data to theoutput file.input andoutput must befileobjects.input will be read untilinput.read() returnsan empty bytes object.encode() inserts a newline character (b'\n')after every 76 bytes of the output, as well as ensuring that the outputalways ends with a newline, as perRFC 2045 (MIME).

base64.encodebytes(s)

Encode thebytes-like objects, which can contain arbitrary binarydata, and returnbytes containing the base64-encoded data, with newlines(b'\n') inserted after every 76 bytes of output, and ensuring thatthere is a trailing newline, as perRFC 2045 (MIME).

Added in version 3.1.

An example usage of the module:

>>>importbase64>>>encoded=base64.b64encode(b'data to be encoded')>>>encodedb'ZGF0YSB0byBiZSBlbmNvZGVk'>>>data=base64.b64decode(encoded)>>>datab'data to be encoded'

Security Considerations

A new security considerations section was added toRFC 4648 (section 12); it’srecommended to review the security section for any code deployed to production.

Δείτε επίσης

Modulebinascii

Support module containing ASCII-to-binary and binary-to-ASCII conversions.

RFC 1521 - MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies

Section 5.2, «Base64 Content-Transfer-Encoding,» provides the definition of thebase64 encoding.