Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. srp_base64_table = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./"
  2.  
  3. def srpb64decode(s):
  4.     ints = [srp_base64_table.index(c) for c in s]
  5.     pad = len(ints) % 4
  6.     if pad != 0:
  7.         pad = 4 - pad
  8.     ints = [0 for i in range(0, pad)] + ints
  9.     notleading = False
  10.     buf = []
  11.  
  12.     pos = 0
  13.     while pos < len(ints):
  14.         b = (ints[pos] << 2) | ((ints[pos+1] & 0x30) >> 4)
  15.         if notleading or b != 0:
  16.             buf.append(b)
  17.             notleading = True
  18.         b = ((ints[pos+1] & 0x0f) << 4) | ((ints[pos+2] & 0x3c) >> 2)
  19.         if notleading or b != 0:
  20.             buf.append(b)
  21.             notleading = True
  22.         b = ((ints[pos+2] & 0x03) << 6) | ints[pos+3]
  23.         if notleading or b != 0:
  24.             buf.append(b)
  25.             notleading = True
  26.         pos += 4
  27.  
  28.     return bytes(buf)
  29.  
  30. def srpb64encode(b):
  31.     pos = len(b) % 3
  32.     b0 = 0
  33.     b1 = 0
  34.     b2 = 0
  35.     notleading = False
  36.     buf = ""
  37.  
  38.     if pos == 1:
  39.         b2 = b[0]
  40.     elif pos == 2:
  41.         b1 = b[0]
  42.         b2 = b[1]
  43.  
  44.     while True:
  45.         c = (b0 & 0xfc) >> 2
  46.         if notleading or c != 0:
  47.             buf += srp_base64_table[c]
  48.             notleading = True
  49.         c = ((b0 & 3) << 4) | ((b1 & 0xf0) >> 4)
  50.         if notleading or c != 0:
  51.             buf += srp_base64_table[c]
  52.             notleading = True
  53.         c = ((b1 & 0xf) << 2) | ((b2 & 0xc0) >> 6)
  54.         if notleading or c != 0:
  55.             buf += srp_base64_table[c]
  56.             notleading = True
  57.         c = b2 & 0x3f
  58.         if notleading or c != 0:
  59.             buf += srp_base64_table[c]
  60.             notleading = True
  61.         if pos >= len(b):
  62.             break
  63.         b0 = b[pos]
  64.         b1 = b[pos + 1]
  65.         b2 = b[pos + 2]
  66.         pos += 3
  67.  
  68.     return buf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement