Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`base64` --- RFC 3548: Base16, Base32, Base64 Data Encodings |
| 3 | ================================================================= |
| 4 | |
| 5 | .. module:: base64 |
| 6 | :synopsis: RFC 3548: Base16, Base32, Base64 Data Encodings |
| 7 | |
| 8 | |
| 9 | .. index:: |
| 10 | pair: base64; encoding |
| 11 | single: MIME; base64 encoding |
| 12 | |
| 13 | This module provides data encoding and decoding as specified in :rfc:`3548`. |
| 14 | This standard defines the Base16, Base32, and Base64 algorithms for encoding and |
| 15 | decoding arbitrary binary strings into text strings that can be safely sent by |
| 16 | email, used as parts of URLs, or included as part of an HTTP POST request. The |
| 17 | encoding algorithm is not the same as the :program:`uuencode` program. |
| 18 | |
| 19 | There are two interfaces provided by this module. The modern interface supports |
| 20 | encoding and decoding string objects using all three alphabets. The legacy |
| 21 | interface provides for encoding and decoding to and from file-like objects as |
| 22 | well as strings, but only using the Base64 standard alphabet. |
| 23 | |
Georg Brandl | e6bcc91 | 2008-05-12 18:05:20 +0000 | [diff] [blame] | 24 | The modern interface provides: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 25 | |
| 26 | .. function:: b64encode(s[, altchars]) |
| 27 | |
| 28 | Encode a string use Base64. |
| 29 | |
| 30 | *s* is the string to encode. Optional *altchars* must be a string of at least |
| 31 | length 2 (additional characters are ignored) which specifies an alternative |
| 32 | alphabet for the ``+`` and ``/`` characters. This allows an application to e.g. |
| 33 | generate URL or filesystem safe Base64 strings. The default is ``None``, for |
| 34 | which the standard Base64 alphabet is used. |
| 35 | |
| 36 | The encoded string is returned. |
| 37 | |
| 38 | |
| 39 | .. function:: b64decode(s[, altchars]) |
| 40 | |
| 41 | Decode a Base64 encoded string. |
| 42 | |
| 43 | *s* is the string to decode. Optional *altchars* must be a string of at least |
| 44 | length 2 (additional characters are ignored) which specifies the alternative |
| 45 | alphabet used instead of the ``+`` and ``/`` characters. |
| 46 | |
| 47 | The decoded string is returned. A :exc:`TypeError` is raised if *s* were |
| 48 | incorrectly padded or if there are non-alphabet characters present in the |
| 49 | string. |
| 50 | |
| 51 | |
| 52 | .. function:: standard_b64encode(s) |
| 53 | |
| 54 | Encode string *s* using the standard Base64 alphabet. |
| 55 | |
| 56 | |
| 57 | .. function:: standard_b64decode(s) |
| 58 | |
| 59 | Decode string *s* using the standard Base64 alphabet. |
| 60 | |
| 61 | |
| 62 | .. function:: urlsafe_b64encode(s) |
| 63 | |
| 64 | Encode string *s* using a URL-safe alphabet, which substitutes ``-`` instead of |
| 65 | ``+`` and ``_`` instead of ``/`` in the standard Base64 alphabet. |
| 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 | |
| 80 | .. function:: b32decode(s[, casefold[, map01]]) |
| 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 | |
| 107 | .. function:: b16decode(s[, casefold]) |
| 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 | |
| 119 | The legacy interface: |
| 120 | |
| 121 | |
| 122 | .. function:: decode(input, output) |
| 123 | |
| 124 | Decode the contents of the *input* file and write the resulting binary data to |
| 125 | the *output* file. *input* and *output* must either be file objects or objects |
| 126 | that mimic the file object interface. *input* will be read until |
| 127 | ``input.read()`` returns an empty string. |
| 128 | |
| 129 | |
| 130 | .. function:: decodestring(s) |
| 131 | |
| 132 | Decode the string *s*, which must contain one or more lines of base64 encoded |
| 133 | data, and return a string containing the resulting binary data. |
| 134 | |
| 135 | |
| 136 | .. function:: encode(input, output) |
| 137 | |
| 138 | Encode the contents of the *input* file and write the resulting base64 encoded |
| 139 | data to the *output* file. *input* and *output* must either be file objects or |
| 140 | objects that mimic the file object interface. *input* will be read until |
| 141 | ``input.read()`` returns an empty string. :func:`encode` returns the encoded |
| 142 | data plus a trailing newline character (``'\n'``). |
| 143 | |
| 144 | |
| 145 | .. function:: encodestring(s) |
| 146 | |
| 147 | Encode the string *s*, which can contain arbitrary binary data, and return a |
| 148 | string containing one or more lines of base64-encoded data. |
| 149 | :func:`encodestring` returns a string containing one or more lines of |
| 150 | base64-encoded data always including an extra trailing newline (``'\n'``). |
| 151 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 152 | An example usage of the module: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 153 | |
| 154 | >>> import base64 |
| 155 | >>> encoded = base64.b64encode('data to be encoded') |
| 156 | >>> encoded |
| 157 | 'ZGF0YSB0byBiZSBlbmNvZGVk' |
| 158 | >>> data = base64.b64decode(encoded) |
| 159 | >>> data |
| 160 | 'data to be encoded' |
| 161 | |
| 162 | |
| 163 | .. seealso:: |
| 164 | |
| 165 | Module :mod:`binascii` |
| 166 | Support module containing ASCII-to-binary and binary-to-ASCII conversions. |
| 167 | |
| 168 | :rfc:`1521` - MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies |
| 169 | Section 5.2, "Base64 Content-Transfer-Encoding," provides the definition of the |
| 170 | base64 encoding. |
| 171 | |