repl.it
Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from sympy import divisors

collides = set()


def badhash(number: int, rounds=15, size: int = 0xFFFFFFFFFF):
    if not (rounds % 2):
        rounds += 1

    for i in range(rounds):
        d = divisors(number)
        for e, div in enumerate(d):
            number ^= (div ^ (number // div)) * e
        number *= sum(d)
        number ^= size
        number &= size
    return number >> 4


class BadHash:

    def __init__(self):
        self._hashed = 0

    def update(self, data: bytes):
        for byte in data:
            self._hashed = badhash(data[0] + self._hashed)
    
    def hexdigest(self):
        return hex(self._hashed)[2:]


bh = BadHash()
bh.update(b"Hello World!")
print(bh.hexdigest())
bh = BadHash()
bh.update(b"Hello World!!")
print(bh.hexdigest())


# for i in range(2, 5000):
#     bh = badhash(i)
#     print(i, hex(bh).upper()[2:])
#     if bh in collides:
#         print("Collided:", hex(bh).upper())
#     collides.add(bh)

# print("Collided {} times".format((i - 1) - len(collides)))
?