IV = 0b01111000011000010111001101101000

def comp(a,b):
    return a ^ b

def xd (s):
    a = IV
    i = 0
    rest = len(s) % 4
    padding = "\0" * (4 - rest) if rest else ""
    s = s + padding
    while i < len(s):
        A = ord(s[i])   << 24
        B = ord(s[i+1]) << 16
        C = ord(s[i+2]) << 8
        D = ord(s[i+3])
        a = comp (a, A+B+C+D)
        i = i + 4
    return hex(a)[2:]

assert xd ("ESIREM") == "787f3a3a"
assert xd ("ESIREM!!") == "787f1b1b"
assert xd ("") == "78617368"

### TODO:
##  * Find collisions
##  * Find first and second preimage
##  * Explain why there is no avalanche effect ?
##  * Imporve the padding method.

## uniformity test
import random, string
N = 31
def rs():
  return ''.join(random.choices(string.printable, k=N))

LIST = []
for i in range (100000):
  hash = xd (rs())
  dl = hash[-2:]
  LIST.append (dl)

import matplotlib.pyplot as plt
LIST = sorted(LIST)
plt.hist (LIST, bins = 400)
plt.title ("Distribution of last hash symbols")
plt.show ()
