blob: 651f889f5b03bfaba581b7202bd2455d938c97a8 [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
27 Encode a string use Base64.
28
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
35 The encoded string is returned.
36
37
Georg Brandlb868a662009-04-02 02:56:10 +000038.. function:: b64decode(s, altchars=None)
Georg Brandl116aa622007-08-15 14:28:22 +000039
40 Decode a Base64 encoded string.
41
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
46 The decoded string is returned. A :exc:`TypeError` is raised if *s* were
47 incorrectly padded or if there are non-alphabet characters present in the
48 string.
49
50
51.. function:: standard_b64encode(s)
52
53 Encode string *s* using the standard Base64 alphabet.
54
55
56.. function:: standard_b64decode(s)
57
58 Decode string *s* using the standard Base64 alphabet.
59
60
61.. function:: urlsafe_b64encode(s)
62
63 Encode 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
70 Decode string *s* using a URL-safe alphabet, which substitutes ``-`` instead of
71 ``+`` and ``_`` instead of ``/`` in the standard Base64 alphabet.
72
73
74.. function:: b32encode(s)
75
76 Encode a string using Base32. *s* is the string to encode. The encoded string
77 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
82 Decode a Base32 encoded string.
83
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
95 The decoded string is returned. A :exc:`TypeError` is raised if *s* were
96 incorrectly padded or if there are non-alphabet characters present in the
97 string.
98
99
100.. function:: b16encode(s)
101
102 Encode a string using Base16.
103
104 *s* is the string to encode. The encoded string is returned.
105
106
Georg Brandlb868a662009-04-02 02:56:10 +0000107.. function:: b16decode(s, casefold=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000108
109 Decode a Base16 encoded string.
110
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
115 The decoded string is returned. A :exc:`TypeError` is raised if *s* were
116 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 Pitrou25d535e2010-09-15 11:25:11 +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 Pitrou25d535e2010-09-15 11:25:11 +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
160 >>> encoded = base64.b64encode('data to be encoded')
161 >>> 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
165 'data to be encoded'
166
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