blob: 844907feef56ae0e2fca5e106a8f7ac362b64feb [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
Guido van Rossum3d731c52015-07-30 16:50:25 +020010import string
Jack Jansen951213e1995-10-04 16:39:20 +000011import binascii
12
Barry Warsaw4c904d12004-01-04 01:12:26 +000013
14__all__ = [
15 # Legacy interface exports traditional RFC 1521 Base64 encodings
16 'encode', 'decode', 'encodestring', 'decodestring',
17 # Generalized interface for other encodings
18 'b64encode', 'b64decode', 'b32encode', 'b32decode',
19 'b16encode', 'b16decode',
20 # Standard Base64 encoding
21 'standard_b64encode', 'standard_b64decode',
22 # Some common Base64 alternatives. As referenced by RFC 3458, see thread
23 # starting at:
24 #
25 # http://zgp.org/pipermail/p2p-hackers/2001-September/000316.html
Barry Warsaw4c904d12004-01-04 01:12:26 +000026 'urlsafe_b64encode', 'urlsafe_b64decode',
27 ]
28
29_translation = [chr(_x) for _x in range(256)]
30EMPTYSTRING = ''
31
32
33def _translate(s, altchars):
34 translation = _translation[:]
35 for k, v in altchars.items():
36 translation[ord(k)] = v
37 return s.translate(''.join(translation))
38
39
40
41# Base64 encoding/decoding uses binascii
42
43def b64encode(s, altchars=None):
44 """Encode a string using Base64.
45
46 s is the string to encode. Optional altchars must be a string of at least
47 length 2 (additional characters are ignored) which specifies an
48 alternative alphabet for the '+' and '/' characters. This allows an
49 application to e.g. generate url or filesystem safe Base64 strings.
50
51 The encoded string is returned.
52 """
53 # Strip off the trailing newline
54 encoded = binascii.b2a_base64(s)[:-1]
55 if altchars is not None:
Guido van Rossum3d731c52015-07-30 16:50:25 +020056 return encoded.translate(string.maketrans(b'+/', altchars[:2]))
Barry Warsaw4c904d12004-01-04 01:12:26 +000057 return encoded
58
59
60def b64decode(s, altchars=None):
61 """Decode a Base64 encoded string.
62
63 s is the string to decode. Optional altchars must be a string of at least
64 length 2 (additional characters are ignored) which specifies the
65 alternative alphabet used instead of the '+' and '/' characters.
66
Martin Panterd77fe942015-12-14 03:41:59 +000067 The decoded string is returned. A TypeError is raised if s is
68 incorrectly padded. Non-base64-alphabet characters are discarded prior
69 to the padding check.
Barry Warsaw4c904d12004-01-04 01:12:26 +000070 """
71 if altchars is not None:
Guido van Rossum3d731c52015-07-30 16:50:25 +020072 s = s.translate(string.maketrans(altchars[:2], '+/'))
Barry Warsaw4c904d12004-01-04 01:12:26 +000073 try:
74 return binascii.a2b_base64(s)
75 except binascii.Error, msg:
76 # Transform this exception for consistency
77 raise TypeError(msg)
78
79
80def standard_b64encode(s):
81 """Encode a string using the standard Base64 alphabet.
82
83 s is the string to encode. The encoded string is returned.
84 """
85 return b64encode(s)
86
87def standard_b64decode(s):
88 """Decode a string encoded with the standard Base64 alphabet.
89
90 s is the string to decode. The decoded string is returned. A TypeError
91 is raised if the string is incorrectly padded or if there are non-alphabet
92 characters present in the string.
93 """
94 return b64decode(s)
95
Guido van Rossum3d731c52015-07-30 16:50:25 +020096_urlsafe_encode_translation = string.maketrans(b'+/', b'-_')
97_urlsafe_decode_translation = string.maketrans(b'-_', b'+/')
98
Barry Warsaw4c904d12004-01-04 01:12:26 +000099def urlsafe_b64encode(s):
100 """Encode a string using a url-safe Base64 alphabet.
101
102 s is the string to encode. The encoded string is returned. The alphabet
103 uses '-' instead of '+' and '_' instead of '/'.
104 """
Guido van Rossum3d731c52015-07-30 16:50:25 +0200105 return b64encode(s).translate(_urlsafe_encode_translation)
Barry Warsaw4c904d12004-01-04 01:12:26 +0000106
107def urlsafe_b64decode(s):
108 """Decode a string encoded with the standard Base64 alphabet.
109
110 s is the string to decode. The decoded string is returned. A TypeError
111 is raised if the string is incorrectly padded or if there are non-alphabet
112 characters present in the string.
113
114 The alphabet uses '-' instead of '+' and '_' instead of '/'.
115 """
Guido van Rossum3d731c52015-07-30 16:50:25 +0200116 return b64decode(s.translate(_urlsafe_decode_translation))
Barry Warsaw4c904d12004-01-04 01:12:26 +0000117
118
119
120# Base32 encoding/decoding must be done in Python
121_b32alphabet = {
122 0: 'A', 9: 'J', 18: 'S', 27: '3',
123 1: 'B', 10: 'K', 19: 'T', 28: '4',
124 2: 'C', 11: 'L', 20: 'U', 29: '5',
125 3: 'D', 12: 'M', 21: 'V', 30: '6',
126 4: 'E', 13: 'N', 22: 'W', 31: '7',
127 5: 'F', 14: 'O', 23: 'X',
128 6: 'G', 15: 'P', 24: 'Y',
129 7: 'H', 16: 'Q', 25: 'Z',
130 8: 'I', 17: 'R', 26: '2',
131 }
132
Armin Rigoa3f09272006-05-28 19:13:17 +0000133_b32tab = _b32alphabet.items()
134_b32tab.sort()
135_b32tab = [v for k, v in _b32tab]
Barry Warsaw4c904d12004-01-04 01:12:26 +0000136_b32rev = dict([(v, long(k)) for k, v in _b32alphabet.items()])
137
138
139def b32encode(s):
140 """Encode a string using Base32.
141
142 s is the string to encode. The encoded string is returned.
143 """
144 parts = []
145 quanta, leftover = divmod(len(s), 5)
146 # Pad the last quantum with zero bits if necessary
147 if leftover:
148 s += ('\0' * (5 - leftover))
149 quanta += 1
150 for i in range(quanta):
151 # c1 and c2 are 16 bits wide, c3 is 8 bits wide. The intent of this
152 # code is to process the 40 bits in units of 5 bits. So we take the 1
153 # leftover bit of c1 and tack it onto c2. Then we take the 2 leftover
154 # bits of c2 and tack them onto c3. The shifts and masks are intended
155 # to give us values of exactly 5 bits in width.
156 c1, c2, c3 = struct.unpack('!HHB', s[i*5:(i+1)*5])
157 c2 += (c1 & 1) << 16 # 17 bits wide
158 c3 += (c2 & 3) << 8 # 10 bits wide
159 parts.extend([_b32tab[c1 >> 11], # bits 1 - 5
160 _b32tab[(c1 >> 6) & 0x1f], # bits 6 - 10
161 _b32tab[(c1 >> 1) & 0x1f], # bits 11 - 15
162 _b32tab[c2 >> 12], # bits 16 - 20 (1 - 5)
163 _b32tab[(c2 >> 7) & 0x1f], # bits 21 - 25 (6 - 10)
164 _b32tab[(c2 >> 2) & 0x1f], # bits 26 - 30 (11 - 15)
165 _b32tab[c3 >> 5], # bits 31 - 35 (1 - 5)
166 _b32tab[c3 & 0x1f], # bits 36 - 40 (1 - 5)
167 ])
168 encoded = EMPTYSTRING.join(parts)
169 # Adjust for any leftover partial quanta
170 if leftover == 1:
171 return encoded[:-6] + '======'
172 elif leftover == 2:
173 return encoded[:-4] + '===='
174 elif leftover == 3:
175 return encoded[:-3] + '==='
176 elif leftover == 4:
177 return encoded[:-1] + '='
178 return encoded
179
180
181def b32decode(s, casefold=False, map01=None):
182 """Decode a Base32 encoded string.
183
184 s is the string to decode. Optional casefold is a flag specifying whether
185 a lowercase alphabet is acceptable as input. For security purposes, the
186 default is False.
187
188 RFC 3548 allows for optional mapping of the digit 0 (zero) to the letter O
189 (oh), and for optional mapping of the digit 1 (one) to either the letter I
190 (eye) or letter L (el). The optional argument map01 when not None,
191 specifies which letter the digit 1 should be mapped to (when map01 is not
192 None, the digit 0 is always mapped to the letter O). For security
193 purposes the default is None, so that 0 and 1 are not allowed in the
194 input.
195
196 The decoded string is returned. A TypeError is raised if s were
197 incorrectly padded or if there are non-alphabet characters present in the
198 string.
199 """
200 quanta, leftover = divmod(len(s), 8)
201 if leftover:
202 raise TypeError('Incorrect padding')
203 # Handle section 2.4 zero and one mapping. The flag map01 will be either
204 # False, or the character to map the digit 1 (one) to. It should be
205 # either L (el) or I (eye).
206 if map01:
Guido van Rossum3d731c52015-07-30 16:50:25 +0200207 s = s.translate(string.maketrans(b'01', b'O' + map01))
Barry Warsaw4c904d12004-01-04 01:12:26 +0000208 if casefold:
209 s = s.upper()
210 # Strip off pad characters from the right. We need to count the pad
211 # characters because this will tell us how many null bytes to remove from
212 # the end of the decoded string.
213 padchars = 0
214 mo = re.search('(?P<pad>[=]*)$', s)
215 if mo:
216 padchars = len(mo.group('pad'))
217 if padchars > 0:
218 s = s[:-padchars]
219 # Now decode the full quanta
220 parts = []
221 acc = 0
222 shift = 35
223 for c in s:
224 val = _b32rev.get(c)
225 if val is None:
226 raise TypeError('Non-base32 digit found')
227 acc += _b32rev[c] << shift
228 shift -= 5
229 if shift < 0:
Andrew M. Kuchling6e57c2a2005-06-08 22:51:38 +0000230 parts.append(binascii.unhexlify('%010x' % acc))
Barry Warsaw4c904d12004-01-04 01:12:26 +0000231 acc = 0
232 shift = 35
233 # Process the last, partial quanta
Andrew M. Kuchling6e57c2a2005-06-08 22:51:38 +0000234 last = binascii.unhexlify('%010x' % acc)
235 if padchars == 0:
236 last = '' # No characters
237 elif padchars == 1:
Barry Warsaw4c904d12004-01-04 01:12:26 +0000238 last = last[:-1]
239 elif padchars == 3:
240 last = last[:-2]
241 elif padchars == 4:
242 last = last[:-3]
243 elif padchars == 6:
244 last = last[:-4]
Andrew M. Kuchling6e57c2a2005-06-08 22:51:38 +0000245 else:
Barry Warsaw4c904d12004-01-04 01:12:26 +0000246 raise TypeError('Incorrect padding')
247 parts.append(last)
248 return EMPTYSTRING.join(parts)
249
250
251
252# RFC 3548, Base 16 Alphabet specifies uppercase, but hexlify() returns
253# lowercase. The RFC also recommends against accepting input case
254# insensitively.
255def b16encode(s):
256 """Encode a string using Base16.
257
258 s is the string to encode. The encoded string is returned.
259 """
260 return binascii.hexlify(s).upper()
261
262
263def b16decode(s, casefold=False):
264 """Decode a Base16 encoded string.
265
266 s is the string to decode. Optional casefold is a flag specifying whether
267 a lowercase alphabet is acceptable as input. For security purposes, the
268 default is False.
269
270 The decoded string is returned. A TypeError is raised if s were
271 incorrectly padded or if there are non-alphabet characters present in the
272 string.
273 """
274 if casefold:
275 s = s.upper()
276 if re.search('[^0-9A-F]', s):
277 raise TypeError('Non-base16 digit found')
278 return binascii.unhexlify(s)
279
280
281
282# Legacy interface. This code could be cleaned up since I don't believe
283# binascii has any line length limitations. It just doesn't seem worth it
284# though.
Skip Montanaroe99d5ea2001-01-20 19:54:20 +0000285
Guido van Rossumf1945461995-06-14 23:43:44 +0000286MAXLINESIZE = 76 # Excluding the CRLF
Guido van Rossum54e54c62001-09-04 19:14:14 +0000287MAXBINSIZE = (MAXLINESIZE//4)*3
Guido van Rossumf1945461995-06-14 23:43:44 +0000288
Guido van Rossumf1945461995-06-14 23:43:44 +0000289def encode(input, output):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000290 """Encode a file."""
Barry Warsaw4c904d12004-01-04 01:12:26 +0000291 while True:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000292 s = input.read(MAXBINSIZE)
Barry Warsaw4c904d12004-01-04 01:12:26 +0000293 if not s:
294 break
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000295 while len(s) < MAXBINSIZE:
296 ns = input.read(MAXBINSIZE-len(s))
Barry Warsaw4c904d12004-01-04 01:12:26 +0000297 if not ns:
298 break
299 s += ns
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000300 line = binascii.b2a_base64(s)
301 output.write(line)
Guido van Rossumf1945461995-06-14 23:43:44 +0000302
Barry Warsaw4c904d12004-01-04 01:12:26 +0000303
Guido van Rossumf1945461995-06-14 23:43:44 +0000304def decode(input, output):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000305 """Decode a file."""
Barry Warsaw4c904d12004-01-04 01:12:26 +0000306 while True:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000307 line = input.readline()
Barry Warsaw4c904d12004-01-04 01:12:26 +0000308 if not line:
309 break
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000310 s = binascii.a2b_base64(line)
311 output.write(s)
Guido van Rossumf1945461995-06-14 23:43:44 +0000312
Barry Warsaw4c904d12004-01-04 01:12:26 +0000313
Guido van Rossumf1945461995-06-14 23:43:44 +0000314def encodestring(s):
Andrew M. Kuchling6d72b0e2006-10-27 17:06:16 +0000315 """Encode a string into multiple lines of base-64 data."""
Peter Schneider-Kampfbb2b4c2001-06-07 18:56:13 +0000316 pieces = []
317 for i in range(0, len(s), MAXBINSIZE):
318 chunk = s[i : i + MAXBINSIZE]
319 pieces.append(binascii.b2a_base64(chunk))
320 return "".join(pieces)
Guido van Rossumf1945461995-06-14 23:43:44 +0000321
Barry Warsaw4c904d12004-01-04 01:12:26 +0000322
Guido van Rossumf1945461995-06-14 23:43:44 +0000323def decodestring(s):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000324 """Decode a string."""
Peter Schneider-Kampfbb2b4c2001-06-07 18:56:13 +0000325 return binascii.a2b_base64(s)
Guido van Rossumf1945461995-06-14 23:43:44 +0000326
Barry Warsaw4c904d12004-01-04 01:12:26 +0000327
328
329# Useable as a script...
Guido van Rossumf1945461995-06-14 23:43:44 +0000330def test():
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000331 """Small test program"""
332 import sys, getopt
333 try:
334 opts, args = getopt.getopt(sys.argv[1:], 'deut')
335 except getopt.error, msg:
336 sys.stdout = sys.stderr
337 print msg
Jeremy Hylton03651802000-07-25 14:34:38 +0000338 print """usage: %s [-d|-e|-u|-t] [file|-]
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000339 -d, -u: decode
340 -e: encode (default)
Jeremy Hylton03651802000-07-25 14:34:38 +0000341 -t: encode and decode string 'Aladdin:open sesame'"""%sys.argv[0]
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000342 sys.exit(2)
343 func = encode
344 for o, a in opts:
345 if o == '-e': func = encode
346 if o == '-d': func = decode
347 if o == '-u': func = decode
348 if o == '-t': test1(); return
349 if args and args[0] != '-':
Antoine Pitrouf7c24452010-10-14 21:22:52 +0000350 with open(args[0], 'rb') as f:
351 func(f, sys.stdout)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000352 else:
353 func(sys.stdin, sys.stdout)
Guido van Rossumf1945461995-06-14 23:43:44 +0000354
Barry Warsaw4c904d12004-01-04 01:12:26 +0000355
Guido van Rossumf1945461995-06-14 23:43:44 +0000356def test1():
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000357 s0 = "Aladdin:open sesame"
358 s1 = encodestring(s0)
359 s2 = decodestring(s1)
Walter Dörwald70a6b492004-02-12 17:35:32 +0000360 print s0, repr(s1), s2
Guido van Rossumf1945461995-06-14 23:43:44 +0000361
Barry Warsaw4c904d12004-01-04 01:12:26 +0000362
Guido van Rossumf1945461995-06-14 23:43:44 +0000363if __name__ == '__main__':
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000364 test()