39 lines
1.3 KiB
Python
39 lines
1.3 KiB
Python
|
|
import itertools
|
||
|
|
import string
|
||
|
|
from modes.hash_mode import HashMode
|
||
|
|
|
||
|
|
|
||
|
|
class CollisionsTest:
|
||
|
|
"""
|
||
|
|
Грубый поиск коллизий: перебираем строки до max_len
|
||
|
|
из заданного алфавита и проверяем, совпадут ли хэши.
|
||
|
|
"""
|
||
|
|
|
||
|
|
def run(self, variant: str, max_len: int = 3, alphabet: str = "alnum", bytes: int = 16) -> dict:
|
||
|
|
hasher = HashMode()
|
||
|
|
seen = {}
|
||
|
|
|
||
|
|
# выбор алфавита
|
||
|
|
if alphabet == "alnum":
|
||
|
|
chars = string.ascii_lowercase + string.digits
|
||
|
|
elif alphabet == "alpha":
|
||
|
|
chars = string.ascii_lowercase
|
||
|
|
elif alphabet == "digits":
|
||
|
|
chars = string.digits
|
||
|
|
else:
|
||
|
|
raise ValueError(f"Неизвестный алфавит: {alphabet}")
|
||
|
|
|
||
|
|
for length in range(1, max_len + 1):
|
||
|
|
for msg_tuple in itertools.product(chars, repeat=length):
|
||
|
|
msg = "".join(msg_tuple)
|
||
|
|
h = hasher.run(variant, msg, bytes=bytes)
|
||
|
|
if h in seen:
|
||
|
|
return {
|
||
|
|
"collision_found": True,
|
||
|
|
"hash": h,
|
||
|
|
"messages": [seen[h], msg],
|
||
|
|
}
|
||
|
|
seen[h] = msg
|
||
|
|
|
||
|
|
return {"collision_found": False}
|