base64.py 11 KB

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