Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- srp_base64_table = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./"
- def srpb64decode(s):
- ints = [srp_base64_table.index(c) for c in s]
- pad = len(ints) % 4
- if pad != 0:
- pad = 4 - pad
- ints = [0 for i in range(0, pad)] + ints
- notleading = False
- buf = []
- pos = 0
- while pos < len(ints):
- b = (ints[pos] << 2) | ((ints[pos+1] & 0x30) >> 4)
- if notleading or b != 0:
- buf.append(b)
- notleading = True
- b = ((ints[pos+1] & 0x0f) << 4) | ((ints[pos+2] & 0x3c) >> 2)
- if notleading or b != 0:
- buf.append(b)
- notleading = True
- b = ((ints[pos+2] & 0x03) << 6) | ints[pos+3]
- if notleading or b != 0:
- buf.append(b)
- notleading = True
- pos += 4
- return bytes(buf)
- def srpb64encode(b):
- pos = len(b) % 3
- b0 = 0
- b1 = 0
- b2 = 0
- notleading = False
- buf = ""
- if pos == 1:
- b2 = b[0]
- elif pos == 2:
- b1 = b[0]
- b2 = b[1]
- while True:
- c = (b0 & 0xfc) >> 2
- if notleading or c != 0:
- buf += srp_base64_table[c]
- notleading = True
- c = ((b0 & 3) << 4) | ((b1 & 0xf0) >> 4)
- if notleading or c != 0:
- buf += srp_base64_table[c]
- notleading = True
- c = ((b1 & 0xf) << 2) | ((b2 & 0xc0) >> 6)
- if notleading or c != 0:
- buf += srp_base64_table[c]
- notleading = True
- c = b2 & 0x3f
- if notleading or c != 0:
- buf += srp_base64_table[c]
- notleading = True
- if pos >= len(b):
- break
- b0 = b[pos]
- b1 = b[pos + 1]
- b2 = b[pos + 2]
- pos += 3
- return buf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement