blob: 2f24bb63912fb6bc7f931aac84c55fa406b3e453 [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
Filipe Laíns4ce6faa2020-08-10 15:48:20 +0100127 :rfc:`4648` allows for optional mapping of the digit 0 (zero) to the letter O
Georg Brandl116aa622007-08-15 14:28:22 +0000128 (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
Filipe Laíns4ce6faa2020-08-10 15:48:20 +0100139.. function:: b32hexencode(s)
140
141 Similar to :func:`b32encode` but uses the Extended Hex Alphabet, as defined in
142 :rfc:`4648`.
143
144 .. versionadded:: 3.10
145
146
147.. function:: b32hexdecode(s, casefold=False)
148
149 Similar to :func:`b32decode` but uses the Extended Hex Alphabet, as defined in
150 :rfc:`4648`.
151
152 This version does not allow the digit 0 (zero) to the letter O (oh) and digit
153 1 (one) to either the letter I (eye) or letter L (el) mappings, all these
154 characters are included in the Extended Hex Alphabet and are not
155 interchangable.
156
157 .. versionadded:: 3.10
158
159
Georg Brandl116aa622007-08-15 14:28:22 +0000160.. function:: b16encode(s)
161
R David Murraya1986452015-12-23 21:17:17 -0500162 Encode the :term:`bytes-like object` *s* using Base16 and return the
163 encoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +0000164
165
Georg Brandlb868a662009-04-02 02:56:10 +0000166.. function:: b16decode(s, casefold=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000167
R David Murraya1986452015-12-23 21:17:17 -0500168 Decode the Base16 encoded :term:`bytes-like object` or ASCII string *s* and
169 return the decoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +0000170
R David Murraya1986452015-12-23 21:17:17 -0500171 Optional *casefold* is a flag specifying whether a
Georg Brandl116aa622007-08-15 14:28:22 +0000172 lowercase alphabet is acceptable as input. For security purposes, the default
173 is ``False``.
174
Martin Panteree3074e2016-02-23 22:30:50 +0000175 A :exc:`binascii.Error` is raised if *s* is
Georg Brandl116aa622007-08-15 14:28:22 +0000176 incorrectly padded or if there are non-alphabet characters present in the
R David Murraya1986452015-12-23 21:17:17 -0500177 input.
Georg Brandl116aa622007-08-15 14:28:22 +0000178
Georg Brandl116aa622007-08-15 14:28:22 +0000179
Martin Panteree3074e2016-02-23 22:30:50 +0000180.. function:: a85encode(b, *, foldspaces=False, wrapcol=0, pad=False, adobe=False)
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100181
Martin Panteree3074e2016-02-23 22:30:50 +0000182 Encode the :term:`bytes-like object` *b* using Ascii85 and return the
R David Murraya1986452015-12-23 21:17:17 -0500183 encoded :class:`bytes`.
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100184
185 *foldspaces* is an optional flag that uses the special short sequence 'y'
186 instead of 4 consecutive spaces (ASCII 0x20) as supported by 'btoa'. This
187 feature is not supported by the "standard" Ascii85 encoding.
188
R David Murraya1986452015-12-23 21:17:17 -0500189 *wrapcol* controls whether the output should have newline (``b'\n'``)
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100190 characters added to it. If this is non-zero, each output line will be
191 at most this many characters long.
192
R David Murraya1986452015-12-23 21:17:17 -0500193 *pad* controls whether the input is padded to a multiple of 4
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100194 before encoding. Note that the ``btoa`` implementation always pads.
195
196 *adobe* controls whether the encoded byte sequence is framed with ``<~``
197 and ``~>``, which is used by the Adobe implementation.
198
199 .. versionadded:: 3.4
200
201
Martin Panteree3074e2016-02-23 22:30:50 +0000202.. function:: a85decode(b, *, foldspaces=False, adobe=False, ignorechars=b' \\t\\n\\r\\v')
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100203
Martin Panteree3074e2016-02-23 22:30:50 +0000204 Decode the Ascii85 encoded :term:`bytes-like object` or ASCII string *b* and
R David Murraya1986452015-12-23 21:17:17 -0500205 return the decoded :class:`bytes`.
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100206
207 *foldspaces* is a flag that specifies whether the 'y' short sequence
208 should be accepted as shorthand for 4 consecutive spaces (ASCII 0x20).
209 This feature is not supported by the "standard" Ascii85 encoding.
210
211 *adobe* controls whether the input sequence is in Adobe Ascii85 format
212 (i.e. is framed with <~ and ~>).
213
R David Murraya1986452015-12-23 21:17:17 -0500214 *ignorechars* should be a :term:`bytes-like object` or ASCII string
215 containing characters to ignore
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100216 from the input. This should only contain whitespace characters, and by
217 default contains all whitespace characters in ASCII.
218
219 .. versionadded:: 3.4
220
221
Martin Panteree3074e2016-02-23 22:30:50 +0000222.. function:: b85encode(b, pad=False)
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100223
Martin Panteree3074e2016-02-23 22:30:50 +0000224 Encode the :term:`bytes-like object` *b* using base85 (as used in e.g.
R David Murraya1986452015-12-23 21:17:17 -0500225 git-style binary diffs) and return the encoded :class:`bytes`.
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100226
R David Murraya1986452015-12-23 21:17:17 -0500227 If *pad* is true, the input is padded with ``b'\0'`` so its length is a
228 multiple of 4 bytes before encoding.
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100229
230 .. versionadded:: 3.4
231
232
233.. function:: b85decode(b)
234
R David Murraya1986452015-12-23 21:17:17 -0500235 Decode the base85-encoded :term:`bytes-like object` or ASCII string *b* and
236 return the decoded :class:`bytes`. Padding is implicitly removed, if
Antoine Pitrou6dd0d462013-11-17 23:52:25 +0100237 necessary.
238
239 .. versionadded:: 3.4
240
241
Georg Brandlb54d8012009-06-04 09:11:51 +0000242The legacy interface:
Georg Brandl116aa622007-08-15 14:28:22 +0000243
244.. function:: decode(input, output)
245
Georg Brandlb54d8012009-06-04 09:11:51 +0000246 Decode the contents of the binary *input* file and write the resulting binary
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000247 data to the *output* file. *input* and *output* must be :term:`file objects
R David Murraya1986452015-12-23 21:17:17 -0500248 <file object>`. *input* will be read until ``input.readline()`` returns an
249 empty bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +0000250
251
Georg Brandlb54d8012009-06-04 09:11:51 +0000252.. function:: decodebytes(s)
Georg Brandl116aa622007-08-15 14:28:22 +0000253
R David Murraya1986452015-12-23 21:17:17 -0500254 Decode the :term:`bytes-like object` *s*, which must contain one or more
255 lines of base64 encoded data, and return the decoded :class:`bytes`.
Georg Brandl116aa622007-08-15 14:28:22 +0000256
R David Murray75fd2252012-08-17 20:55:21 -0400257 .. versionadded:: 3.1
258
Georg Brandl116aa622007-08-15 14:28:22 +0000259
260.. function:: encode(input, output)
261
Georg Brandlb54d8012009-06-04 09:11:51 +0000262 Encode the contents of the binary *input* file and write the resulting base64
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000263 encoded data to the *output* file. *input* and *output* must be :term:`file
264 objects <file object>`. *input* will be read until ``input.read()`` returns
R David Murraya1986452015-12-23 21:17:17 -0500265 an empty bytes object. :func:`encode` inserts a newline character (``b'\n'``)
266 after every 76 bytes of the output, as well as ensuring that the output
267 always ends with a newline, as per :rfc:`2045` (MIME).
Georg Brandl116aa622007-08-15 14:28:22 +0000268
269
Georg Brandlb54d8012009-06-04 09:11:51 +0000270.. function:: encodebytes(s)
Georg Brandl116aa622007-08-15 14:28:22 +0000271
R David Murraya1986452015-12-23 21:17:17 -0500272 Encode the :term:`bytes-like object` *s*, which can contain arbitrary binary
273 data, and return :class:`bytes` containing the base64-encoded data, with newlines
274 (``b'\n'``) inserted after every 76 bytes of output, and ensuring that
275 there is a trailing newline, as per :rfc:`2045` (MIME).
276
Matthias Bussonnierc643a962017-03-02 06:21:26 -0800277 .. versionadded:: 3.1
278
Georg Brandl116aa622007-08-15 14:28:22 +0000279
Christian Heimesfe337bf2008-03-23 21:54:12 +0000280An example usage of the module:
Georg Brandl116aa622007-08-15 14:28:22 +0000281
282 >>> import base64
Georg Brandl134c35b2010-10-17 11:36:28 +0000283 >>> encoded = base64.b64encode(b'data to be encoded')
Georg Brandl116aa622007-08-15 14:28:22 +0000284 >>> encoded
Georg Brandl38d54f72009-01-18 10:43:58 +0000285 b'ZGF0YSB0byBiZSBlbmNvZGVk'
Georg Brandl116aa622007-08-15 14:28:22 +0000286 >>> data = base64.b64decode(encoded)
287 >>> data
Georg Brandl134c35b2010-10-17 11:36:28 +0000288 b'data to be encoded'
Georg Brandl116aa622007-08-15 14:28:22 +0000289
290
291.. seealso::
292
293 Module :mod:`binascii`
294 Support module containing ASCII-to-binary and binary-to-ASCII conversions.
295
296 :rfc:`1521` - MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies
297 Section 5.2, "Base64 Content-Transfer-Encoding," provides the definition of the
298 base64 encoding.
299