blob: c10a74ac8abf1f7db3f65c4ae8454e90fb7dec9e [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
21alphabets. The legacy interface provides for encoding and decoding to and from
22file-like objects as well as byte strings, but only using the Base64 standard
23alphabet.
Georg Brandl116aa622007-08-15 14:28:22 +000024
Georg Brandle6bcc912008-05-12 18:05:20 +000025The modern interface provides:
Georg Brandl116aa622007-08-15 14:28:22 +000026
Georg Brandlb868a662009-04-02 02:56:10 +000027.. function:: b64encode(s, altchars=None)
Georg Brandl116aa622007-08-15 14:28:22 +000028
R. David Murray7cefc302010-10-17 23:12:16 +000029 Encode a byte string using Base64.
Georg Brandl116aa622007-08-15 14:28:22 +000030
31 *s* is the string to encode. Optional *altchars* must be a string of at least
32 length 2 (additional characters are ignored) which specifies an alternative
33 alphabet for the ``+`` and ``/`` characters. This allows an application to e.g.
34 generate URL or filesystem safe Base64 strings. The default is ``None``, for
35 which the standard Base64 alphabet is used.
36
Georg Brandl62e42312010-08-02 20:39:35 +000037 The encoded byte string is returned.
Georg Brandl116aa622007-08-15 14:28:22 +000038
39
Georg Brandlb868a662009-04-02 02:56:10 +000040.. function:: b64decode(s, altchars=None)
Georg Brandl116aa622007-08-15 14:28:22 +000041
Georg Brandl62e42312010-08-02 20:39:35 +000042 Decode a Base64 encoded byte string.
Georg Brandl116aa622007-08-15 14:28:22 +000043
R. David Murray7cefc302010-10-17 23:12:16 +000044 *s* is the byte string to decode. Optional *altchars* must be a string of
45 at least length 2 (additional characters are ignored) which specifies the
46 alternative alphabet used instead of the ``+`` and ``/`` characters.
Georg Brandl116aa622007-08-15 14:28:22 +000047
Georg Brandl62e42312010-08-02 20:39:35 +000048 The decoded byte string is returned. A :exc:`TypeError` is raised if *s* were
Georg Brandl116aa622007-08-15 14:28:22 +000049 incorrectly padded or if there are non-alphabet characters present in the
50 string.
51
52
53.. function:: standard_b64encode(s)
54
Georg Brandl62e42312010-08-02 20:39:35 +000055 Encode byte string *s* using the standard Base64 alphabet.
Georg Brandl116aa622007-08-15 14:28:22 +000056
57
58.. function:: standard_b64decode(s)
59
Georg Brandl62e42312010-08-02 20:39:35 +000060 Decode byte string *s* using the standard Base64 alphabet.
Georg Brandl116aa622007-08-15 14:28:22 +000061
62
63.. function:: urlsafe_b64encode(s)
64
Georg Brandl62e42312010-08-02 20:39:35 +000065 Encode byte string *s* using a URL-safe alphabet, which substitutes ``-`` instead of
Benjamin Petersond75fcb42009-02-19 04:22:03 +000066 ``+`` and ``_`` instead of ``/`` in the standard Base64 alphabet. The result
67 can still contain ``=``.
Georg Brandl116aa622007-08-15 14:28:22 +000068
69
70.. function:: urlsafe_b64decode(s)
71
Georg Brandl62e42312010-08-02 20:39:35 +000072 Decode byte string *s* using a URL-safe alphabet, which substitutes ``-`` instead of
Georg Brandl116aa622007-08-15 14:28:22 +000073 ``+`` and ``_`` instead of ``/`` in the standard Base64 alphabet.
74
75
76.. function:: b32encode(s)
77
Georg Brandl62e42312010-08-02 20:39:35 +000078 Encode a byte string using Base32. *s* is the string to encode. The encoded string
Georg Brandl116aa622007-08-15 14:28:22 +000079 is returned.
80
81
Georg Brandlb868a662009-04-02 02:56:10 +000082.. function:: b32decode(s, casefold=False, map01=None)
Georg Brandl116aa622007-08-15 14:28:22 +000083
Georg Brandl62e42312010-08-02 20:39:35 +000084 Decode a Base32 encoded byte string.
Georg Brandl116aa622007-08-15 14:28:22 +000085
R. David Murray7cefc302010-10-17 23:12:16 +000086 *s* is the byte string to decode. Optional *casefold* is a flag specifying
87 whether a lowercase alphabet is acceptable as input. For security purposes,
88 the default is ``False``.
Georg Brandl116aa622007-08-15 14:28:22 +000089
90 :rfc:`3548` allows for optional mapping of the digit 0 (zero) to the letter O
91 (oh), and for optional mapping of the digit 1 (one) to either the letter I (eye)
92 or letter L (el). The optional argument *map01* when not ``None``, specifies
93 which letter the digit 1 should be mapped to (when *map01* is not ``None``, the
94 digit 0 is always mapped to the letter O). For security purposes the default is
95 ``None``, so that 0 and 1 are not allowed in the input.
96
Georg Brandl62e42312010-08-02 20:39:35 +000097 The decoded byte string is returned. A :exc:`TypeError` is raised if *s* were
Georg Brandl116aa622007-08-15 14:28:22 +000098 incorrectly padded or if there are non-alphabet characters present in the
99 string.
100
101
102.. function:: b16encode(s)
103
Georg Brandl62e42312010-08-02 20:39:35 +0000104 Encode a byte string using Base16.
Georg Brandl116aa622007-08-15 14:28:22 +0000105
Georg Brandl62e42312010-08-02 20:39:35 +0000106 *s* is the string to encode. The encoded byte string is returned.
Georg Brandl116aa622007-08-15 14:28:22 +0000107
108
Georg Brandlb868a662009-04-02 02:56:10 +0000109.. function:: b16decode(s, casefold=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000110
Georg Brandl62e42312010-08-02 20:39:35 +0000111 Decode a Base16 encoded byte string.
Georg Brandl116aa622007-08-15 14:28:22 +0000112
113 *s* is the string to decode. Optional *casefold* is a flag specifying whether a
114 lowercase alphabet is acceptable as input. For security purposes, the default
115 is ``False``.
116
Georg Brandl62e42312010-08-02 20:39:35 +0000117 The decoded byte string is returned. A :exc:`TypeError` is raised if *s* were
Georg Brandl116aa622007-08-15 14:28:22 +0000118 incorrectly padded or if there are non-alphabet characters present in the
119 string.
120
Georg Brandl116aa622007-08-15 14:28:22 +0000121
Georg Brandlb54d8012009-06-04 09:11:51 +0000122The legacy interface:
Georg Brandl116aa622007-08-15 14:28:22 +0000123
124.. function:: decode(input, output)
125
Georg Brandlb54d8012009-06-04 09:11:51 +0000126 Decode the contents of the binary *input* file and write the resulting binary
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000127 data to the *output* file. *input* and *output* must be :term:`file objects
128 <file object>`. *input* will be read until ``input.read()`` returns an empty
129 bytes object.
Georg Brandl116aa622007-08-15 14:28:22 +0000130
131
Georg Brandlb54d8012009-06-04 09:11:51 +0000132.. function:: decodebytes(s)
133 decodestring(s)
Georg Brandl116aa622007-08-15 14:28:22 +0000134
R. David Murray7cefc302010-10-17 23:12:16 +0000135 Decode the byte string *s*, which must contain one or more lines of base64
136 encoded data, and return a byte string containing the resulting binary data.
Georg Brandlb54d8012009-06-04 09:11:51 +0000137 ``decodestring`` is a deprecated alias.
Georg Brandl116aa622007-08-15 14:28:22 +0000138
139
140.. function:: encode(input, output)
141
Georg Brandlb54d8012009-06-04 09:11:51 +0000142 Encode the contents of the binary *input* file and write the resulting base64
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000143 encoded data to the *output* file. *input* and *output* must be :term:`file
144 objects <file object>`. *input* will be read until ``input.read()`` returns
145 an empty bytes object. :func:`encode` returns the encoded data plus a trailing
146 newline character (``b'\n'``).
Georg Brandl116aa622007-08-15 14:28:22 +0000147
148
Georg Brandlb54d8012009-06-04 09:11:51 +0000149.. function:: encodebytes(s)
150 encodestring(s)
Georg Brandl116aa622007-08-15 14:28:22 +0000151
R. David Murray7cefc302010-10-17 23:12:16 +0000152 Encode the byte string *s*, which can contain arbitrary binary data, and
153 return a byte string containing one or more lines of base64-encoded data.
Georg Brandlb54d8012009-06-04 09:11:51 +0000154 :func:`encodebytes` returns a string containing one or more lines of
155 base64-encoded data always including an extra trailing newline (``b'\n'``).
156 ``encodestring`` is a deprecated alias.
157
Georg Brandl116aa622007-08-15 14:28:22 +0000158
Christian Heimesfe337bf2008-03-23 21:54:12 +0000159An example usage of the module:
Georg Brandl116aa622007-08-15 14:28:22 +0000160
161 >>> import base64
Georg Brandl134c35b2010-10-17 11:36:28 +0000162 >>> encoded = base64.b64encode(b'data to be encoded')
Georg Brandl116aa622007-08-15 14:28:22 +0000163 >>> encoded
Georg Brandl38d54f72009-01-18 10:43:58 +0000164 b'ZGF0YSB0byBiZSBlbmNvZGVk'
Georg Brandl116aa622007-08-15 14:28:22 +0000165 >>> data = base64.b64decode(encoded)
166 >>> data
Georg Brandl134c35b2010-10-17 11:36:28 +0000167 b'data to be encoded'
Georg Brandl116aa622007-08-15 14:28:22 +0000168
169
170.. seealso::
171
172 Module :mod:`binascii`
173 Support module containing ASCII-to-binary and binary-to-ASCII conversions.
174
175 :rfc:`1521` - MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies
176 Section 5.2, "Base64 Content-Transfer-Encoding," provides the definition of the
177 base64 encoding.
178