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`. |
| 13 | This standard defines the Base16, Base32, and Base64 algorithms for encoding and |
| 14 | decoding arbitrary binary strings into text strings that can be safely sent by |
| 15 | email, used as parts of URLs, or included as part of an HTTP POST request. The |
| 16 | encoding algorithm is not the same as the :program:`uuencode` program. |
| 17 | |
| 18 | There are two interfaces provided by this module. The modern interface supports |
| 19 | encoding and decoding string objects using all three alphabets. The legacy |
| 20 | interface provides for encoding and decoding to and from file-like objects as |
| 21 | well as strings, but only using the Base64 standard alphabet. |
| 22 | |
Georg Brandl | e6bcc91 | 2008-05-12 18:05:20 +0000 | [diff] [blame] | 23 | The modern interface provides: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 24 | |
Georg Brandl | b868a66 | 2009-04-02 02:56:10 +0000 | [diff] [blame] | 25 | .. function:: b64encode(s, altchars=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | |
| 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 Brandl | b868a66 | 2009-04-02 02:56:10 +0000 | [diff] [blame] | 38 | .. function:: b64decode(s, altchars=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 39 | |
| 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 Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 64 | ``+`` and ``_`` instead of ``/`` in the standard Base64 alphabet. The result |
| 65 | can still contain ``=``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 66 | |
| 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 Brandl | b868a66 | 2009-04-02 02:56:10 +0000 | [diff] [blame] | 80 | .. function:: b32decode(s, casefold=False, map01=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | |
| 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 Brandl | b868a66 | 2009-04-02 02:56:10 +0000 | [diff] [blame] | 107 | .. function:: b16decode(s, casefold=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 108 | |
| 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 119 | |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 120 | The legacy interface: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 121 | |
| 122 | .. function:: decode(input, output) |
| 123 | |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 124 | Decode the contents of the binary *input* file and write the resulting binary |
Antoine Pitrou | 25d535e | 2010-09-15 11:25:11 +0000 | [diff] [blame] | 125 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 128 | |
| 129 | |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 130 | .. function:: decodebytes(s) |
| 131 | decodestring(s) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 132 | |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 133 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 136 | |
| 137 | |
| 138 | .. function:: encode(input, output) |
| 139 | |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 140 | Encode the contents of the binary *input* file and write the resulting base64 |
Antoine Pitrou | 25d535e | 2010-09-15 11:25:11 +0000 | [diff] [blame] | 141 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 145 | |
| 146 | |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 147 | .. function:: encodebytes(s) |
| 148 | encodestring(s) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 149 | |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 150 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 156 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 157 | An example usage of the module: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 158 | |
| 159 | >>> import base64 |
| 160 | >>> encoded = base64.b64encode('data to be encoded') |
| 161 | >>> encoded |
Georg Brandl | 38d54f7 | 2009-01-18 10:43:58 +0000 | [diff] [blame] | 162 | b'ZGF0YSB0byBiZSBlbmNvZGVk' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 163 | >>> 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 | |