blob: f90b91d994cfe9bd50d10ed7ccdef6711c64f015 [file] [log] [blame]
Guido van Rossumaa925a51997-04-02 05:47:39 +00001#! /usr/bin/env python
2
Barry Warsaw4c904d12004-01-04 01:12:26 +00003"""RFC 3548: Base16, Base32, Base64 Data Encodings"""
Guido van Rossum4acc25b2000-02-02 15:10:15 +00004
Barry Warsaw4c904d12004-01-04 01:12:26 +00005# Modified 04-Oct-1995 by Jack Jansen to use binascii module
6# Modified 30-Dec-2003 by Barry Warsaw to add full RFC 3548 support
Jack Jansen951213e1995-10-04 16:39:20 +00007
Barry Warsaw4c904d12004-01-04 01:12:26 +00008import re
9import struct
Jack Jansen951213e1995-10-04 16:39:20 +000010import binascii
11
Barry Warsaw4c904d12004-01-04 01:12:26 +000012
13__all__ = [
14 # Legacy interface exports traditional RFC 1521 Base64 encodings
15 'encode', 'decode', 'encodestring', 'decodestring',
16 # Generalized interface for other encodings
17 'b64encode', 'b64decode', 'b32encode', 'b32decode',
18 'b16encode', 'b16decode',
19 # Standard Base64 encoding
20 'standard_b64encode', 'standard_b64decode',
21 # Some common Base64 alternatives. As referenced by RFC 3458, see thread
22 # starting at:
23 #
24 # http://zgp.org/pipermail/p2p-hackers/2001-September/000316.html
Barry Warsaw4c904d12004-01-04 01:12:26 +000025 'urlsafe_b64encode', 'urlsafe_b64decode',
26 ]
27
28_translation = [chr(_x) for _x in range(256)]
29EMPTYSTRING = ''
30
31
32def _translate(s, altchars):
33 translation = _translation[:]
34 for k, v in altchars.items():
35 translation[ord(k)] = v
36 return s.translate(''.join(translation))
37
38
39
40# Base64 encoding/decoding uses binascii
41
42def b64encode(s, altchars=None):
43 """Encode a string using Base64.
44
45 s is the string to encode. Optional altchars must be a string of at least
46 length 2 (additional characters are ignored) which specifies an
47 alternative alphabet for the '+' and '/' characters. This allows an
48 application to e.g. generate url or filesystem safe Base64 strings.
49
50 The encoded string is returned.
51 """
52 # Strip off the trailing newline
53 encoded = binascii.b2a_base64(s)[:-1]
54 if altchars is not None:
55 return _translate(encoded, {'+': altchars[0], '/': altchars[1]})
56 return encoded
57
58
59def b64decode(s, altchars=None):
60 """Decode a Base64 encoded string.
61
62 s is the string to decode. Optional altchars must be a string of at least
63 length 2 (additional characters are ignored) which specifies the
64 alternative alphabet used instead of the '+' and '/' characters.
65
66 The decoded string is returned. A TypeError is raised if s were
67 incorrectly padded or if there are non-alphabet characters present in the
68 string.
69 """
70 if altchars is not None:
71 s = _translate(s, {altchars[0]: '+', altchars[1]: '/'})
72 try:
73 return binascii.a2b_base64(s)
74 except binascii.Error, msg:
75 # Transform this exception for consistency
76 raise TypeError(msg)
77
78
79def standard_b64encode(s):
80 """Encode a string using the standard Base64 alphabet.
81
82 s is the string to encode. The encoded string is returned.
83 """
84 return b64encode(s)
85
86def standard_b64decode(s):
87 """Decode a string encoded with the standard Base64 alphabet.
88
89 s is the string to decode. The decoded string is returned. A TypeError
90 is raised if the string is incorrectly padded or if there are non-alphabet
91 characters present in the string.
92 """
93 return b64decode(s)
94
95def urlsafe_b64encode(s):
96 """Encode a string using a url-safe Base64 alphabet.
97
98 s is the string to encode. The encoded string is returned. The alphabet
99 uses '-' instead of '+' and '_' instead of '/'.
100 """
101 return b64encode(s, '-_')
102
103def urlsafe_b64decode(s):
104 """Decode a string encoded with the standard Base64 alphabet.
105
106 s is the string to decode. The decoded string is returned. A TypeError
107 is raised if the string is incorrectly padded or if there are non-alphabet
108 characters present in the string.
109
110 The alphabet uses '-' instead of '+' and '_' instead of '/'.
111 """
112 return b64decode(s, '-_')
113
114
115
116# Base32 encoding/decoding must be done in Python
117_b32alphabet = {
118 0: 'A', 9: 'J', 18: 'S', 27: '3',
119 1: 'B', 10: 'K', 19: 'T', 28: '4',
120 2: 'C', 11: 'L', 20: 'U', 29: '5',
121 3: 'D', 12: 'M', 21: 'V', 30: '6',
122 4: 'E', 13: 'N', 22: 'W', 31: '7',
123 5: 'F', 14: 'O', 23: 'X',
124 6: 'G', 15: 'P', 24: 'Y',
125 7: 'H', 16: 'Q', 25: 'Z',
126 8: 'I', 17: 'R', 26: '2',
127 }
128
129_b32tab = [v for v in _b32alphabet.values()]
130_b32rev = dict([(v, long(k)) for k, v in _b32alphabet.items()])
131
132
133def b32encode(s):
134 """Encode a string using Base32.
135
136 s is the string to encode. The encoded string is returned.
137 """
138 parts = []
139 quanta, leftover = divmod(len(s), 5)
140 # Pad the last quantum with zero bits if necessary
141 if leftover:
142 s += ('\0' * (5 - leftover))
143 quanta += 1
144 for i in range(quanta):
145 # c1 and c2 are 16 bits wide, c3 is 8 bits wide. The intent of this
146 # code is to process the 40 bits in units of 5 bits. So we take the 1
147 # leftover bit of c1 and tack it onto c2. Then we take the 2 leftover
148 # bits of c2 and tack them onto c3. The shifts and masks are intended
149 # to give us values of exactly 5 bits in width.
150 c1, c2, c3 = struct.unpack('!HHB', s[i*5:(i+1)*5])
151 c2 += (c1 & 1) << 16 # 17 bits wide
152 c3 += (c2 & 3) << 8 # 10 bits wide
153 parts.extend([_b32tab[c1 >> 11], # bits 1 - 5
154 _b32tab[(c1 >> 6) & 0x1f], # bits 6 - 10
155 _b32tab[(c1 >> 1) & 0x1f], # bits 11 - 15
156 _b32tab[c2 >> 12], # bits 16 - 20 (1 - 5)
157 _b32tab[(c2 >> 7) & 0x1f], # bits 21 - 25 (6 - 10)
158 _b32tab[(c2 >> 2) & 0x1f], # bits 26 - 30 (11 - 15)
159 _b32tab[c3 >> 5], # bits 31 - 35 (1 - 5)
160 _b32tab[c3 & 0x1f], # bits 36 - 40 (1 - 5)
161 ])
162 encoded = EMPTYSTRING.join(parts)
163 # Adjust for any leftover partial quanta
164 if leftover == 1:
165 return encoded[:-6] + '======'
166 elif leftover == 2:
167 return encoded[:-4] + '===='
168 elif leftover == 3:
169 return encoded[:-3] + '==='
170 elif leftover == 4:
171 return encoded[:-1] + '='
172 return encoded
173
174
175def b32decode(s, casefold=False, map01=None):
176 """Decode a Base32 encoded string.
177
178 s is the string to decode. Optional casefold is a flag specifying whether
179 a lowercase alphabet is acceptable as input. For security purposes, the
180 default is False.
181
182 RFC 3548 allows for optional mapping of the digit 0 (zero) to the letter O
183 (oh), and for optional mapping of the digit 1 (one) to either the letter I
184 (eye) or letter L (el). The optional argument map01 when not None,
185 specifies which letter the digit 1 should be mapped to (when map01 is not
186 None, the digit 0 is always mapped to the letter O). For security
187 purposes the default is None, so that 0 and 1 are not allowed in the
188 input.
189
190 The decoded string is returned. A TypeError is raised if s were
191 incorrectly padded or if there are non-alphabet characters present in the
192 string.
193 """
194 quanta, leftover = divmod(len(s), 8)
195 if leftover:
196 raise TypeError('Incorrect padding')
197 # Handle section 2.4 zero and one mapping. The flag map01 will be either
198 # False, or the character to map the digit 1 (one) to. It should be
199 # either L (el) or I (eye).
200 if map01:
201 s = _translate(s, {'0': 'O', '1': map01})
202 if casefold:
203 s = s.upper()
204 # Strip off pad characters from the right. We need to count the pad
205 # characters because this will tell us how many null bytes to remove from
206 # the end of the decoded string.
207 padchars = 0
208 mo = re.search('(?P<pad>[=]*)$', s)
209 if mo:
210 padchars = len(mo.group('pad'))
211 if padchars > 0:
212 s = s[:-padchars]
213 # Now decode the full quanta
214 parts = []
215 acc = 0
216 shift = 35
217 for c in s:
218 val = _b32rev.get(c)
219 if val is None:
220 raise TypeError('Non-base32 digit found')
221 acc += _b32rev[c] << shift
222 shift -= 5
223 if shift < 0:
224 parts.append(binascii.unhexlify(hex(acc)[2:-1]))
225 acc = 0
226 shift = 35
227 # Process the last, partial quanta
228 last = binascii.unhexlify(hex(acc)[2:-1])
229 if padchars == 1:
230 last = last[:-1]
231 elif padchars == 3:
232 last = last[:-2]
233 elif padchars == 4:
234 last = last[:-3]
235 elif padchars == 6:
236 last = last[:-4]
237 elif padchars <> 0:
238 raise TypeError('Incorrect padding')
239 parts.append(last)
240 return EMPTYSTRING.join(parts)
241
242
243
244# RFC 3548, Base 16 Alphabet specifies uppercase, but hexlify() returns
245# lowercase. The RFC also recommends against accepting input case
246# insensitively.
247def b16encode(s):
248 """Encode a string using Base16.
249
250 s is the string to encode. The encoded string is returned.
251 """
252 return binascii.hexlify(s).upper()
253
254
255def b16decode(s, casefold=False):
256 """Decode a Base16 encoded string.
257
258 s is the string to decode. Optional casefold is a flag specifying whether
259 a lowercase alphabet is acceptable as input. For security purposes, the
260 default is False.
261
262 The decoded string is returned. A TypeError is raised if s were
263 incorrectly padded or if there are non-alphabet characters present in the
264 string.
265 """
266 if casefold:
267 s = s.upper()
268 if re.search('[^0-9A-F]', s):
269 raise TypeError('Non-base16 digit found')
270 return binascii.unhexlify(s)
271
272
273
274# Legacy interface. This code could be cleaned up since I don't believe
275# binascii has any line length limitations. It just doesn't seem worth it
276# though.
Skip Montanaroe99d5ea2001-01-20 19:54:20 +0000277
Guido van Rossumf1945461995-06-14 23:43:44 +0000278MAXLINESIZE = 76 # Excluding the CRLF
Guido van Rossum54e54c62001-09-04 19:14:14 +0000279MAXBINSIZE = (MAXLINESIZE//4)*3
Guido van Rossumf1945461995-06-14 23:43:44 +0000280
Guido van Rossumf1945461995-06-14 23:43:44 +0000281def encode(input, output):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000282 """Encode a file."""
Barry Warsaw4c904d12004-01-04 01:12:26 +0000283 while True:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000284 s = input.read(MAXBINSIZE)
Barry Warsaw4c904d12004-01-04 01:12:26 +0000285 if not s:
286 break
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000287 while len(s) < MAXBINSIZE:
288 ns = input.read(MAXBINSIZE-len(s))
Barry Warsaw4c904d12004-01-04 01:12:26 +0000289 if not ns:
290 break
291 s += ns
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000292 line = binascii.b2a_base64(s)
293 output.write(line)
Guido van Rossumf1945461995-06-14 23:43:44 +0000294
Barry Warsaw4c904d12004-01-04 01:12:26 +0000295
Guido van Rossumf1945461995-06-14 23:43:44 +0000296def decode(input, output):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000297 """Decode a file."""
Barry Warsaw4c904d12004-01-04 01:12:26 +0000298 while True:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000299 line = input.readline()
Barry Warsaw4c904d12004-01-04 01:12:26 +0000300 if not line:
301 break
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000302 s = binascii.a2b_base64(line)
303 output.write(s)
Guido van Rossumf1945461995-06-14 23:43:44 +0000304
Barry Warsaw4c904d12004-01-04 01:12:26 +0000305
Guido van Rossumf1945461995-06-14 23:43:44 +0000306def encodestring(s):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000307 """Encode a string."""
Peter Schneider-Kampfbb2b4c2001-06-07 18:56:13 +0000308 pieces = []
309 for i in range(0, len(s), MAXBINSIZE):
310 chunk = s[i : i + MAXBINSIZE]
311 pieces.append(binascii.b2a_base64(chunk))
312 return "".join(pieces)
Guido van Rossumf1945461995-06-14 23:43:44 +0000313
Barry Warsaw4c904d12004-01-04 01:12:26 +0000314
Guido van Rossumf1945461995-06-14 23:43:44 +0000315def decodestring(s):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000316 """Decode a string."""
Peter Schneider-Kampfbb2b4c2001-06-07 18:56:13 +0000317 return binascii.a2b_base64(s)
Guido van Rossumf1945461995-06-14 23:43:44 +0000318
Barry Warsaw4c904d12004-01-04 01:12:26 +0000319
320
321# Useable as a script...
Guido van Rossumf1945461995-06-14 23:43:44 +0000322def test():
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000323 """Small test program"""
324 import sys, getopt
325 try:
326 opts, args = getopt.getopt(sys.argv[1:], 'deut')
327 except getopt.error, msg:
328 sys.stdout = sys.stderr
329 print msg
Jeremy Hylton03651802000-07-25 14:34:38 +0000330 print """usage: %s [-d|-e|-u|-t] [file|-]
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000331 -d, -u: decode
332 -e: encode (default)
Jeremy Hylton03651802000-07-25 14:34:38 +0000333 -t: encode and decode string 'Aladdin:open sesame'"""%sys.argv[0]
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000334 sys.exit(2)
335 func = encode
336 for o, a in opts:
337 if o == '-e': func = encode
338 if o == '-d': func = decode
339 if o == '-u': func = decode
340 if o == '-t': test1(); return
341 if args and args[0] != '-':
342 func(open(args[0], 'rb'), sys.stdout)
343 else:
344 func(sys.stdin, sys.stdout)
Guido van Rossumf1945461995-06-14 23:43:44 +0000345
Barry Warsaw4c904d12004-01-04 01:12:26 +0000346
Guido van Rossumf1945461995-06-14 23:43:44 +0000347def test1():
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000348 s0 = "Aladdin:open sesame"
349 s1 = encodestring(s0)
350 s2 = decodestring(s1)
Walter Dörwald70a6b492004-02-12 17:35:32 +0000351 print s0, repr(s1), s2
Guido van Rossumf1945461995-06-14 23:43:44 +0000352
Barry Warsaw4c904d12004-01-04 01:12:26 +0000353
Guido van Rossumf1945461995-06-14 23:43:44 +0000354if __name__ == '__main__':
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000355 test()