blob: de8744137c0e6e75a456a851fc7d5958c8ec6e3a [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`base64` --- RFC 3548: Base16, Base32, Base64 Data Encodings
2=================================================================
3
4.. module:: base64
5 :synopsis: RFC 3548: Base16, Base32, Base64 Data Encodings
6
7
8.. index::
9 pair: base64; encoding
10 single: MIME; base64 encoding
11
12This module provides data encoding and decoding as specified in :rfc:`3548`.
R. David Murray7cefc302010-10-17 23:12:16 +000013This standard defines the Base16, Base32, and Base64 algorithms for encoding
14and decoding arbitrary binary strings into ASCII-only byte strings that can be
15safely sent by email, used as parts of URLs, or included as part of an HTTP
16POST request. The encoding algorithm is not the same as the
17:program:`uuencode` program.
Georg Brandl116aa622007-08-15 14:28:22 +000018
R. David Murray7cefc302010-10-17 23:12:16 +000019There are two interfaces provided by this module. The modern interface
20supports encoding and decoding ASCII byte string objects using all three
Antoine Pitrouea6b4d52012-02-20 19:30:23 +010021alphabets. Additionally, the decoding functions of the modern interface also
22accept Unicode strings containing only ASCII characters. The legacy interface
23provides for encoding and decoding to and from file-like objects as well as
24byte strings, but only using the Base64 standard alphabet.
25
26.. versionchanged:: 3.3
27 ASCII-only Unicode strings are now accepted by the decoding functions of
28 the modern interface.
Georg Brandl116aa622007-08-15 14:28:22 +000029
Nick Coghlanfdf239a2013-10-03 00:43:22 +100030.. versionchanged:: 3.4
31 Any :term:`bytes-like object`\ s are now accepted by all
32 encoding and decoding functions in this module.
33
Georg Brandle6bcc912008-05-12 18:05:20 +000034The modern interface provides:
Georg Brandl116aa622007-08-15 14:28:22 +000035
Georg Brandlb868a662009-04-02 02:56:10 +000036.. function:: b64encode(s, altchars=None)
Georg Brandl116aa622007-08-15 14:28:22 +000037
R. David Murray7cefc302010-10-17 23:12:16 +000038 Encode a byte string using Base64.
Georg Brandl116aa622007-08-15 14:28:22 +000039
40 *s* is the string to encode. Optional *altchars* must be a string of at least
41 length 2 (additional characters are ignored) which specifies an alternative
42 alphabet for the ``+`` and ``/`` characters. This allows an application to e.g.
43 generate URL or filesystem safe Base64 strings. The default is ``None``, for
44 which the standard Base64 alphabet is used.
45
Georg Brandl62e42312010-08-02 20:39:35 +000046 The encoded byte string is returned.
Georg Brandl116aa622007-08-15 14:28:22 +000047
48
R. David Murray64951362010-11-11 20:09:20 +000049.. function:: b64decode(s, altchars=None, validate=False)
Georg Brandl116aa622007-08-15 14:28:22 +000050
Georg Brandl62e42312010-08-02 20:39:35 +000051 Decode a Base64 encoded byte string.
Georg Brandl116aa622007-08-15 14:28:22 +000052
R. David Murray7cefc302010-10-17 23:12:16 +000053 *s* is the byte string to decode. Optional *altchars* must be a string of
54 at least length 2 (additional characters are ignored) which specifies the
55 alternative alphabet used instead of the ``+`` and ``/`` characters.
Georg Brandl116aa622007-08-15 14:28:22 +000056
Éric Araujo941afed2011-09-01 02:47:34 +020057 The decoded string is returned. A :exc:`binascii.Error` exception is raised
58 if *s* is incorrectly padded.
R. David Murray64951362010-11-11 20:09:20 +000059
60 If *validate* is ``False`` (the default), non-base64-alphabet characters are
61 discarded prior to the padding check. If *validate* is ``True``,
62 non-base64-alphabet characters in the input result in a
63 :exc:`binascii.Error`.
Georg Brandl116aa622007-08-15 14:28:22 +000064
65
66.. function:: standard_b64encode(s)
67
Georg Brandl62e42312010-08-02 20:39:35 +000068 Encode byte string *s* using the standard Base64 alphabet.
Georg Brandl116aa622007-08-15 14:28:22 +000069
70
71.. function:: standard_b64decode(s)
72
Georg Brandl62e42312010-08-02 20:39:35 +000073 Decode byte string *s* using the standard Base64 alphabet.
Georg Brandl116aa622007-08-15 14:28:22 +000074
75
76.. function:: urlsafe_b64encode(s)
77
Georg Brandl62e42312010-08-02 20:39:35 +000078 Encode byte string *s* using a URL-safe alphabet, which substitutes ``-`` instead of
Benjamin Petersond75fcb42009-02-19 04:22:03 +000079 ``+`` and ``_`` instead of ``/`` in the standard Base64 alphabet. The result
80 can still contain ``=``.
Georg Brandl116aa622007-08-15 14:28:22 +000081
82
83.. function:: urlsafe_b64decode(s)
84
Georg Brandl62e42312010-08-02 20:39:35 +000085 Decode byte string *s* using a URL-safe alphabet, which substitutes ``-`` instead of
Georg Brandl116aa622007-08-15 14:28:22 +000086 ``+`` and ``_`` instead of ``/`` in the standard Base64 alphabet.
87
88
89.. function:: b32encode(s)
90
Georg Brandl62e42312010-08-02 20:39:35 +000091 Encode a byte string using Base32. *s* is the string to encode. The encoded string
Georg Brandl116aa622007-08-15 14:28:22 +000092 is returned.
93
94
Georg Brandlb868a662009-04-02 02:56:10 +000095.. function:: b32decode(s, casefold=False, map01=None)
Georg Brandl116aa622007-08-15 14:28:22 +000096
Georg Brandl62e42312010-08-02 20:39:35 +000097 Decode a Base32 encoded byte string.
Georg Brandl116aa622007-08-15 14:28:22 +000098
R. David Murray7cefc302010-10-17 23:12:16 +000099 *s* is the byte string to decode. Optional *casefold* is a flag specifying
100 whether a lowercase alphabet is acceptable as input. For security purposes,
101 the default is ``False``.
Georg Brandl116aa622007-08-15 14:28:22 +0000102
103 :rfc:`3548` allows for optional mapping of the digit 0 (zero) to the letter O
104 (oh), and for optional mapping of the digit 1 (one) to either the letter I (eye)
105 or letter L (el). The optional argument *map01* when not ``None``, specifies
106 which letter the digit 1 should be mapped to (when *map01* is not ``None``, the
107 digit 0 is always mapped to the letter O). For security purposes the default is
108 ``None``, so that 0 and 1 are not allowed in the input.
109
Serhiy Storchakaea2b4902013-05-28 15:27:29 +0300110 The decoded byte string is returned. A :exc:`binascii.Error` is raised if *s* were
Georg Brandl116aa622007-08-15 14:28:22 +0000111 incorrectly padded or if there are non-alphabet characters present in the
112 string.
113
114
115.. function:: b16encode(s)
116
Georg Brandl62e42312010-08-02 20:39:35 +0000117 Encode a byte string using Base16.
Georg Brandl116aa622007-08-15 14:28:22 +0000118
Georg Brandl62e42312010-08-02 20:39:35 +0000119 *s* is the string to encode. The encoded byte string is returned.
Georg Brandl116aa622007-08-15 14:28:22 +0000120
121
Georg Brandlb868a662009-04-02 02:56:10 +0000122.. function:: b16decode(s, casefold=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000123
Georg Brandl62e42312010-08-02 20:39:35 +0000124 Decode a Base16 encoded byte string.
Georg Brandl116aa622007-08-15 14:28:22 +0000125
126 *s* is the string to decode. Optional *casefold* is a flag specifying whether a
127 lowercase alphabet is acceptable as input. For security purposes, the default
128 is ``False``.
129
Georg Brandl62e42312010-08-02 20:39:35 +0000130 The decoded byte string is returned. A :exc:`TypeError` is raised if *s* were
Georg Brandl116aa622007-08-15 14:28:22 +0000131 incorrectly padded or if there are non-alphabet characters present in the
132 string.
133
Georg Brandl116aa622007-08-15 14:28:22 +0000134
Georg Brandlb54d8012009-06-04 09:11:51 +0000135The legacy interface:
Georg Brandl116aa622007-08-15 14:28:22 +0000136
137.. function:: decode(input, output)
138
Georg Brandlb54d8012009-06-04 09:11:51 +0000139 Decode the contents of the binary *input* file and write the resulting binary
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000140 data to the *output* file. *input* and *output* must be :term:`file objects
141 <file object>`. *input* will be read until ``input.read()`` returns an empty
142 bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +0000143
144
Georg Brandlb54d8012009-06-04 09:11:51 +0000145.. function:: decodebytes(s)
146 decodestring(s)
Georg Brandl116aa622007-08-15 14:28:22 +0000147
R. David Murray7cefc302010-10-17 23:12:16 +0000148 Decode the byte string *s*, which must contain one or more lines of base64
149 encoded data, and return a byte string containing the resulting binary data.
Georg Brandlb54d8012009-06-04 09:11:51 +0000150 ``decodestring`` is a deprecated alias.
Georg Brandl116aa622007-08-15 14:28:22 +0000151
R David Murray75fd2252012-08-17 20:55:21 -0400152 .. versionadded:: 3.1
153
Georg Brandl116aa622007-08-15 14:28:22 +0000154
155.. function:: encode(input, output)
156
Georg Brandlb54d8012009-06-04 09:11:51 +0000157 Encode the contents of the binary *input* file and write the resulting base64
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000158 encoded data to the *output* file. *input* and *output* must be :term:`file
159 objects <file object>`. *input* will be read until ``input.read()`` returns
160 an empty bytes object. :func:`encode` returns the encoded data plus a trailing
161 newline character (``b'\n'``).
Georg Brandl116aa622007-08-15 14:28:22 +0000162
163
Georg Brandlb54d8012009-06-04 09:11:51 +0000164.. function:: encodebytes(s)
165 encodestring(s)
Georg Brandl116aa622007-08-15 14:28:22 +0000166
R. David Murray7cefc302010-10-17 23:12:16 +0000167 Encode the byte string *s*, which can contain arbitrary binary data, and
168 return a byte string containing one or more lines of base64-encoded data.
Georg Brandlb54d8012009-06-04 09:11:51 +0000169 :func:`encodebytes` returns a string containing one or more lines of
170 base64-encoded data always including an extra trailing newline (``b'\n'``).
171 ``encodestring`` is a deprecated alias.
172
Georg Brandl116aa622007-08-15 14:28:22 +0000173
Christian Heimesfe337bf2008-03-23 21:54:12 +0000174An example usage of the module:
Georg Brandl116aa622007-08-15 14:28:22 +0000175
176 >>> import base64
Georg Brandl134c35b2010-10-17 11:36:28 +0000177 >>> encoded = base64.b64encode(b'data to be encoded')
Georg Brandl116aa622007-08-15 14:28:22 +0000178 >>> encoded
Georg Brandl38d54f72009-01-18 10:43:58 +0000179 b'ZGF0YSB0byBiZSBlbmNvZGVk'
Georg Brandl116aa622007-08-15 14:28:22 +0000180 >>> data = base64.b64decode(encoded)
181 >>> data
Georg Brandl134c35b2010-10-17 11:36:28 +0000182 b'data to be encoded'
Georg Brandl116aa622007-08-15 14:28:22 +0000183
184
185.. seealso::
186
187 Module :mod:`binascii`
188 Support module containing ASCII-to-binary and binary-to-ASCII conversions.
189
190 :rfc:`1521` - MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies
191 Section 5.2, "Base64 Content-Transfer-Encoding," provides the definition of the
192 base64 encoding.
193