blob: b51f8c820bc7c18a046c09e08ec85c97f7acdac6 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{base64} ---
Barry Warsawad9aaee2004-01-04 01:14:01 +00002 RFC 3548: Base16, Base32, Base64 Data Encodings}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drake180b68b1999-02-22 13:45:09 +00004\declaremodule{standard}{base64}
Barry Warsawad9aaee2004-01-04 01:14:01 +00005\modulesynopsis{RFC 3548: Base16, Base32, Base64 Data Encodings}
Fred Drake180b68b1999-02-22 13:45:09 +00006
Fred Drakeb91e9341998-07-23 17:59:49 +00007
Fred Drake674e0fd1998-04-02 16:24:29 +00008\indexii{base64}{encoding}
9\index{MIME!base64 encoding}
Guido van Rossume76b7a81997-04-27 21:25:52 +000010
Barry Warsawad9aaee2004-01-04 01:14:01 +000011This module provides data encoding and decoding as specified in
12\rfc{3548}. This standard defines the Base16, Base32, and Base64
13algorithms for encoding and decoding arbitrary binary strings into
14text strings that can be safely sent by email, used as parts of URLs,
15or included as part of an HTTP POST request. The encoding algorith is
16not the same as the \program{uuencode} program.
Guido van Rossume76b7a81997-04-27 21:25:52 +000017
Barry Warsawad9aaee2004-01-04 01:14:01 +000018There are two interfaces provided by this module. The modern
19interface supports encoding and decoding string objects using all
20three alphabets. The legacy interface provides for encoding and
21decoding to and from file-like objects as well as strings, but only
22using the Base64 standard alphabet.
23
24The modern interface provides:
25
26\begin{funcdesc}{b64encode}{s\optional{, altchars}}
27Encode a string use Base64.
28
29\var{s} is the string to encode. Optional \var{altchars} must be a
30string of at least length 2 (additional characters are ignored) which
31specifies an alternative alphabet for the \code{+} and \code{/}
32characters. This allows an application to e.g. generate URL or
33filesystem safe Base64 strings. The default is \code{None}, for which
34the standard Base64 alphabet is used.
35
36The encoded string is returned.
37\end{funcdesc}
38
39\begin{funcdesc}{b64decode}{s\optional{, altchars}}
40Decode a Base64 encoded string.
41
42\var{s} is the string to decode. Optional \var{altchars} must be a
43string of at least length 2 (additional characters are ignored) which
44specifies the alternative alphabet used instead of the \code{+} and
45\code{/} characters.
46
47The decoded string is returned. A \exception{TypeError} is raised if
48\var{s} were incorrectly padded or if there are non-alphabet
49characters present in the string.
50\end{funcdesc}
51
52\begin{funcdesc}{standard_b64encode}{s}
53Encode string \var{s} using the standard Base64 alphabet.
54\end{funcdesc}
55
56\begin{funcdesc}{standard_b64decode}{s}
57Decode string \var{s} using the standard Base64 alphabet.
58\end{funcdesc}
59
60\begin{funcdesc}{urlsafe_b64encode}{s}
61Encode string \var{s} using a URL-safe alphabet, which substitutes
62\code{-} instead of \code{+} and \code{_} instead of \code{/} in the
63standard Base64 alphabet.
64\end{funcdesc}
65
66\begin{funcdesc}{urlsafe_b64decode}{s}
67Decode string \var{s} using a URL-safe alphabet, which substitutes
68\code{-} instead of \code{+} and \code{_} instead of \code{/} in the
69standard Base64 alphabet.
70\end{funcdesc}
71
72\begin{funcdesc}{b32encode}{s}
73Encode a string using Base32. \var{s} is the string to encode. The
74encoded string is returned.
75\end{funcdesc}
76
77\begin{funcdesc}{b32decode}{s\optional{, casefold\optional{, map01}}}
78Decode a Base32 encoded string.
79
80\var{s} is the string to decode. Optional \var{casefold} is a flag
81specifying whether a lowercase alphabet is acceptable as input. For
82security purposes, the default is \code{False}.
83
84\rfc{3548} allows for optional mapping of the digit 0 (zero) to the
85letter O (oh), and for optional mapping of the digit 1 (one) to either
86the letter I (eye) or letter L (el). The optional argument
87\var{map01} when not \code{None}, specifies which letter the digit 1 should
88be mapped to (when map01 is not \var{None}, the digit 0 is always
89mapped to the letter O). For security purposes the default is
90\code{None}, so that 0 and 1 are not allowed in the input.
91
92The decoded string is returned. A \exception{TypeError} is raised if
93\var{s} were incorrectly padded or if there are non-alphabet characters
94present in the string.
95\end{funcdesc}
96
97\begin{funcdesc}{b16encode}{s}
98Encode a string using Base16.
99
100\var{s} is the string to encode. The encoded string is returned.
101\end{funcdesc}
102
103\begin{funcdesc}{b16decode}{s\optional{, casefold}}
104Decode a Base16 encoded string.
105
106\var{s} is the string to decode. Optional \var{casefold} is a flag
107specifying whether a lowercase alphabet is acceptable as input. For
108security purposes, the default is \code{False}.
109
110The decoded string is returned. A \exception{TypeError} is raised if
111\var{s} were incorrectly padded or if there are non-alphabet
112characters present in the string.
113\end{funcdesc}
114
115The legacy interface:
Fred Drake798654f1997-11-30 05:53:22 +0000116
Fred Drake79948431998-01-07 03:47:10 +0000117\begin{funcdesc}{decode}{input, output}
Guido van Rossume76b7a81997-04-27 21:25:52 +0000118Decode the contents of the \var{input} file and write the resulting
119binary data to the \var{output} file.
120\var{input} and \var{output} must either be file objects or objects that
121mimic the file object interface. \var{input} will be read until
122\code{\var{input}.read()} returns an empty string.
123\end{funcdesc}
124
125\begin{funcdesc}{decodestring}{s}
126Decode the string \var{s}, which must contain one or more lines of
Fred Drake674e0fd1998-04-02 16:24:29 +0000127base64 encoded data, and return a string containing the resulting
Guido van Rossume76b7a81997-04-27 21:25:52 +0000128binary data.
129\end{funcdesc}
130
Fred Drake79948431998-01-07 03:47:10 +0000131\begin{funcdesc}{encode}{input, output}
Guido van Rossume76b7a81997-04-27 21:25:52 +0000132Encode the contents of the \var{input} file and write the resulting
Fred Drake674e0fd1998-04-02 16:24:29 +0000133base64 encoded data to the \var{output} file.
Guido van Rossume76b7a81997-04-27 21:25:52 +0000134\var{input} and \var{output} must either be file objects or objects that
135mimic the file object interface. \var{input} will be read until
Raymond Hettingercadc9fb2002-05-16 04:28:44 +0000136\code{\var{input}.read()} returns an empty string. \function{encode()}
137returns the encoded data plus a trailing newline character
138(\code{'\e n'}).
Guido van Rossume76b7a81997-04-27 21:25:52 +0000139\end{funcdesc}
140
141\begin{funcdesc}{encodestring}{s}
142Encode the string \var{s}, which can contain arbitrary binary data,
143and return a string containing one or more lines of
Raymond Hettingercadc9fb2002-05-16 04:28:44 +0000144base64-encoded data. \function{encodestring()} returns a
145string containing one or more lines of base64-encoded data
146always including an extra trailing newline (\code{'\e n'}).
Guido van Rossume76b7a81997-04-27 21:25:52 +0000147\end{funcdesc}
Fred Drakeae35aa81999-04-23 15:52:18 +0000148
Fred Drakeae35aa81999-04-23 15:52:18 +0000149\begin{seealso}
Fred Drakec37b65e2001-11-28 07:26:15 +0000150 \seemodule{binascii}{Support module containing \ASCII-to-binary
Fred Drakeba0a9892000-10-18 17:43:06 +0000151 and binary-to-\ASCII{} conversions.}
Fred Drakee20bd192001-04-12 16:47:17 +0000152 \seerfc{1521}{MIME (Multipurpose Internet Mail Extensions) Part One:
153 Mechanisms for Specifying and Describing the Format of
154 Internet Message Bodies}{Section 5.2, ``Base64
155 Content-Transfer-Encoding,'' provides the definition of the
156 base64 encoding.}
Fred Drakeae35aa81999-04-23 15:52:18 +0000157\end{seealso}