blob: 0802e6c53c6c715d38b3abc6874e2f628c2381d5 [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`.
13This standard defines the Base16, Base32, and Base64 algorithms for encoding and
14decoding arbitrary binary strings into text strings that can be safely sent by
15email, used as parts of URLs, or included as part of an HTTP POST request. The
16encoding algorithm is not the same as the :program:`uuencode` program.
17
18There are two interfaces provided by this module. The modern interface supports
19encoding and decoding string objects using all three alphabets. The legacy
20interface provides for encoding and decoding to and from file-like objects as
21well as strings, but only using the Base64 standard alphabet.
22
Georg Brandle6bcc912008-05-12 18:05:20 +000023The modern interface provides:
Georg Brandl116aa622007-08-15 14:28:22 +000024
Georg Brandlb868a662009-04-02 02:56:10 +000025.. function:: b64encode(s, altchars=None)
Georg Brandl116aa622007-08-15 14:28:22 +000026
Georg Brandl62e42312010-08-02 20:39:35 +000027 Encode a byte string use Base64.
Georg Brandl116aa622007-08-15 14:28:22 +000028
29 *s* is the string to encode. Optional *altchars* must be a string of at least
30 length 2 (additional characters are ignored) which specifies an alternative
31 alphabet for the ``+`` and ``/`` characters. This allows an application to e.g.
32 generate URL or filesystem safe Base64 strings. The default is ``None``, for
33 which the standard Base64 alphabet is used.
34
Georg Brandl62e42312010-08-02 20:39:35 +000035 The encoded byte string is returned.
Georg Brandl116aa622007-08-15 14:28:22 +000036
37
Georg Brandlb868a662009-04-02 02:56:10 +000038.. function:: b64decode(s, altchars=None)
Georg Brandl116aa622007-08-15 14:28:22 +000039
Georg Brandl62e42312010-08-02 20:39:35 +000040 Decode a Base64 encoded byte string.
Georg Brandl116aa622007-08-15 14:28:22 +000041
42 *s* is the string to decode. Optional *altchars* must be a string of at least
43 length 2 (additional characters are ignored) which specifies the alternative
44 alphabet used instead of the ``+`` and ``/`` characters.
45
Georg Brandl62e42312010-08-02 20:39:35 +000046 The decoded byte string is returned. A :exc:`TypeError` is raised if *s* were
Georg Brandl116aa622007-08-15 14:28:22 +000047 incorrectly padded or if there are non-alphabet characters present in the
48 string.
49
50
51.. function:: standard_b64encode(s)
52
Georg Brandl62e42312010-08-02 20:39:35 +000053 Encode byte string *s* using the standard Base64 alphabet.
Georg Brandl116aa622007-08-15 14:28:22 +000054
55
56.. function:: standard_b64decode(s)
57
Georg Brandl62e42312010-08-02 20:39:35 +000058 Decode byte string *s* using the standard Base64 alphabet.
Georg Brandl116aa622007-08-15 14:28:22 +000059
60
61.. function:: urlsafe_b64encode(s)
62
Georg Brandl62e42312010-08-02 20:39:35 +000063 Encode byte string *s* using a URL-safe alphabet, which substitutes ``-`` instead of
Benjamin Petersond75fcb42009-02-19 04:22:03 +000064 ``+`` and ``_`` instead of ``/`` in the standard Base64 alphabet. The result
65 can still contain ``=``.
Georg Brandl116aa622007-08-15 14:28:22 +000066
67
68.. function:: urlsafe_b64decode(s)
69
Georg Brandl62e42312010-08-02 20:39:35 +000070 Decode byte string *s* using a URL-safe alphabet, which substitutes ``-`` instead of
Georg Brandl116aa622007-08-15 14:28:22 +000071 ``+`` and ``_`` instead of ``/`` in the standard Base64 alphabet.
72
73
74.. function:: b32encode(s)
75
Georg Brandl62e42312010-08-02 20:39:35 +000076 Encode a byte string using Base32. *s* is the string to encode. The encoded string
Georg Brandl116aa622007-08-15 14:28:22 +000077 is returned.
78
79
Georg Brandlb868a662009-04-02 02:56:10 +000080.. function:: b32decode(s, casefold=False, map01=None)
Georg Brandl116aa622007-08-15 14:28:22 +000081
Georg Brandl62e42312010-08-02 20:39:35 +000082 Decode a Base32 encoded byte string.
Georg Brandl116aa622007-08-15 14:28:22 +000083
84 *s* is the string to decode. Optional *casefold* is a flag specifying whether a
85 lowercase alphabet is acceptable as input. For security purposes, the default
86 is ``False``.
87
88 :rfc:`3548` allows for optional mapping of the digit 0 (zero) to the letter O
89 (oh), and for optional mapping of the digit 1 (one) to either the letter I (eye)
90 or letter L (el). The optional argument *map01* when not ``None``, specifies
91 which letter the digit 1 should be mapped to (when *map01* is not ``None``, the
92 digit 0 is always mapped to the letter O). For security purposes the default is
93 ``None``, so that 0 and 1 are not allowed in the input.
94
Georg Brandl62e42312010-08-02 20:39:35 +000095 The decoded byte string is returned. A :exc:`TypeError` is raised if *s* were
Georg Brandl116aa622007-08-15 14:28:22 +000096 incorrectly padded or if there are non-alphabet characters present in the
97 string.
98
99
100.. function:: b16encode(s)
101
Georg Brandl62e42312010-08-02 20:39:35 +0000102 Encode a byte string using Base16.
Georg Brandl116aa622007-08-15 14:28:22 +0000103
Georg Brandl62e42312010-08-02 20:39:35 +0000104 *s* is the string to encode. The encoded byte string is returned.
Georg Brandl116aa622007-08-15 14:28:22 +0000105
106
Georg Brandlb868a662009-04-02 02:56:10 +0000107.. function:: b16decode(s, casefold=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000108
Georg Brandl62e42312010-08-02 20:39:35 +0000109 Decode a Base16 encoded byte string.
Georg Brandl116aa622007-08-15 14:28:22 +0000110
111 *s* is the string to decode. Optional *casefold* is a flag specifying whether a
112 lowercase alphabet is acceptable as input. For security purposes, the default
113 is ``False``.
114
Georg Brandl62e42312010-08-02 20:39:35 +0000115 The decoded byte string is returned. A :exc:`TypeError` is raised if *s* were
Georg Brandl116aa622007-08-15 14:28:22 +0000116 incorrectly padded or if there are non-alphabet characters present in the
117 string.
118
Georg Brandl116aa622007-08-15 14:28:22 +0000119
Georg Brandlb54d8012009-06-04 09:11:51 +0000120The legacy interface:
Georg Brandl116aa622007-08-15 14:28:22 +0000121
122.. function:: decode(input, output)
123
Georg Brandlb54d8012009-06-04 09:11:51 +0000124 Decode the contents of the binary *input* file and write the resulting binary
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000125 data to the *output* file. *input* and *output* must be :term:`file objects
126 <file object>`. *input* will be read until ``input.read()`` returns an empty
127 bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +0000128
129
Georg Brandlb54d8012009-06-04 09:11:51 +0000130.. function:: decodebytes(s)
131 decodestring(s)
Georg Brandl116aa622007-08-15 14:28:22 +0000132
Georg Brandlb54d8012009-06-04 09:11:51 +0000133 Decode the bytestring *s*, which must contain one or more lines of base64
134 encoded data, and return a bytestring containing the resulting binary data.
135 ``decodestring`` is a deprecated alias.
Georg Brandl116aa622007-08-15 14:28:22 +0000136
137
138.. function:: encode(input, output)
139
Georg Brandlb54d8012009-06-04 09:11:51 +0000140 Encode the contents of the binary *input* file and write the resulting base64
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000141 encoded data to the *output* file. *input* and *output* must be :term:`file
142 objects <file object>`. *input* will be read until ``input.read()`` returns
143 an empty bytes object. :func:`encode` returns the encoded data plus a trailing
144 newline character (``b'\n'``).
Georg Brandl116aa622007-08-15 14:28:22 +0000145
146
Georg Brandlb54d8012009-06-04 09:11:51 +0000147.. function:: encodebytes(s)
148 encodestring(s)
Georg Brandl116aa622007-08-15 14:28:22 +0000149
Georg Brandlb54d8012009-06-04 09:11:51 +0000150 Encode the bytestring *s*, which can contain arbitrary binary data, and
151 return a bytestring containing one or more lines of base64-encoded data.
152 :func:`encodebytes` returns a string containing one or more lines of
153 base64-encoded data always including an extra trailing newline (``b'\n'``).
154 ``encodestring`` is a deprecated alias.
155
Georg Brandl116aa622007-08-15 14:28:22 +0000156
Christian Heimesfe337bf2008-03-23 21:54:12 +0000157An example usage of the module:
Georg Brandl116aa622007-08-15 14:28:22 +0000158
159 >>> import base64
Georg Brandl134c35b2010-10-17 11:36:28 +0000160 >>> encoded = base64.b64encode(b'data to be encoded')
Georg Brandl116aa622007-08-15 14:28:22 +0000161 >>> encoded
Georg Brandl38d54f72009-01-18 10:43:58 +0000162 b'ZGF0YSB0byBiZSBlbmNvZGVk'
Georg Brandl116aa622007-08-15 14:28:22 +0000163 >>> data = base64.b64decode(encoded)
164 >>> data
Georg Brandl134c35b2010-10-17 11:36:28 +0000165 b'data to be encoded'
Georg Brandl116aa622007-08-15 14:28:22 +0000166
167
168.. seealso::
169
170 Module :mod:`binascii`
171 Support module containing ASCII-to-binary and binary-to-ASCII conversions.
172
173 :rfc:`1521` - MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies
174 Section 5.2, "Base64 Content-Transfer-Encoding," provides the definition of the
175 base64 encoding.
176