blob: 3f0161ffad76c736e2859696254508f18007b5d2 [file] [log] [blame]
Larry Hastings3732ed22014-03-15 21:13:56 -07001:mod:`base64` --- Base16, Base32, Base64, Base85 Data Encodings
2===============================================================
Georg Brandl116aa622007-08-15 14:28:22 +00003
4.. module:: base64
Larry Hastings3732ed22014-03-15 21:13:56 -07005 :synopsis: RFC 3548: Base16, Base32, Base64 Data Encodings;
6 Base85 and Ascii85
Georg Brandl116aa622007-08-15 14:28:22 +00007
8
9.. index::
10 pair: base64; encoding
11 single: MIME; base64 encoding
12
Larry Hastings3732ed22014-03-15 21:13:56 -070013This module provides functions for encoding binary data to printable
14ASCII characters and decoding such encodings back to binary data.
15It provides encoding and decoding functions for the encodings specified in
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020016:rfc:`3548`, which defines the Base16, Base32, and Base64 algorithms,
Larry Hastings3732ed22014-03-15 21:13:56 -070017and for the de-facto standard Ascii85 and Base85 encodings.
18
19The :rfc:`3548` encodings are suitable for encoding binary data so that it can
R. David Murray7cefc302010-10-17 23:12:16 +000020safely sent by email, used as parts of URLs, or included as part of an HTTP
21POST request. The encoding algorithm is not the same as the
22:program:`uuencode` program.
Georg Brandl116aa622007-08-15 14:28:22 +000023
R David Murraya1986452015-12-23 21:17:17 -050024There are two interfaces provided by this module. The modern interface
25supports encoding :term:`bytes-like objects <bytes-like object>` to ASCII
26:class:`bytes`, and decoding :term:`bytes-like objects <bytes-like object>` or
27strings containing ASCII to :class:`bytes`. All three :rfc:`3548` defined
28alphabets (normal, URL-safe, and filesystem-safe) are supported.
29
30The legacy interface does not support decoding from strings, but it does
31provide functions for encoding and decoding to and from :term:`file objects
32<file object>`. It only supports the Base64 standard alphabet, and it adds
33newlines every 76 characters as per :rfc:`2045`. Note that if you are looking
34for :rfc:`2045` support you probably want to be looking at the :mod:`email`
35package instead.
36
Antoine Pitrouea6b4d52012-02-20 19:30:23 +010037
38.. versionchanged:: 3.3
39 ASCII-only Unicode strings are now accepted by the decoding functions of
40 the modern interface.
Georg Brandl116aa622007-08-15 14:28:22 +000041
Nick Coghlanfdf239a2013-10-03 00:43:22 +100042.. versionchanged:: 3.4
43 Any :term:`bytes-like object`\ s are now accepted by all
Larry Hastings3732ed22014-03-15 21:13:56 -070044 encoding and decoding functions in this module. Ascii85/Base85 support added.
Nick Coghlanfdf239a2013-10-03 00:43:22 +100045
Georg Brandle6bcc912008-05-12 18:05:20 +000046The modern interface provides:
Georg Brandl116aa622007-08-15 14:28:22 +000047
Georg Brandlb868a662009-04-02 02:56:10 +000048.. function:: b64encode(s, altchars=None)
Georg Brandl116aa622007-08-15 14:28:22 +000049
R David Murraya1986452015-12-23 21:17:17 -050050 Encode the :term:`bytes-like object` *s* using Base64 and return the encoded
51 :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +000052
R David Murraya1986452015-12-23 21:17:17 -050053 Optional *altchars* must be a :term:`bytes-like object` of at least
Georg Brandl116aa622007-08-15 14:28:22 +000054 length 2 (additional characters are ignored) which specifies an alternative
55 alphabet for the ``+`` and ``/`` characters. This allows an application to e.g.
56 generate URL or filesystem safe Base64 strings. The default is ``None``, for
57 which the standard Base64 alphabet is used.
58
Georg Brandl116aa622007-08-15 14:28:22 +000059
R. David Murray64951362010-11-11 20:09:20 +000060.. function:: b64decode(s, altchars=None, validate=False)
Georg Brandl116aa622007-08-15 14:28:22 +000061
R David Murraya1986452015-12-23 21:17:17 -050062 Decode the Base64 encoded :term:`bytes-like object` or ASCII string
63 *s* and return the decoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +000064
R David Murraya1986452015-12-23 21:17:17 -050065 Optional *altchars* must be a :term:`bytes-like object` or ASCII string of
R. David Murray7cefc302010-10-17 23:12:16 +000066 at least length 2 (additional characters are ignored) which specifies the
67 alternative alphabet used instead of the ``+`` and ``/`` characters.
Georg Brandl116aa622007-08-15 14:28:22 +000068
R David Murraya1986452015-12-23 21:17:17 -050069 A :exc:`binascii.Error` exception is raised
Éric Araujo941afed2011-09-01 02:47:34 +020070 if *s* is incorrectly padded.
R. David Murray64951362010-11-11 20:09:20 +000071
72 If *validate* is ``False`` (the default), non-base64-alphabet characters are
73 discarded prior to the padding check. If *validate* is ``True``,
74 non-base64-alphabet characters in the input result in a
75 :exc:`binascii.Error`.
Georg Brandl116aa622007-08-15 14:28:22 +000076
77
78.. function:: standard_b64encode(s)
79
R David Murraya1986452015-12-23 21:17:17 -050080 Encode :term:`bytes-like object` *s* using the standard Base64 alphabet
81 and return the encoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +000082
83
84.. function:: standard_b64decode(s)
85
R David Murraya1986452015-12-23 21:17:17 -050086 Decode :term:`bytes-like object` or ASCII string *s* using the standard
87 Base64 alphabet and return the decoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +000088
89
90.. function:: urlsafe_b64encode(s)
91
R David Murraya1986452015-12-23 21:17:17 -050092 Encode :term:`bytes-like object` *s* using a URL-safe alphabet, which
93 substitutes ``-`` instead of ``+`` and ``_`` instead of ``/`` in the
94 standard Base64 alphabet, and return the encoded :class:`bytes`. The result
Benjamin Petersond75fcb42009-02-19 04:22:03 +000095 can still contain ``=``.
Georg Brandl116aa622007-08-15 14:28:22 +000096
97
98.. function:: urlsafe_b64decode(s)
99
R David Murraya1986452015-12-23 21:17:17 -0500100 Decode :term:`bytes-like object` or ASCII string *s* using a URL-safe
101 alphabet, which substitutes ``-`` instead of ``+`` and ``_`` instead of
102 ``/`` in the standard Base64 alphabet, and return the decoded
103 :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +0000104
105
106.. function:: b32encode(s)
107
R David Murraya1986452015-12-23 21:17:17 -0500108 Encode the :term:`bytes-like object` *s* using Base32 and return the
109 encoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +0000110
111
Georg Brandlb868a662009-04-02 02:56:10 +0000112.. function:: b32decode(s, casefold=False, map01=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000113
R David Murraya1986452015-12-23 21:17:17 -0500114 Decode the Base32 encoded :term:`bytes-like object` or ASCII string *s* and
115 return the decoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +0000116
R David Murraya1986452015-12-23 21:17:17 -0500117 Optional *casefold* is a flag specifying
R. David Murray7cefc302010-10-17 23:12:16 +0000118 whether a lowercase alphabet is acceptable as input. For security purposes,
119 the default is ``False``.
Georg Brandl116aa622007-08-15 14:28:22 +0000120
121 :rfc:`3548` allows for optional mapping of the digit 0 (zero) to the letter O
122 (oh), and for optional mapping of the digit 1 (one) to either the letter I (eye)
123 or letter L (el). The optional argument *map01* when not ``None``, specifies
124 which letter the digit 1 should be mapped to (when *map01* is not ``None``, the
125 digit 0 is always mapped to the letter O). For security purposes the default is
126 ``None``, so that 0 and 1 are not allowed in the input.
127
R David Murraya1986452015-12-23 21:17:17 -0500128 A :exc:`binascii.Error` is raised if *s* is
Georg Brandl116aa622007-08-15 14:28:22 +0000129 incorrectly padded or if there are non-alphabet characters present in the
R David Murraya1986452015-12-23 21:17:17 -0500130 input.
Georg Brandl116aa622007-08-15 14:28:22 +0000131
132
133.. function:: b16encode(s)
134
R David Murraya1986452015-12-23 21:17:17 -0500135 Encode the :term:`bytes-like object` *s* using Base16 and return the
136 encoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +0000137
138
Georg Brandlb868a662009-04-02 02:56:10 +0000139.. function:: b16decode(s, casefold=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000140
R David Murraya1986452015-12-23 21:17:17 -0500141 Decode the Base16 encoded :term:`bytes-like object` or ASCII string *s* and
142 return the decoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +0000143
R David Murraya1986452015-12-23 21:17:17 -0500144 Optional *casefold* is a flag specifying whether a
Georg Brandl116aa622007-08-15 14:28:22 +0000145 lowercase alphabet is acceptable as input. For security purposes, the default
146 is ``False``.
147
R David Murraya1986452015-12-23 21:17:17 -0500148 A :exc:`TypeError` is raised if *s* is
Georg Brandl116aa622007-08-15 14:28:22 +0000149 incorrectly padded or if there are non-alphabet characters present in the
R David Murraya1986452015-12-23 21:17:17 -0500150 input.
Georg Brandl116aa622007-08-15 14:28:22 +0000151
Georg Brandl116aa622007-08-15 14:28:22 +0000152
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100153.. function:: a85encode(s, *, foldspaces=False, wrapcol=0, pad=False, adobe=False)
154
R David Murraya1986452015-12-23 21:17:17 -0500155 Encode the :term:`bytes-like object` *s* using Ascii85 and return the
156 encoded :class:`bytes`.
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100157
158 *foldspaces* is an optional flag that uses the special short sequence 'y'
159 instead of 4 consecutive spaces (ASCII 0x20) as supported by 'btoa'. This
160 feature is not supported by the "standard" Ascii85 encoding.
161
R David Murraya1986452015-12-23 21:17:17 -0500162 *wrapcol* controls whether the output should have newline (``b'\n'``)
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100163 characters added to it. If this is non-zero, each output line will be
164 at most this many characters long.
165
R David Murraya1986452015-12-23 21:17:17 -0500166 *pad* controls whether the input is padded to a multiple of 4
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100167 before encoding. Note that the ``btoa`` implementation always pads.
168
169 *adobe* controls whether the encoded byte sequence is framed with ``<~``
170 and ``~>``, which is used by the Adobe implementation.
171
172 .. versionadded:: 3.4
173
174
Serhiy Storchakabf7b9ed2015-11-23 16:43:05 +0200175.. function:: a85decode(s, *, foldspaces=False, adobe=False, ignorechars=b' \\t\\n\\r\\v')
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100176
R David Murraya1986452015-12-23 21:17:17 -0500177 Decode the Ascii85 encoded :term:`bytes-like object` or ASCII string *s* and
178 return the decoded :class:`bytes`.
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100179
180 *foldspaces* is a flag that specifies whether the 'y' short sequence
181 should be accepted as shorthand for 4 consecutive spaces (ASCII 0x20).
182 This feature is not supported by the "standard" Ascii85 encoding.
183
184 *adobe* controls whether the input sequence is in Adobe Ascii85 format
185 (i.e. is framed with <~ and ~>).
186
R David Murraya1986452015-12-23 21:17:17 -0500187 *ignorechars* should be a :term:`bytes-like object` or ASCII string
188 containing characters to ignore
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100189 from the input. This should only contain whitespace characters, and by
190 default contains all whitespace characters in ASCII.
191
192 .. versionadded:: 3.4
193
194
195.. function:: b85encode(s, pad=False)
196
R David Murraya1986452015-12-23 21:17:17 -0500197 Encode the :term:`bytes-like object` *s* using base85 (as used in e.g.
198 git-style binary diffs) and return the encoded :class:`bytes`.
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100199
R David Murraya1986452015-12-23 21:17:17 -0500200 If *pad* is true, the input is padded with ``b'\0'`` so its length is a
201 multiple of 4 bytes before encoding.
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100202
203 .. versionadded:: 3.4
204
205
206.. function:: b85decode(b)
207
R David Murraya1986452015-12-23 21:17:17 -0500208 Decode the base85-encoded :term:`bytes-like object` or ASCII string *b* and
209 return the decoded :class:`bytes`. Padding is implicitly removed, if
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100210 necessary.
211
212 .. versionadded:: 3.4
213
214
215.. note::
216 Both Base85 and Ascii85 have an expansion factor of 5 to 4 (5 Base85 or
217 Ascii85 characters can encode 4 binary bytes), while the better-known
218 Base64 has an expansion factor of 6 to 4. They are therefore more
219 efficient when space expensive. They differ by details such as the
220 character map used for encoding.
221
222
Georg Brandlb54d8012009-06-04 09:11:51 +0000223The legacy interface:
Georg Brandl116aa622007-08-15 14:28:22 +0000224
225.. function:: decode(input, output)
226
Georg Brandlb54d8012009-06-04 09:11:51 +0000227 Decode the contents of the binary *input* file and write the resulting binary
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000228 data to the *output* file. *input* and *output* must be :term:`file objects
R David Murraya1986452015-12-23 21:17:17 -0500229 <file object>`. *input* will be read until ``input.readline()`` returns an
230 empty bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +0000231
232
Georg Brandlb54d8012009-06-04 09:11:51 +0000233.. function:: decodebytes(s)
234 decodestring(s)
Georg Brandl116aa622007-08-15 14:28:22 +0000235
R David Murraya1986452015-12-23 21:17:17 -0500236 Decode the :term:`bytes-like object` *s*, which must contain one or more
237 lines of base64 encoded data, and return the decoded :class:`bytes`.
Georg Brandlb54d8012009-06-04 09:11:51 +0000238 ``decodestring`` is a deprecated alias.
Georg Brandl116aa622007-08-15 14:28:22 +0000239
R David Murray75fd2252012-08-17 20:55:21 -0400240 .. versionadded:: 3.1
241
Georg Brandl116aa622007-08-15 14:28:22 +0000242
243.. function:: encode(input, output)
244
Georg Brandlb54d8012009-06-04 09:11:51 +0000245 Encode the contents of the binary *input* file and write the resulting base64
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000246 encoded data to the *output* file. *input* and *output* must be :term:`file
247 objects <file object>`. *input* will be read until ``input.read()`` returns
R David Murraya1986452015-12-23 21:17:17 -0500248 an empty bytes object. :func:`encode` inserts a newline character (``b'\n'``)
249 after every 76 bytes of the output, as well as ensuring that the output
250 always ends with a newline, as per :rfc:`2045` (MIME).
Georg Brandl116aa622007-08-15 14:28:22 +0000251
252
Georg Brandlb54d8012009-06-04 09:11:51 +0000253.. function:: encodebytes(s)
254 encodestring(s)
Georg Brandl116aa622007-08-15 14:28:22 +0000255
R David Murraya1986452015-12-23 21:17:17 -0500256 Encode the :term:`bytes-like object` *s*, which can contain arbitrary binary
257 data, and return :class:`bytes` containing the base64-encoded data, with newlines
258 (``b'\n'``) inserted after every 76 bytes of output, and ensuring that
259 there is a trailing newline, as per :rfc:`2045` (MIME).
260
Georg Brandlb54d8012009-06-04 09:11:51 +0000261 ``encodestring`` is a deprecated alias.
262
Georg Brandl116aa622007-08-15 14:28:22 +0000263
Christian Heimesfe337bf2008-03-23 21:54:12 +0000264An example usage of the module:
Georg Brandl116aa622007-08-15 14:28:22 +0000265
266 >>> import base64
Georg Brandl134c35b2010-10-17 11:36:28 +0000267 >>> encoded = base64.b64encode(b'data to be encoded')
Georg Brandl116aa622007-08-15 14:28:22 +0000268 >>> encoded
Georg Brandl38d54f72009-01-18 10:43:58 +0000269 b'ZGF0YSB0byBiZSBlbmNvZGVk'
Georg Brandl116aa622007-08-15 14:28:22 +0000270 >>> data = base64.b64decode(encoded)
271 >>> data
Georg Brandl134c35b2010-10-17 11:36:28 +0000272 b'data to be encoded'
Georg Brandl116aa622007-08-15 14:28:22 +0000273
274
275.. seealso::
276
277 Module :mod:`binascii`
278 Support module containing ASCII-to-binary and binary-to-ASCII conversions.
279
280 :rfc:`1521` - MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies
281 Section 5.2, "Base64 Content-Transfer-Encoding," provides the definition of the
282 base64 encoding.
283