def rol7_hash(name): x = 0 for c in name: x = rol(x, 7) & 0xffffffff x += ord(c) x &= 0xffffffff return x
def rolling_xor(d, key, rl=8, off=0, add_index=False, xor_index=False): r = [] for i, c in enumerate(d): a = ord(c) ^ key ^ (i if xor_index else 0) r.append(chr(a & 0xff)) key = rol(key, rl) + off if add_index: key += i return ''.join(r)
def __init__(self, key): self.state = S = [] key += "\0" * (4 - len(key) & 3) # pad key L = list(struct.unpack("<%sL" % (len(key) / 4), key)) S.append(0xb7e15163) for i in range(43): S.append(_add(S[i], 0x9e3779b9)) v = max(132, len(L) * 3) A = B = i = j = 0 for n in range(v): A = S[i] = rol(_add(S[i], A, B), 3) B = L[j] = rol(_add(L[j] + A + B), _add(A + B)) i = (i + 1) % len(S) j = (j + 1) % len(L)
def decrypt(self, block,state = None): S = state if state else self.state A, B, C, D = struct.unpack("<4L", block.ljust(16,"\0"))# * 16) C = _add(C, -S[43]) A = _add(A, -S[42]) for i in range(20,0,-1): # 20..1 A, B, C, D = D, A, B, C u = rol(_mul(D, _add(rol(D, 1) | 1)), 5) t = rol(_mul(B, _add(rol(B, 1) | 1)), 5) C = ror(_add(C, -S[2 * i + 1]), t) ^ u A = ror(_add(A, -S[2 * i]), u) ^ t D = _add(D, -S[1]) B = _add(B, -S[0]) return struct.pack("<4L", A&0xffffffff, B&0xffffffff, C&0xffffffff, D&0xffffffff)#[A,B,C,D]
def encrypt(self, block, state=None): S = state if state else self.state A, B, C, D = struct.unpack("<4L", block.ljust(16, '\0')) B = _add(B, S[0]) D = _add(D, S[1]) for i in range(1, 21): # 1..20 t = rol(_mul(B, rol(B, 1) | 1), 5) u = rol(_mul(D, rol(D, 1) | 1), 5) A = _add(rol(A ^ t, u), S[2 * i]) C = _add(rol(C ^ u, t), S[2 * i + 1]) A, B, C, D = B, C, D, A A = _add(A, S[42]) C = _add(C, S[43]) return struct.pack("<4L", A, B, C, D)
def ROL16(x, n): return rol(x, n, 16) & 0xFFFF
def mlwr_hash(name): x = 0 for c in name: x = rol(x, 7) x = (x ^ ord(c)) & 0xff | x & 0xffffff00 return x
def std_hash(name): x = 0 for c in name: x = (rol(x, 19) + ord(c)) & 0xffffffff return x
def ROTL32(*args): return rol(*args) & 0xffffffff