blob: 6e88a7651859de2e51f65b56328898db11c80e06 [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
Martin Panteree3074e2016-02-23 22:30:50 +000027strings containing ASCII to :class:`bytes`. Both base-64 alphabets
28defined in :rfc:`3548` (normal, and URL- and filesystem-safe) are supported.
R David Murraya1986452015-12-23 21:17:17 -050029
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
Martin Panteree3074e2016-02-23 22:30:50 +000072 If *validate* is ``False`` (the default), characters that are neither
73 in the normal base-64 alphabet nor the alternative alphabet are
R. David Murray64951362010-11-11 20:09:20 +000074 discarded prior to the padding check. If *validate* is ``True``,
Martin Panteree3074e2016-02-23 22:30:50 +000075 these non-alphabet characters in the input result in a
R. David Murray64951362010-11-11 20:09:20 +000076 :exc:`binascii.Error`.
Georg Brandl116aa622007-08-15 14:28:22 +000077
78
79.. function:: standard_b64encode(s)
80
R David Murraya1986452015-12-23 21:17:17 -050081 Encode :term:`bytes-like object` *s* using the standard Base64 alphabet
82 and return the encoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +000083
84
85.. function:: standard_b64decode(s)
86
R David Murraya1986452015-12-23 21:17:17 -050087 Decode :term:`bytes-like object` or ASCII string *s* using the standard
88 Base64 alphabet and return the decoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +000089
90
91.. function:: urlsafe_b64encode(s)
92
Martin Panteree3074e2016-02-23 22:30:50 +000093 Encode :term:`bytes-like object` *s* using the
94 URL- and filesystem-safe alphabet, which
R David Murraya1986452015-12-23 21:17:17 -050095 substitutes ``-`` instead of ``+`` and ``_`` instead of ``/`` in the
96 standard Base64 alphabet, and return the encoded :class:`bytes`. The result
Benjamin Petersond75fcb42009-02-19 04:22:03 +000097 can still contain ``=``.
Georg Brandl116aa622007-08-15 14:28:22 +000098
99
100.. function:: urlsafe_b64decode(s)
101
Martin Panteree3074e2016-02-23 22:30:50 +0000102 Decode :term:`bytes-like object` or ASCII string *s*
103 using the URL- and filesystem-safe
R David Murraya1986452015-12-23 21:17:17 -0500104 alphabet, which substitutes ``-`` instead of ``+`` and ``_`` instead of
105 ``/`` in the standard Base64 alphabet, and return the decoded
106 :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +0000107
108
109.. function:: b32encode(s)
110
R David Murraya1986452015-12-23 21:17:17 -0500111 Encode the :term:`bytes-like object` *s* using Base32 and return the
112 encoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +0000113
114
Georg Brandlb868a662009-04-02 02:56:10 +0000115.. function:: b32decode(s, casefold=False, map01=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000116
R David Murraya1986452015-12-23 21:17:17 -0500117 Decode the Base32 encoded :term:`bytes-like object` or ASCII string *s* and
118 return the decoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +0000119
R David Murraya1986452015-12-23 21:17:17 -0500120 Optional *casefold* is a flag specifying
R. David Murray7cefc302010-10-17 23:12:16 +0000121 whether a lowercase alphabet is acceptable as input. For security purposes,
122 the default is ``False``.
Georg Brandl116aa622007-08-15 14:28:22 +0000123
124 :rfc:`3548` allows for optional mapping of the digit 0 (zero) to the letter O
125 (oh), and for optional mapping of the digit 1 (one) to either the letter I (eye)
126 or letter L (el). The optional argument *map01* when not ``None``, specifies
127 which letter the digit 1 should be mapped to (when *map01* is not ``None``, the
128 digit 0 is always mapped to the letter O). For security purposes the default is
129 ``None``, so that 0 and 1 are not allowed in the input.
130
R David Murraya1986452015-12-23 21:17:17 -0500131 A :exc:`binascii.Error` is raised if *s* is
Georg Brandl116aa622007-08-15 14:28:22 +0000132 incorrectly padded or if there are non-alphabet characters present in the
R David Murraya1986452015-12-23 21:17:17 -0500133 input.
Georg Brandl116aa622007-08-15 14:28:22 +0000134
135
136.. function:: b16encode(s)
137
R David Murraya1986452015-12-23 21:17:17 -0500138 Encode the :term:`bytes-like object` *s* using Base16 and return the
139 encoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +0000140
141
Georg Brandlb868a662009-04-02 02:56:10 +0000142.. function:: b16decode(s, casefold=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000143
R David Murraya1986452015-12-23 21:17:17 -0500144 Decode the Base16 encoded :term:`bytes-like object` or ASCII string *s* and
145 return the decoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +0000146
R David Murraya1986452015-12-23 21:17:17 -0500147 Optional *casefold* is a flag specifying whether a
Georg Brandl116aa622007-08-15 14:28:22 +0000148 lowercase alphabet is acceptable as input. For security purposes, the default
149 is ``False``.
150
Martin Panteree3074e2016-02-23 22:30:50 +0000151 A :exc:`binascii.Error` is raised if *s* is
Georg Brandl116aa622007-08-15 14:28:22 +0000152 incorrectly padded or if there are non-alphabet characters present in the
R David Murraya1986452015-12-23 21:17:17 -0500153 input.
Georg Brandl116aa622007-08-15 14:28:22 +0000154
Georg Brandl116aa622007-08-15 14:28:22 +0000155
Martin Panteree3074e2016-02-23 22:30:50 +0000156.. function:: a85encode(b, *, foldspaces=False, wrapcol=0, pad=False, adobe=False)
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100157
Martin Panteree3074e2016-02-23 22:30:50 +0000158 Encode the :term:`bytes-like object` *b* using Ascii85 and return the
R David Murraya1986452015-12-23 21:17:17 -0500159 encoded :class:`bytes`.
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100160
161 *foldspaces* is an optional flag that uses the special short sequence 'y'
162 instead of 4 consecutive spaces (ASCII 0x20) as supported by 'btoa'. This
163 feature is not supported by the "standard" Ascii85 encoding.
164
R David Murraya1986452015-12-23 21:17:17 -0500165 *wrapcol* controls whether the output should have newline (``b'\n'``)
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100166 characters added to it. If this is non-zero, each output line will be
167 at most this many characters long.
168
R David Murraya1986452015-12-23 21:17:17 -0500169 *pad* controls whether the input is padded to a multiple of 4
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100170 before encoding. Note that the ``btoa`` implementation always pads.
171
172 *adobe* controls whether the encoded byte sequence is framed with ``<~``
173 and ``~>``, which is used by the Adobe implementation.
174
175 .. versionadded:: 3.4
176
177
Martin Panteree3074e2016-02-23 22:30:50 +0000178.. function:: a85decode(b, *, foldspaces=False, adobe=False, ignorechars=b' \\t\\n\\r\\v')
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100179
Martin Panteree3074e2016-02-23 22:30:50 +0000180 Decode the Ascii85 encoded :term:`bytes-like object` or ASCII string *b* and
R David Murraya1986452015-12-23 21:17:17 -0500181 return the decoded :class:`bytes`.
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100182
183 *foldspaces* is a flag that specifies whether the 'y' short sequence
184 should be accepted as shorthand for 4 consecutive spaces (ASCII 0x20).
185 This feature is not supported by the "standard" Ascii85 encoding.
186
187 *adobe* controls whether the input sequence is in Adobe Ascii85 format
188 (i.e. is framed with <~ and ~>).
189
R David Murraya1986452015-12-23 21:17:17 -0500190 *ignorechars* should be a :term:`bytes-like object` or ASCII string
191 containing characters to ignore
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100192 from the input. This should only contain whitespace characters, and by
193 default contains all whitespace characters in ASCII.
194
195 .. versionadded:: 3.4
196
197
Martin Panteree3074e2016-02-23 22:30:50 +0000198.. function:: b85encode(b, pad=False)
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100199
Martin Panteree3074e2016-02-23 22:30:50 +0000200 Encode the :term:`bytes-like object` *b* using base85 (as used in e.g.
R David Murraya1986452015-12-23 21:17:17 -0500201 git-style binary diffs) and return the encoded :class:`bytes`.
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100202
R David Murraya1986452015-12-23 21:17:17 -0500203 If *pad* is true, the input is padded with ``b'\0'`` so its length is a
204 multiple of 4 bytes before encoding.
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100205
206 .. versionadded:: 3.4
207
208
209.. function:: b85decode(b)
210
R David Murraya1986452015-12-23 21:17:17 -0500211 Decode the base85-encoded :term:`bytes-like object` or ASCII string *b* and
212 return the decoded :class:`bytes`. Padding is implicitly removed, if
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100213 necessary.
214
215 .. versionadded:: 3.4
216
217
218.. note::
219 Both Base85 and Ascii85 have an expansion factor of 5 to 4 (5 Base85 or
220 Ascii85 characters can encode 4 binary bytes), while the better-known
221 Base64 has an expansion factor of 6 to 4. They are therefore more
222 efficient when space expensive. They differ by details such as the
223 character map used for encoding.
224
225
Georg Brandlb54d8012009-06-04 09:11:51 +0000226The legacy interface:
Georg Brandl116aa622007-08-15 14:28:22 +0000227
228.. function:: decode(input, output)
229
Georg Brandlb54d8012009-06-04 09:11:51 +0000230 Decode the contents of the binary *input* file and write the resulting binary
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000231 data to the *output* file. *input* and *output* must be :term:`file objects
R David Murraya1986452015-12-23 21:17:17 -0500232 <file object>`. *input* will be read until ``input.readline()`` returns an
233 empty bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +0000234
235
Georg Brandlb54d8012009-06-04 09:11:51 +0000236.. function:: decodebytes(s)
237 decodestring(s)
Georg Brandl116aa622007-08-15 14:28:22 +0000238
R David Murraya1986452015-12-23 21:17:17 -0500239 Decode the :term:`bytes-like object` *s*, which must contain one or more
240 lines of base64 encoded data, and return the decoded :class:`bytes`.
Georg Brandlb54d8012009-06-04 09:11:51 +0000241 ``decodestring`` is a deprecated alias.
Georg Brandl116aa622007-08-15 14:28:22 +0000242
R David Murray75fd2252012-08-17 20:55:21 -0400243 .. versionadded:: 3.1
244
Georg Brandl116aa622007-08-15 14:28:22 +0000245
246.. function:: encode(input, output)
247
Georg Brandlb54d8012009-06-04 09:11:51 +0000248 Encode the contents of the binary *input* file and write the resulting base64
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000249 encoded data to the *output* file. *input* and *output* must be :term:`file
250 objects <file object>`. *input* will be read until ``input.read()`` returns
R David Murraya1986452015-12-23 21:17:17 -0500251 an empty bytes object. :func:`encode` inserts a newline character (``b'\n'``)
252 after every 76 bytes of the output, as well as ensuring that the output
253 always ends with a newline, as per :rfc:`2045` (MIME).
Georg Brandl116aa622007-08-15 14:28:22 +0000254
255
Georg Brandlb54d8012009-06-04 09:11:51 +0000256.. function:: encodebytes(s)
257 encodestring(s)
Georg Brandl116aa622007-08-15 14:28:22 +0000258
R David Murraya1986452015-12-23 21:17:17 -0500259 Encode the :term:`bytes-like object` *s*, which can contain arbitrary binary
260 data, and return :class:`bytes` containing the base64-encoded data, with newlines
261 (``b'\n'``) inserted after every 76 bytes of output, and ensuring that
262 there is a trailing newline, as per :rfc:`2045` (MIME).
263
Georg Brandlb54d8012009-06-04 09:11:51 +0000264 ``encodestring`` is a deprecated alias.
265
Georg Brandl116aa622007-08-15 14:28:22 +0000266
Christian Heimesfe337bf2008-03-23 21:54:12 +0000267An example usage of the module:
Georg Brandl116aa622007-08-15 14:28:22 +0000268
269 >>> import base64
Georg Brandl134c35b2010-10-17 11:36:28 +0000270 >>> encoded = base64.b64encode(b'data to be encoded')
Georg Brandl116aa622007-08-15 14:28:22 +0000271 >>> encoded
Georg Brandl38d54f72009-01-18 10:43:58 +0000272 b'ZGF0YSB0byBiZSBlbmNvZGVk'
Georg Brandl116aa622007-08-15 14:28:22 +0000273 >>> data = base64.b64decode(encoded)
274 >>> data
Georg Brandl134c35b2010-10-17 11:36:28 +0000275 b'data to be encoded'
Georg Brandl116aa622007-08-15 14:28:22 +0000276
277
278.. seealso::
279
280 Module :mod:`binascii`
281 Support module containing ASCII-to-binary and binary-to-ASCII conversions.
282
283 :rfc:`1521` - MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies
284 Section 5.2, "Base64 Content-Transfer-Encoding," provides the definition of the
285 base64 encoding.
286