blob: ad9f5f58bee2aac1f330f612519809b01dbb5a64 [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
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04008**Source code:** :source:`Lib/base64.py`
Georg Brandl116aa622007-08-15 14:28:22 +00009
10.. index::
11 pair: base64; encoding
12 single: MIME; base64 encoding
13
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040014--------------
15
Larry Hastings3732ed22014-03-15 21:13:56 -070016This module provides functions for encoding binary data to printable
17ASCII characters and decoding such encodings back to binary data.
18It provides encoding and decoding functions for the encodings specified in
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020019:rfc:`3548`, which defines the Base16, Base32, and Base64 algorithms,
Larry Hastings3732ed22014-03-15 21:13:56 -070020and for the de-facto standard Ascii85 and Base85 encodings.
21
22The :rfc:`3548` encodings are suitable for encoding binary data so that it can
R. David Murray7cefc302010-10-17 23:12:16 +000023safely sent by email, used as parts of URLs, or included as part of an HTTP
24POST request. The encoding algorithm is not the same as the
25:program:`uuencode` program.
Georg Brandl116aa622007-08-15 14:28:22 +000026
R David Murraya1986452015-12-23 21:17:17 -050027There are two interfaces provided by this module. The modern interface
28supports encoding :term:`bytes-like objects <bytes-like object>` to ASCII
29:class:`bytes`, and decoding :term:`bytes-like objects <bytes-like object>` or
Martin Panteree3074e2016-02-23 22:30:50 +000030strings containing ASCII to :class:`bytes`. Both base-64 alphabets
31defined in :rfc:`3548` (normal, and URL- and filesystem-safe) are supported.
R David Murraya1986452015-12-23 21:17:17 -050032
33The legacy interface does not support decoding from strings, but it does
34provide functions for encoding and decoding to and from :term:`file objects
35<file object>`. It only supports the Base64 standard alphabet, and it adds
36newlines every 76 characters as per :rfc:`2045`. Note that if you are looking
37for :rfc:`2045` support you probably want to be looking at the :mod:`email`
38package instead.
39
Antoine Pitrouea6b4d52012-02-20 19:30:23 +010040
41.. versionchanged:: 3.3
42 ASCII-only Unicode strings are now accepted by the decoding functions of
43 the modern interface.
Georg Brandl116aa622007-08-15 14:28:22 +000044
Nick Coghlanfdf239a2013-10-03 00:43:22 +100045.. versionchanged:: 3.4
Serhiy Storchakae5ea1ab2016-05-18 13:54:54 +030046 Any :term:`bytes-like objects <bytes-like object>` are now accepted by all
Larry Hastings3732ed22014-03-15 21:13:56 -070047 encoding and decoding functions in this module. Ascii85/Base85 support added.
Nick Coghlanfdf239a2013-10-03 00:43:22 +100048
Georg Brandle6bcc912008-05-12 18:05:20 +000049The modern interface provides:
Georg Brandl116aa622007-08-15 14:28:22 +000050
Georg Brandlb868a662009-04-02 02:56:10 +000051.. function:: b64encode(s, altchars=None)
Georg Brandl116aa622007-08-15 14:28:22 +000052
R David Murraya1986452015-12-23 21:17:17 -050053 Encode the :term:`bytes-like object` *s* using Base64 and return the encoded
54 :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +000055
R David Murraya1986452015-12-23 21:17:17 -050056 Optional *altchars* must be a :term:`bytes-like object` of at least
Georg Brandl116aa622007-08-15 14:28:22 +000057 length 2 (additional characters are ignored) which specifies an alternative
58 alphabet for the ``+`` and ``/`` characters. This allows an application to e.g.
59 generate URL or filesystem safe Base64 strings. The default is ``None``, for
60 which the standard Base64 alphabet is used.
61
Georg Brandl116aa622007-08-15 14:28:22 +000062
R. David Murray64951362010-11-11 20:09:20 +000063.. function:: b64decode(s, altchars=None, validate=False)
Georg Brandl116aa622007-08-15 14:28:22 +000064
R David Murraya1986452015-12-23 21:17:17 -050065 Decode the Base64 encoded :term:`bytes-like object` or ASCII string
66 *s* and return the decoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +000067
R David Murraya1986452015-12-23 21:17:17 -050068 Optional *altchars* must be a :term:`bytes-like object` or ASCII string of
R. David Murray7cefc302010-10-17 23:12:16 +000069 at least length 2 (additional characters are ignored) which specifies the
70 alternative alphabet used instead of the ``+`` and ``/`` characters.
Georg Brandl116aa622007-08-15 14:28:22 +000071
R David Murraya1986452015-12-23 21:17:17 -050072 A :exc:`binascii.Error` exception is raised
Éric Araujo941afed2011-09-01 02:47:34 +020073 if *s* is incorrectly padded.
R. David Murray64951362010-11-11 20:09:20 +000074
Martin Panteree3074e2016-02-23 22:30:50 +000075 If *validate* is ``False`` (the default), characters that are neither
76 in the normal base-64 alphabet nor the alternative alphabet are
R. David Murray64951362010-11-11 20:09:20 +000077 discarded prior to the padding check. If *validate* is ``True``,
Martin Panteree3074e2016-02-23 22:30:50 +000078 these non-alphabet characters in the input result in a
R. David Murray64951362010-11-11 20:09:20 +000079 :exc:`binascii.Error`.
Georg Brandl116aa622007-08-15 14:28:22 +000080
81
82.. function:: standard_b64encode(s)
83
R David Murraya1986452015-12-23 21:17:17 -050084 Encode :term:`bytes-like object` *s* using the standard Base64 alphabet
85 and return the encoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +000086
87
88.. function:: standard_b64decode(s)
89
R David Murraya1986452015-12-23 21:17:17 -050090 Decode :term:`bytes-like object` or ASCII string *s* using the standard
91 Base64 alphabet and return the decoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +000092
93
94.. function:: urlsafe_b64encode(s)
95
Martin Panteree3074e2016-02-23 22:30:50 +000096 Encode :term:`bytes-like object` *s* using the
97 URL- and filesystem-safe alphabet, which
R David Murraya1986452015-12-23 21:17:17 -050098 substitutes ``-`` instead of ``+`` and ``_`` instead of ``/`` in the
99 standard Base64 alphabet, and return the encoded :class:`bytes`. The result
Benjamin Petersond75fcb42009-02-19 04:22:03 +0000100 can still contain ``=``.
Georg Brandl116aa622007-08-15 14:28:22 +0000101
102
103.. function:: urlsafe_b64decode(s)
104
Martin Panteree3074e2016-02-23 22:30:50 +0000105 Decode :term:`bytes-like object` or ASCII string *s*
106 using the URL- and filesystem-safe
R David Murraya1986452015-12-23 21:17:17 -0500107 alphabet, which substitutes ``-`` instead of ``+`` and ``_`` instead of
108 ``/`` in the standard Base64 alphabet, and return the decoded
109 :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +0000110
111
112.. function:: b32encode(s)
113
R David Murraya1986452015-12-23 21:17:17 -0500114 Encode the :term:`bytes-like object` *s* using Base32 and return the
115 encoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +0000116
117
Georg Brandlb868a662009-04-02 02:56:10 +0000118.. function:: b32decode(s, casefold=False, map01=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000119
R David Murraya1986452015-12-23 21:17:17 -0500120 Decode the Base32 encoded :term:`bytes-like object` or ASCII string *s* and
121 return the decoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +0000122
R David Murraya1986452015-12-23 21:17:17 -0500123 Optional *casefold* is a flag specifying
R. David Murray7cefc302010-10-17 23:12:16 +0000124 whether a lowercase alphabet is acceptable as input. For security purposes,
125 the default is ``False``.
Georg Brandl116aa622007-08-15 14:28:22 +0000126
127 :rfc:`3548` allows for optional mapping of the digit 0 (zero) to the letter O
128 (oh), and for optional mapping of the digit 1 (one) to either the letter I (eye)
129 or letter L (el). The optional argument *map01* when not ``None``, specifies
130 which letter the digit 1 should be mapped to (when *map01* is not ``None``, the
131 digit 0 is always mapped to the letter O). For security purposes the default is
132 ``None``, so that 0 and 1 are not allowed in the input.
133
R David Murraya1986452015-12-23 21:17:17 -0500134 A :exc:`binascii.Error` is raised if *s* is
Georg Brandl116aa622007-08-15 14:28:22 +0000135 incorrectly padded or if there are non-alphabet characters present in the
R David Murraya1986452015-12-23 21:17:17 -0500136 input.
Georg Brandl116aa622007-08-15 14:28:22 +0000137
138
139.. function:: b16encode(s)
140
R David Murraya1986452015-12-23 21:17:17 -0500141 Encode the :term:`bytes-like object` *s* using Base16 and return the
142 encoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +0000143
144
Georg Brandlb868a662009-04-02 02:56:10 +0000145.. function:: b16decode(s, casefold=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000146
R David Murraya1986452015-12-23 21:17:17 -0500147 Decode the Base16 encoded :term:`bytes-like object` or ASCII string *s* and
148 return the decoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +0000149
R David Murraya1986452015-12-23 21:17:17 -0500150 Optional *casefold* is a flag specifying whether a
Georg Brandl116aa622007-08-15 14:28:22 +0000151 lowercase alphabet is acceptable as input. For security purposes, the default
152 is ``False``.
153
Martin Panteree3074e2016-02-23 22:30:50 +0000154 A :exc:`binascii.Error` is raised if *s* is
Georg Brandl116aa622007-08-15 14:28:22 +0000155 incorrectly padded or if there are non-alphabet characters present in the
R David Murraya1986452015-12-23 21:17:17 -0500156 input.
Georg Brandl116aa622007-08-15 14:28:22 +0000157
Georg Brandl116aa622007-08-15 14:28:22 +0000158
Martin Panteree3074e2016-02-23 22:30:50 +0000159.. function:: a85encode(b, *, foldspaces=False, wrapcol=0, pad=False, adobe=False)
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100160
Martin Panteree3074e2016-02-23 22:30:50 +0000161 Encode the :term:`bytes-like object` *b* using Ascii85 and return the
R David Murraya1986452015-12-23 21:17:17 -0500162 encoded :class:`bytes`.
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100163
164 *foldspaces* is an optional flag that uses the special short sequence 'y'
165 instead of 4 consecutive spaces (ASCII 0x20) as supported by 'btoa'. This
166 feature is not supported by the "standard" Ascii85 encoding.
167
R David Murraya1986452015-12-23 21:17:17 -0500168 *wrapcol* controls whether the output should have newline (``b'\n'``)
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100169 characters added to it. If this is non-zero, each output line will be
170 at most this many characters long.
171
R David Murraya1986452015-12-23 21:17:17 -0500172 *pad* controls whether the input is padded to a multiple of 4
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100173 before encoding. Note that the ``btoa`` implementation always pads.
174
175 *adobe* controls whether the encoded byte sequence is framed with ``<~``
176 and ``~>``, which is used by the Adobe implementation.
177
178 .. versionadded:: 3.4
179
180
Martin Panteree3074e2016-02-23 22:30:50 +0000181.. function:: a85decode(b, *, foldspaces=False, adobe=False, ignorechars=b' \\t\\n\\r\\v')
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100182
Martin Panteree3074e2016-02-23 22:30:50 +0000183 Decode the Ascii85 encoded :term:`bytes-like object` or ASCII string *b* and
R David Murraya1986452015-12-23 21:17:17 -0500184 return the decoded :class:`bytes`.
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100185
186 *foldspaces* is a flag that specifies whether the 'y' short sequence
187 should be accepted as shorthand for 4 consecutive spaces (ASCII 0x20).
188 This feature is not supported by the "standard" Ascii85 encoding.
189
190 *adobe* controls whether the input sequence is in Adobe Ascii85 format
191 (i.e. is framed with <~ and ~>).
192
R David Murraya1986452015-12-23 21:17:17 -0500193 *ignorechars* should be a :term:`bytes-like object` or ASCII string
194 containing characters to ignore
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100195 from the input. This should only contain whitespace characters, and by
196 default contains all whitespace characters in ASCII.
197
198 .. versionadded:: 3.4
199
200
Martin Panteree3074e2016-02-23 22:30:50 +0000201.. function:: b85encode(b, pad=False)
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100202
Martin Panteree3074e2016-02-23 22:30:50 +0000203 Encode the :term:`bytes-like object` *b* using base85 (as used in e.g.
R David Murraya1986452015-12-23 21:17:17 -0500204 git-style binary diffs) and return the encoded :class:`bytes`.
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100205
R David Murraya1986452015-12-23 21:17:17 -0500206 If *pad* is true, the input is padded with ``b'\0'`` so its length is a
207 multiple of 4 bytes before encoding.
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100208
209 .. versionadded:: 3.4
210
211
212.. function:: b85decode(b)
213
R David Murraya1986452015-12-23 21:17:17 -0500214 Decode the base85-encoded :term:`bytes-like object` or ASCII string *b* and
215 return the decoded :class:`bytes`. Padding is implicitly removed, if
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100216 necessary.
217
218 .. versionadded:: 3.4
219
220
Georg Brandlb54d8012009-06-04 09:11:51 +0000221The legacy interface:
Georg Brandl116aa622007-08-15 14:28:22 +0000222
223.. function:: decode(input, output)
224
Georg Brandlb54d8012009-06-04 09:11:51 +0000225 Decode the contents of the binary *input* file and write the resulting binary
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000226 data to the *output* file. *input* and *output* must be :term:`file objects
R David Murraya1986452015-12-23 21:17:17 -0500227 <file object>`. *input* will be read until ``input.readline()`` returns an
228 empty bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +0000229
230
Georg Brandlb54d8012009-06-04 09:11:51 +0000231.. function:: decodebytes(s)
Georg Brandl116aa622007-08-15 14:28:22 +0000232
R David Murraya1986452015-12-23 21:17:17 -0500233 Decode the :term:`bytes-like object` *s*, which must contain one or more
234 lines of base64 encoded data, and return the decoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +0000235
R David Murray75fd2252012-08-17 20:55:21 -0400236 .. versionadded:: 3.1
237
Matthias Bussonnierc643a962017-03-02 06:21:26 -0800238.. function:: decodestring(s)
239
240 Deprecated alias of :func:`decodebytes`.
241
242 .. deprecated:: 3.1
243
Georg Brandl116aa622007-08-15 14:28:22 +0000244
245.. function:: encode(input, output)
246
Georg Brandlb54d8012009-06-04 09:11:51 +0000247 Encode the contents of the binary *input* file and write the resulting base64
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000248 encoded data to the *output* file. *input* and *output* must be :term:`file
249 objects <file object>`. *input* will be read until ``input.read()`` returns
R David Murraya1986452015-12-23 21:17:17 -0500250 an empty bytes object. :func:`encode` inserts a newline character (``b'\n'``)
251 after every 76 bytes of the output, as well as ensuring that the output
252 always ends with a newline, as per :rfc:`2045` (MIME).
Georg Brandl116aa622007-08-15 14:28:22 +0000253
254
Georg Brandlb54d8012009-06-04 09:11:51 +0000255.. function:: encodebytes(s)
Georg Brandl116aa622007-08-15 14:28:22 +0000256
R David Murraya1986452015-12-23 21:17:17 -0500257 Encode the :term:`bytes-like object` *s*, which can contain arbitrary binary
258 data, and return :class:`bytes` containing the base64-encoded data, with newlines
259 (``b'\n'``) inserted after every 76 bytes of output, and ensuring that
260 there is a trailing newline, as per :rfc:`2045` (MIME).
261
Matthias Bussonnierc643a962017-03-02 06:21:26 -0800262 .. versionadded:: 3.1
263
264.. function:: encodestring(s)
265
266 Deprecated alias of :func:`encodebytes`.
267
268 .. deprecated:: 3.1
Georg Brandlb54d8012009-06-04 09:11:51 +0000269
Georg Brandl116aa622007-08-15 14:28:22 +0000270
Christian Heimesfe337bf2008-03-23 21:54:12 +0000271An example usage of the module:
Georg Brandl116aa622007-08-15 14:28:22 +0000272
273 >>> import base64
Georg Brandl134c35b2010-10-17 11:36:28 +0000274 >>> encoded = base64.b64encode(b'data to be encoded')
Georg Brandl116aa622007-08-15 14:28:22 +0000275 >>> encoded
Georg Brandl38d54f72009-01-18 10:43:58 +0000276 b'ZGF0YSB0byBiZSBlbmNvZGVk'
Georg Brandl116aa622007-08-15 14:28:22 +0000277 >>> data = base64.b64decode(encoded)
278 >>> data
Georg Brandl134c35b2010-10-17 11:36:28 +0000279 b'data to be encoded'
Georg Brandl116aa622007-08-15 14:28:22 +0000280
281
282.. seealso::
283
284 Module :mod:`binascii`
285 Support module containing ASCII-to-binary and binary-to-ASCII conversions.
286
287 :rfc:`1521` - MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies
288 Section 5.2, "Base64 Content-Transfer-Encoding," provides the definition of the
289 base64 encoding.
290