Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :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 | |
| 12 | This module provides data encoding and decoding as specified in :rfc:`3548`. |
R. David Murray | 7cefc30 | 2010-10-17 23:12:16 +0000 | [diff] [blame] | 13 | This standard defines the Base16, Base32, and Base64 algorithms for encoding |
| 14 | and decoding arbitrary binary strings into ASCII-only byte strings that can be |
| 15 | safely sent by email, used as parts of URLs, or included as part of an HTTP |
| 16 | POST request. The encoding algorithm is not the same as the |
| 17 | :program:`uuencode` program. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 18 | |
R. David Murray | 7cefc30 | 2010-10-17 23:12:16 +0000 | [diff] [blame] | 19 | There are two interfaces provided by this module. The modern interface |
| 20 | supports encoding and decoding ASCII byte string objects using all three |
Antoine Pitrou | ea6b4d5 | 2012-02-20 19:30:23 +0100 | [diff] [blame] | 21 | alphabets. Additionally, the decoding functions of the modern interface also |
| 22 | accept Unicode strings containing only ASCII characters. The legacy interface |
| 23 | provides for encoding and decoding to and from file-like objects as well as |
| 24 | byte 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 29 | |
Nick Coghlan | fdf239a | 2013-10-03 00:43:22 +1000 | [diff] [blame] | 30 | .. 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 Brandl | e6bcc91 | 2008-05-12 18:05:20 +0000 | [diff] [blame] | 34 | The modern interface provides: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 35 | |
Georg Brandl | b868a66 | 2009-04-02 02:56:10 +0000 | [diff] [blame] | 36 | .. function:: b64encode(s, altchars=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 37 | |
R. David Murray | 7cefc30 | 2010-10-17 23:12:16 +0000 | [diff] [blame] | 38 | Encode a byte string using Base64. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 39 | |
| 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 Brandl | 62e4231 | 2010-08-02 20:39:35 +0000 | [diff] [blame] | 46 | The encoded byte string is returned. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 47 | |
| 48 | |
R. David Murray | 6495136 | 2010-11-11 20:09:20 +0000 | [diff] [blame] | 49 | .. function:: b64decode(s, altchars=None, validate=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 50 | |
Georg Brandl | 62e4231 | 2010-08-02 20:39:35 +0000 | [diff] [blame] | 51 | Decode a Base64 encoded byte string. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 52 | |
R. David Murray | 7cefc30 | 2010-10-17 23:12:16 +0000 | [diff] [blame] | 53 | *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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 56 | |
Éric Araujo | 941afed | 2011-09-01 02:47:34 +0200 | [diff] [blame] | 57 | The decoded string is returned. A :exc:`binascii.Error` exception is raised |
| 58 | if *s* is incorrectly padded. |
R. David Murray | 6495136 | 2010-11-11 20:09:20 +0000 | [diff] [blame] | 59 | |
| 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 64 | |
| 65 | |
| 66 | .. function:: standard_b64encode(s) |
| 67 | |
Georg Brandl | 62e4231 | 2010-08-02 20:39:35 +0000 | [diff] [blame] | 68 | Encode byte string *s* using the standard Base64 alphabet. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 69 | |
| 70 | |
| 71 | .. function:: standard_b64decode(s) |
| 72 | |
Georg Brandl | 62e4231 | 2010-08-02 20:39:35 +0000 | [diff] [blame] | 73 | Decode byte string *s* using the standard Base64 alphabet. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 74 | |
| 75 | |
| 76 | .. function:: urlsafe_b64encode(s) |
| 77 | |
Georg Brandl | 62e4231 | 2010-08-02 20:39:35 +0000 | [diff] [blame] | 78 | Encode byte string *s* using a URL-safe alphabet, which substitutes ``-`` instead of |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 79 | ``+`` and ``_`` instead of ``/`` in the standard Base64 alphabet. The result |
| 80 | can still contain ``=``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | |
| 82 | |
| 83 | .. function:: urlsafe_b64decode(s) |
| 84 | |
Georg Brandl | 62e4231 | 2010-08-02 20:39:35 +0000 | [diff] [blame] | 85 | Decode byte string *s* using a URL-safe alphabet, which substitutes ``-`` instead of |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 86 | ``+`` and ``_`` instead of ``/`` in the standard Base64 alphabet. |
| 87 | |
| 88 | |
| 89 | .. function:: b32encode(s) |
| 90 | |
Georg Brandl | 62e4231 | 2010-08-02 20:39:35 +0000 | [diff] [blame] | 91 | Encode a byte string using Base32. *s* is the string to encode. The encoded string |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 92 | is returned. |
| 93 | |
| 94 | |
Georg Brandl | b868a66 | 2009-04-02 02:56:10 +0000 | [diff] [blame] | 95 | .. function:: b32decode(s, casefold=False, map01=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 96 | |
Georg Brandl | 62e4231 | 2010-08-02 20:39:35 +0000 | [diff] [blame] | 97 | Decode a Base32 encoded byte string. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 98 | |
R. David Murray | 7cefc30 | 2010-10-17 23:12:16 +0000 | [diff] [blame] | 99 | *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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 102 | |
| 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 Storchaka | ea2b490 | 2013-05-28 15:27:29 +0300 | [diff] [blame] | 110 | The decoded byte string is returned. A :exc:`binascii.Error` is raised if *s* were |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 111 | incorrectly padded or if there are non-alphabet characters present in the |
| 112 | string. |
| 113 | |
| 114 | |
| 115 | .. function:: b16encode(s) |
| 116 | |
Georg Brandl | 62e4231 | 2010-08-02 20:39:35 +0000 | [diff] [blame] | 117 | Encode a byte string using Base16. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 118 | |
Georg Brandl | 62e4231 | 2010-08-02 20:39:35 +0000 | [diff] [blame] | 119 | *s* is the string to encode. The encoded byte string is returned. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 120 | |
| 121 | |
Georg Brandl | b868a66 | 2009-04-02 02:56:10 +0000 | [diff] [blame] | 122 | .. function:: b16decode(s, casefold=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 123 | |
Georg Brandl | 62e4231 | 2010-08-02 20:39:35 +0000 | [diff] [blame] | 124 | Decode a Base16 encoded byte string. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 125 | |
| 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 Brandl | 62e4231 | 2010-08-02 20:39:35 +0000 | [diff] [blame] | 130 | The decoded byte string is returned. A :exc:`TypeError` is raised if *s* were |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 131 | incorrectly padded or if there are non-alphabet characters present in the |
| 132 | string. |
| 133 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 134 | |
Antoine Pitrou | 6dd0d46 | 2013-11-17 23:52:25 +0100 | [diff] [blame] | 135 | .. function:: a85encode(s, *, foldspaces=False, wrapcol=0, pad=False, adobe=False) |
| 136 | |
| 137 | Encode a byte string using Ascii85. |
| 138 | |
| 139 | *s* is the string to encode. The encoded byte string is returned. |
| 140 | |
| 141 | *foldspaces* is an optional flag that uses the special short sequence 'y' |
| 142 | instead of 4 consecutive spaces (ASCII 0x20) as supported by 'btoa'. This |
| 143 | feature is not supported by the "standard" Ascii85 encoding. |
| 144 | |
| 145 | *wrapcol* controls whether the output should have newline ('\n') |
| 146 | characters added to it. If this is non-zero, each output line will be |
| 147 | at most this many characters long. |
| 148 | |
| 149 | *pad* controls whether the input string is padded to a multiple of 4 |
| 150 | before encoding. Note that the ``btoa`` implementation always pads. |
| 151 | |
| 152 | *adobe* controls whether the encoded byte sequence is framed with ``<~`` |
| 153 | and ``~>``, which is used by the Adobe implementation. |
| 154 | |
| 155 | .. versionadded:: 3.4 |
| 156 | |
| 157 | |
| 158 | .. function:: a85decode(s, *, foldspaces=False, adobe=False, ignorechars=b' \t\n\r\v') |
| 159 | |
| 160 | Decode an Ascii85 encoded byte string. |
| 161 | |
| 162 | *s* is the byte string to decode. |
| 163 | |
| 164 | *foldspaces* is a flag that specifies whether the 'y' short sequence |
| 165 | should be accepted as shorthand for 4 consecutive spaces (ASCII 0x20). |
| 166 | This feature is not supported by the "standard" Ascii85 encoding. |
| 167 | |
| 168 | *adobe* controls whether the input sequence is in Adobe Ascii85 format |
| 169 | (i.e. is framed with <~ and ~>). |
| 170 | |
| 171 | *ignorechars* should be a byte string containing characters to ignore |
| 172 | from the input. This should only contain whitespace characters, and by |
| 173 | default contains all whitespace characters in ASCII. |
| 174 | |
| 175 | .. versionadded:: 3.4 |
| 176 | |
| 177 | |
| 178 | .. function:: b85encode(s, pad=False) |
| 179 | |
| 180 | Encode a byte string using base85, as used in e.g. git-style binary |
| 181 | diffs. |
| 182 | |
| 183 | If *pad* is true, the input is padded with "\\0" so its length is a |
| 184 | multiple of 4 characters before encoding. |
| 185 | |
| 186 | .. versionadded:: 3.4 |
| 187 | |
| 188 | |
| 189 | .. function:: b85decode(b) |
| 190 | |
| 191 | Decode base85-encoded byte string. Padding is implicitly removed, if |
| 192 | necessary. |
| 193 | |
| 194 | .. versionadded:: 3.4 |
| 195 | |
| 196 | |
| 197 | .. note:: |
| 198 | Both Base85 and Ascii85 have an expansion factor of 5 to 4 (5 Base85 or |
| 199 | Ascii85 characters can encode 4 binary bytes), while the better-known |
| 200 | Base64 has an expansion factor of 6 to 4. They are therefore more |
| 201 | efficient when space expensive. They differ by details such as the |
| 202 | character map used for encoding. |
| 203 | |
| 204 | |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 205 | The legacy interface: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 206 | |
| 207 | .. function:: decode(input, output) |
| 208 | |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 209 | Decode the contents of the binary *input* file and write the resulting binary |
Antoine Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 210 | data to the *output* file. *input* and *output* must be :term:`file objects |
| 211 | <file object>`. *input* will be read until ``input.read()`` returns an empty |
| 212 | bytes object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 213 | |
| 214 | |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 215 | .. function:: decodebytes(s) |
| 216 | decodestring(s) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 217 | |
R. David Murray | 7cefc30 | 2010-10-17 23:12:16 +0000 | [diff] [blame] | 218 | Decode the byte string *s*, which must contain one or more lines of base64 |
| 219 | encoded data, and return a byte string containing the resulting binary data. |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 220 | ``decodestring`` is a deprecated alias. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 221 | |
R David Murray | 75fd225 | 2012-08-17 20:55:21 -0400 | [diff] [blame] | 222 | .. versionadded:: 3.1 |
| 223 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 224 | |
| 225 | .. function:: encode(input, output) |
| 226 | |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 227 | Encode the contents of the binary *input* file and write the resulting base64 |
Antoine Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 228 | encoded data to the *output* file. *input* and *output* must be :term:`file |
| 229 | objects <file object>`. *input* will be read until ``input.read()`` returns |
| 230 | an empty bytes object. :func:`encode` returns the encoded data plus a trailing |
| 231 | newline character (``b'\n'``). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 232 | |
| 233 | |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 234 | .. function:: encodebytes(s) |
| 235 | encodestring(s) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 236 | |
R. David Murray | 7cefc30 | 2010-10-17 23:12:16 +0000 | [diff] [blame] | 237 | Encode the byte string *s*, which can contain arbitrary binary data, and |
| 238 | return a byte string containing one or more lines of base64-encoded data. |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 239 | :func:`encodebytes` returns a string containing one or more lines of |
| 240 | base64-encoded data always including an extra trailing newline (``b'\n'``). |
| 241 | ``encodestring`` is a deprecated alias. |
| 242 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 243 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 244 | An example usage of the module: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 245 | |
| 246 | >>> import base64 |
Georg Brandl | 134c35b | 2010-10-17 11:36:28 +0000 | [diff] [blame] | 247 | >>> encoded = base64.b64encode(b'data to be encoded') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 248 | >>> encoded |
Georg Brandl | 38d54f7 | 2009-01-18 10:43:58 +0000 | [diff] [blame] | 249 | b'ZGF0YSB0byBiZSBlbmNvZGVk' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 250 | >>> data = base64.b64decode(encoded) |
| 251 | >>> data |
Georg Brandl | 134c35b | 2010-10-17 11:36:28 +0000 | [diff] [blame] | 252 | b'data to be encoded' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 253 | |
| 254 | |
| 255 | .. seealso:: |
| 256 | |
| 257 | Module :mod:`binascii` |
| 258 | Support module containing ASCII-to-binary and binary-to-ASCII conversions. |
| 259 | |
| 260 | :rfc:`1521` - MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies |
| 261 | Section 5.2, "Base64 Content-Transfer-Encoding," provides the definition of the |
| 262 | base64 encoding. |
| 263 | |