-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavalanche_analysis.py
More file actions
33 lines (24 loc) · 1.01 KB
/
avalanche_analysis.py
File metadata and controls
33 lines (24 loc) · 1.01 KB
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
from hash_visualizer import compute_hash, hex_to_bin
import numpy as np
import matplotlib.pyplot as plt
def avalanche_score(msg1: str, msg2: str, algorithm: str) -> float:
h1 = hex_to_bin(compute_hash(msg1, algorithm))
h2 = hex_to_bin(compute_hash(msg2, algorithm))
# Count differing bits
diff_bits = sum(b1 != b2 for b1, b2 in zip(h1, h2))
total_bits = len(h1)
return (diff_bits / total_bits) * 100.0
def show_diff_plot(msg1: str, msg2: str, algorithm: str):
h1 = compute_hash(msg1, algorithm)
h2 = compute_hash(msg2, algorithm)
b1 = np.array(list(hex_to_bin(h1)), dtype=int)
b2 = np.array(list(hex_to_bin(h2)), dtype=int)
diff = np.logical_xor(b1, b2).astype(int)
fig, ax = plt.subplots(figsize=(10, 1))
ax.imshow(diff.reshape(1, -1), cmap='coolwarm', aspect='auto')
ax.set_xticks([])
ax.set_yticks([])
ax.set_title(f"Bit Differences for {algorithm.upper()} ({sum(diff)} flipped bits)",
fontsize=10)
plt.tight_layout()
return fig