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 3548, which defines the Base16, Base32, and Base64 algorithms,and for the de-facto standard Ascii85 and Base85 encodings.
TheRFC 3548 encodings are suitable for encoding binary data so that it cansafely 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 3548 (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.
Changed in version 3.3:ASCII-only Unicode strings are now accepted by the decoding functions ofthe modern interface.
Changed in version 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 encoded
bytes.Optionalaltchars must be abytes-like object of at leastlength 2 (additional characters are ignored) which specifies an alternativealphabet for the
+and/characters. This allows an application to e.g.generate URL or filesystem safe Base64 strings. The default isNone, forwhich the standard Base64 alphabet is used.
base64.b64decode(s,altchars=None,validate=False)¶Decode the Base64 encodedbytes-like object or ASCII strings and return the decoded
bytes.Optionalaltchars must be abytes-like object or ASCII string ofat least length 2 (additional characters are ignored) which specifies thealternative alphabet used instead of the
+and/characters.A
binascii.Errorexception is raisedifs is incorrectly padded.Ifvalidate is
False(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.
base64.standard_b64encode(s)¶Encodebytes-like objects using the standard Base64 alphabetand return the encoded
bytes.
base64.standard_b64decode(s)¶Decodebytes-like object or ASCII strings using the standardBase64 alphabet and return the decoded
bytes.
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 theencoded
bytes.
base64.b32decode(s,casefold=False,map01=None)¶Decode the Base32 encodedbytes-like object or ASCII strings andreturn the decoded
bytes.Optionalcasefold is a flag specifyingwhether a lowercase alphabet is acceptable as input. For security purposes,the default is
False.RFC 3548 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 not
None, 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.A
binascii.Erroris raised ifs isincorrectly padded or if there are non-alphabet characters present in theinput.
base64.b16encode(s)¶Encode thebytes-like objects using Base16 and return theencoded
bytes.
base64.b16decode(s,casefold=False)¶Decode the Base16 encodedbytes-like object or ASCII strings andreturn the decoded
bytes.Optionalcasefold is a flag specifying whether alowercase alphabet is acceptable as input. For security purposes, the defaultis
False.A
binascii.Erroris 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 theencoded
bytes.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.pad controls whether the input is padded to a multiple of 4before encoding. Note that the
btoaimplementation always pads.adobe controls whether the encoded byte sequence is framed with
<~and~>, which is used by the Adobe implementation.New in version 3.4.
base64.a85decode(b,*,foldspaces=False,adobe=False,ignorechars=b' \t\n\r\v')¶Decode the Ascii85 encodedbytes-like object or ASCII stringb andreturn the decoded
bytes.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.
New 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 encoded
bytes.Ifpad is true, the input is padded with
b'\0'so its length is amultiple of 4 bytes before encoding.New in version 3.4.
base64.b85decode(b)¶Decode the base85-encodedbytes-like object or ASCII stringb andreturn the decoded
bytes. Padding is implicitly removed, ifnecessary.New in version 3.4.
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 until
input.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 decoded
bytes.New in version 3.1.
base64.decodestring(s)¶Deprecated alias of
decodebytes().Deprecated since 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 until
input.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 return
bytescontaining 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).New in version 3.1.
base64.encodestring(s)¶Deprecated alias of
encodebytes().Deprecated since 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'
See also
- Module
binascii 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.