base64.py 12 KB

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