45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
|
|
import string
|
||
|
|
import random
|
||
|
|
from modes.hash_mode import HashMode
|
||
|
|
|
||
|
|
|
||
|
|
class UniformityAnalyzer:
|
||
|
|
"""
|
||
|
|
Анализ равномерности распределения хэшей.
|
||
|
|
"""
|
||
|
|
|
||
|
|
def run(self, variant: str, count: int = 100, buckets: int = 10, bytes: int = 16) -> dict:
|
||
|
|
hasher = HashMode()
|
||
|
|
alphabet = string.ascii_lowercase + string.digits
|
||
|
|
|
||
|
|
# случайные сообщения
|
||
|
|
messages = [
|
||
|
|
"".join(random.choice(alphabet)
|
||
|
|
for _ in range(random.randint(5, 10)))
|
||
|
|
for _ in range(count)
|
||
|
|
]
|
||
|
|
|
||
|
|
# хэши
|
||
|
|
hashes = [hasher.run(variant, msg, bytes=bytes) for msg in messages]
|
||
|
|
|
||
|
|
# берём первые 8 hex-символов → число
|
||
|
|
values = [int(h[:8], 16) for h in hashes]
|
||
|
|
values.sort()
|
||
|
|
|
||
|
|
# диапазоны
|
||
|
|
min_val, max_val = values[0], values[-1]
|
||
|
|
step = (max_val - min_val) / buckets if max_val > min_val else 1
|
||
|
|
|
||
|
|
distribution = [0] * buckets
|
||
|
|
for v in values:
|
||
|
|
idx = min(int((v - min_val) / step), buckets - 1)
|
||
|
|
distribution[idx] += 1
|
||
|
|
|
||
|
|
return {
|
||
|
|
"count": count,
|
||
|
|
"buckets": buckets,
|
||
|
|
"messages": messages,
|
||
|
|
"hashes": hashes,
|
||
|
|
"distribution": distribution,
|
||
|
|
}
|