annotate Lib/base64.py @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents fb9a7b288dce
children 4269267e6e57 6b18a095c2e7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5243
4f251b8e328f Add #! line.
Guido van Rossum <guido@python.org>
parents: 3145
diff changeset
1 #! /usr/bin/env python
4f251b8e328f Add #! line.
Guido van Rossum <guido@python.org>
parents: 3145
diff changeset
2
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
3 """RFC 3548: Base16, Base32, Base64 Data Encodings"""
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
4
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
5 # Modified 04-Oct-1995 by Jack Jansen to use binascii module
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
6 # Modified 30-Dec-2003 by Barry Warsaw to add full RFC 3548 support
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
8 import re
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
9 import struct
3145
43f8db23ef15 Use binascii module (resulting in a 60-fold speedup:-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3104
diff changeset
10 import binascii
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
12
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
13 __all__ = [
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
14 # Legacy interface exports traditional RFC 1521 Base64 encodings
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
15 'encode', 'decode', 'encodestring', 'decodestring',
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
16 # Generalized interface for other encodings
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
17 'b64encode', 'b64decode', 'b32encode', 'b32decode',
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
18 'b16encode', 'b16decode',
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
19 # Standard Base64 encoding
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
20 'standard_b64encode', 'standard_b64decode',
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
21 # Some common Base64 alternatives. As referenced by RFC 3458, see thread
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
22 # starting at:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
23 #
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
24 # http://zgp.org/pipermail/p2p-hackers/2001-September/000316.html
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
25 'urlsafe_b64encode', 'urlsafe_b64decode',
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
26 ]
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
27
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
28 _translation = [chr(_x) for _x in range(256)]
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
29 EMPTYSTRING = ''
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
30
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
31
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
32 def _translate(s, altchars):
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
33 translation = _translation[:]
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
34 for k, v in altchars.items():
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
35 translation[ord(k)] = v
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
36 return s.translate(''.join(translation))
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
37
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
38
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
39
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
40 # Base64 encoding/decoding uses binascii
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
41
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
42 def b64encode(s, altchars=None):
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
43 """Encode a string using Base64.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
44
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
45 s is the string to encode. Optional altchars must be a string of at least
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
46 length 2 (additional characters are ignored) which specifies an
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
47 alternative alphabet for the '+' and '/' characters. This allows an
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
48 application to e.g. generate url or filesystem safe Base64 strings.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
49
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
50 The encoded string is returned.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
51 """
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
52 # Strip off the trailing newline
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
53 encoded = binascii.b2a_base64(s)[:-1]
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
54 if altchars is not None:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
55 return _translate(encoded, {'+': altchars[0], '/': altchars[1]})
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
56 return encoded
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
57
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
58
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
59 def b64decode(s, altchars=None):
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
60 """Decode a Base64 encoded string.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
61
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
62 s is the string to decode. Optional altchars must be a string of at least
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
63 length 2 (additional characters are ignored) which specifies the
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
64 alternative alphabet used instead of the '+' and '/' characters.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
65
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
66 The decoded string is returned. A TypeError is raised if s were
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
67 incorrectly padded or if there are non-alphabet characters present in the
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
68 string.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
69 """
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
70 if altchars is not None:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
71 s = _translate(s, {altchars[0]: '+', altchars[1]: '/'})
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
72 try:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
73 return binascii.a2b_base64(s)
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
74 except binascii.Error, msg:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
75 # Transform this exception for consistency
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
76 raise TypeError(msg)
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
77
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
78
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
79 def standard_b64encode(s):
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
80 """Encode a string using the standard Base64 alphabet.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
81
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
82 s is the string to encode. The encoded string is returned.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
83 """
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
84 return b64encode(s)
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
85
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
86 def standard_b64decode(s):
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
87 """Decode a string encoded with the standard Base64 alphabet.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
88
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
89 s is the string to decode. The decoded string is returned. A TypeError
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
90 is raised if the string is incorrectly padded or if there are non-alphabet
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
91 characters present in the string.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
92 """
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
93 return b64decode(s)
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
94
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
95 def urlsafe_b64encode(s):
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
96 """Encode a string using a url-safe Base64 alphabet.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
97
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
98 s is the string to encode. The encoded string is returned. The alphabet
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
99 uses '-' instead of '+' and '_' instead of '/'.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
100 """
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
101 return b64encode(s, '-_')
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
102
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
103 def urlsafe_b64decode(s):
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
104 """Decode a string encoded with the standard Base64 alphabet.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
105
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
106 s is the string to decode. The decoded string is returned. A TypeError
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
107 is raised if the string is incorrectly padded or if there are non-alphabet
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
108 characters present in the string.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
109
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
110 The alphabet uses '-' instead of '+' and '_' instead of '/'.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
111 """
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
112 return b64decode(s, '-_')
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
113
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
114
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
115
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
116 # Base32 encoding/decoding must be done in Python
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
117 _b32alphabet = {
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
118 0: 'A', 9: 'J', 18: 'S', 27: '3',
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
119 1: 'B', 10: 'K', 19: 'T', 28: '4',
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
120 2: 'C', 11: 'L', 20: 'U', 29: '5',
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
121 3: 'D', 12: 'M', 21: 'V', 30: '6',
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
122 4: 'E', 13: 'N', 22: 'W', 31: '7',
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
123 5: 'F', 14: 'O', 23: 'X',
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
124 6: 'G', 15: 'P', 24: 'Y',
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
125 7: 'H', 16: 'Q', 25: 'Z',
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
126 8: 'I', 17: 'R', 26: '2',
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
127 }
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
128
38464
e989ee5d404a ("Forward-port" of r46506)
Armin Rigo <arigo@tunes.org>
parents: 35047
diff changeset
129 _b32tab = _b32alphabet.items()
e989ee5d404a ("Forward-port" of r46506)
Armin Rigo <arigo@tunes.org>
parents: 35047
diff changeset
130 _b32tab.sort()
e989ee5d404a ("Forward-port" of r46506)
Armin Rigo <arigo@tunes.org>
parents: 35047
diff changeset
131 _b32tab = [v for k, v in _b32tab]
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
132 _b32rev = dict([(v, long(k)) for k, v in _b32alphabet.items()])
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
133
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
134
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
135 def b32encode(s):
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
136 """Encode a string using Base32.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
137
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
138 s is the string to encode. The encoded string is returned.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
139 """
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
140 parts = []
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
141 quanta, leftover = divmod(len(s), 5)
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
142 # Pad the last quantum with zero bits if necessary
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
143 if leftover:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
144 s += ('\0' * (5 - leftover))
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
145 quanta += 1
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
146 for i in range(quanta):
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
147 # c1 and c2 are 16 bits wide, c3 is 8 bits wide. The intent of this
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
148 # code is to process the 40 bits in units of 5 bits. So we take the 1
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
149 # leftover bit of c1 and tack it onto c2. Then we take the 2 leftover
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
150 # bits of c2 and tack them onto c3. The shifts and masks are intended
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
151 # to give us values of exactly 5 bits in width.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
152 c1, c2, c3 = struct.unpack('!HHB', s[i*5:(i+1)*5])
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
153 c2 += (c1 & 1) << 16 # 17 bits wide
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
154 c3 += (c2 & 3) << 8 # 10 bits wide
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
155 parts.extend([_b32tab[c1 >> 11], # bits 1 - 5
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
156 _b32tab[(c1 >> 6) & 0x1f], # bits 6 - 10
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
157 _b32tab[(c1 >> 1) & 0x1f], # bits 11 - 15
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
158 _b32tab[c2 >> 12], # bits 16 - 20 (1 - 5)
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
159 _b32tab[(c2 >> 7) & 0x1f], # bits 21 - 25 (6 - 10)
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
160 _b32tab[(c2 >> 2) & 0x1f], # bits 26 - 30 (11 - 15)
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
161 _b32tab[c3 >> 5], # bits 31 - 35 (1 - 5)
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
162 _b32tab[c3 & 0x1f], # bits 36 - 40 (1 - 5)
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
163 ])
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
164 encoded = EMPTYSTRING.join(parts)
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
165 # Adjust for any leftover partial quanta
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
166 if leftover == 1:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
167 return encoded[:-6] + '======'
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
168 elif leftover == 2:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
169 return encoded[:-4] + '===='
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
170 elif leftover == 3:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
171 return encoded[:-3] + '==='
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
172 elif leftover == 4:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
173 return encoded[:-1] + '='
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
174 return encoded
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
175
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
176
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
177 def b32decode(s, casefold=False, map01=None):
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
178 """Decode a Base32 encoded string.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
179
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
180 s is the string to decode. Optional casefold is a flag specifying whether
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
181 a lowercase alphabet is acceptable as input. For security purposes, the
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
182 default is False.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
183
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
184 RFC 3548 allows for optional mapping of the digit 0 (zero) to the letter O
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
185 (oh), and for optional mapping of the digit 1 (one) to either the letter I
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
186 (eye) or letter L (el). The optional argument map01 when not None,
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
187 specifies which letter the digit 1 should be mapped to (when map01 is not
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
188 None, the digit 0 is always mapped to the letter O). For security
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
189 purposes the default is None, so that 0 and 1 are not allowed in the
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
190 input.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
191
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
192 The decoded string is returned. A TypeError is raised if s were
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
193 incorrectly padded or if there are non-alphabet characters present in the
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
194 string.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
195 """
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
196 quanta, leftover = divmod(len(s), 8)
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
197 if leftover:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
198 raise TypeError('Incorrect padding')
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
199 # Handle section 2.4 zero and one mapping. The flag map01 will be either
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
200 # False, or the character to map the digit 1 (one) to. It should be
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
201 # either L (el) or I (eye).
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
202 if map01:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
203 s = _translate(s, {'0': 'O', '1': map01})
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
204 if casefold:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
205 s = s.upper()
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
206 # Strip off pad characters from the right. We need to count the pad
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
207 # characters because this will tell us how many null bytes to remove from
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
208 # the end of the decoded string.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
209 padchars = 0
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
210 mo = re.search('(?P<pad>[=]*)$', s)
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
211 if mo:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
212 padchars = len(mo.group('pad'))
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
213 if padchars > 0:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
214 s = s[:-padchars]
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
215 # Now decode the full quanta
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
216 parts = []
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
217 acc = 0
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
218 shift = 35
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
219 for c in s:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
220 val = _b32rev.get(c)
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
221 if val is None:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
222 raise TypeError('Non-base32 digit found')
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
223 acc += _b32rev[c] << shift
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
224 shift -= 5
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
225 if shift < 0:
35047
662488d4187d [Patch #1171487, bug #1170331] Fix error in base64.b32decode when encoding a single null byte; test a null byte in all encodings to be sure it works
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
226 parts.append(binascii.unhexlify('%010x' % acc))
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
227 acc = 0
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
228 shift = 35
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
229 # Process the last, partial quanta
35047
662488d4187d [Patch #1171487, bug #1170331] Fix error in base64.b32decode when encoding a single null byte; test a null byte in all encodings to be sure it works
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
230 last = binascii.unhexlify('%010x' % acc)
662488d4187d [Patch #1171487, bug #1170331] Fix error in base64.b32decode when encoding a single null byte; test a null byte in all encodings to be sure it works
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
231 if padchars == 0:
662488d4187d [Patch #1171487, bug #1170331] Fix error in base64.b32decode when encoding a single null byte; test a null byte in all encodings to be sure it works
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
232 last = '' # No characters
662488d4187d [Patch #1171487, bug #1170331] Fix error in base64.b32decode when encoding a single null byte; test a null byte in all encodings to be sure it works
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
233 elif padchars == 1:
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
234 last = last[:-1]
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
235 elif padchars == 3:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
236 last = last[:-2]
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
237 elif padchars == 4:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
238 last = last[:-3]
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
239 elif padchars == 6:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
240 last = last[:-4]
35047
662488d4187d [Patch #1171487, bug #1170331] Fix error in base64.b32decode when encoding a single null byte; test a null byte in all encodings to be sure it works
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
241 else:
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
242 raise TypeError('Incorrect padding')
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
243 parts.append(last)
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
244 return EMPTYSTRING.join(parts)
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
245
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
246
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
247
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
248 # RFC 3548, Base 16 Alphabet specifies uppercase, but hexlify() returns
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
249 # lowercase. The RFC also recommends against accepting input case
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
250 # insensitively.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
251 def b16encode(s):
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
252 """Encode a string using Base16.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
253
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
254 s is the string to encode. The encoded string is returned.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
255 """
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
256 return binascii.hexlify(s).upper()
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
257
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
258
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
259 def b16decode(s, casefold=False):
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
260 """Decode a Base16 encoded string.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
261
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
262 s is the string to decode. Optional casefold is a flag specifying whether
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
263 a lowercase alphabet is acceptable as input. For security purposes, the
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
264 default is False.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
265
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
266 The decoded string is returned. A TypeError is raised if s were
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
267 incorrectly padded or if there are non-alphabet characters present in the
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
268 string.
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
269 """
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
270 if casefold:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
271 s = s.upper()
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
272 if re.search('[^0-9A-F]', s):
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
273 raise TypeError('Non-base16 digit found')
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
274 return binascii.unhexlify(s)
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
275
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
276
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
277
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
278 # Legacy interface. This code could be cleaned up since I don't believe
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
279 # binascii has any line length limitations. It just doesn't seem worth it
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
280 # though.
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 13710
diff changeset
281
3145
43f8db23ef15 Use binascii module (resulting in a 60-fold speedup:-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 3104
diff changeset
282 MAXLINESIZE = 76 # Excluding the CRLF
19694
3716b116fb31 The first batch of changes recommended by the fixdiv tool. These are
Guido van Rossum <guido@python.org>
parents: 18053
diff changeset
283 MAXBINSIZE = (MAXLINESIZE//4)*3
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
284
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 def encode(input, output):
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
286 """Encode a file."""
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
287 while True:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
288 s = input.read(MAXBINSIZE)
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
289 if not s:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
290 break
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
291 while len(s) < MAXBINSIZE:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
292 ns = input.read(MAXBINSIZE-len(s))
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
293 if not ns:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
294 break
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
295 s += ns
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
296 line = binascii.b2a_base64(s)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
297 output.write(line)
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
298
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
299
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 def decode(input, output):
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
301 """Decode a file."""
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
302 while True:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
303 line = input.readline()
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
304 if not line:
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
305 break
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
306 s = binascii.a2b_base64(line)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
307 output.write(s)
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
308
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
309
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 def encodestring(s):
40204
fb9a7b288dce Clarify docstring
Andrew M. Kuchling <amk@amk.ca>
parents: 38464
diff changeset
311 """Encode a string into multiple lines of base-64 data."""
18053
2b017b2c8f7c check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 16070
diff changeset
312 pieces = []
2b017b2c8f7c check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 16070
diff changeset
313 for i in range(0, len(s), MAXBINSIZE):
2b017b2c8f7c check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 16070
diff changeset
314 chunk = s[i : i + MAXBINSIZE]
2b017b2c8f7c check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 16070
diff changeset
315 pieces.append(binascii.b2a_base64(chunk))
2b017b2c8f7c check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 16070
diff changeset
316 return "".join(pieces)
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
317
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
318
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 def decodestring(s):
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
320 """Decode a string."""
18053
2b017b2c8f7c check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 16070
diff changeset
321 return binascii.a2b_base64(s)
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
322
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
323
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
324
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
325 # Useable as a script...
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 def test():
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
327 """Small test program"""
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
328 import sys, getopt
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
329 try:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
330 opts, args = getopt.getopt(sys.argv[1:], 'deut')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
331 except getopt.error, msg:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
332 sys.stdout = sys.stderr
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
333 print msg
13710
697c88cc4714 a couple of nits from Patch #100933
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11691
diff changeset
334 print """usage: %s [-d|-e|-u|-t] [file|-]
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
335 -d, -u: decode
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
336 -e: encode (default)
13710
697c88cc4714 a couple of nits from Patch #100933
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11691
diff changeset
337 -t: encode and decode string 'Aladdin:open sesame'"""%sys.argv[0]
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
338 sys.exit(2)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
339 func = encode
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
340 for o, a in opts:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
341 if o == '-e': func = encode
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
342 if o == '-d': func = decode
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
343 if o == '-u': func = decode
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
344 if o == '-t': test1(); return
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
345 if args and args[0] != '-':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
346 func(open(args[0], 'rb'), sys.stdout)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
347 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
348 func(sys.stdin, sys.stdout)
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
349
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
350
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 def test1():
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
352 s0 = "Aladdin:open sesame"
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
353 s1 = encodestring(s0)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
354 s2 = decodestring(s1)
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31356
diff changeset
355 print s0, repr(s1), s2
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
356
31326
3cc0d8fd4e2b Added more complete RFC 3548 support for Base64, Base32, and Base16
Barry Warsaw <barry@python.org>
parents: 19694
diff changeset
357
2742
a827801ed963 two mime encoding schemes
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 if __name__ == '__main__':
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 11642
diff changeset
359 test()