forked from mtusi2026/mtusi2026lab7
57 lines
1.3 KiB
Python
57 lines
1.3 KiB
Python
|
|
import statistics
|
|
import time
|
|
|
|
from argon2 import PasswordHasher, Type
|
|
|
|
PASSWORD = "Lab7-Benchmark-Password-2026"
|
|
ITERATIONS = 10
|
|
|
|
CONFIGURATIONS = [
|
|
("baseline", 19456, 2, 1),
|
|
("stronger", 32768, 3, 1),
|
|
("high-memory", 65536, 3, 1),
|
|
]
|
|
|
|
|
|
def benchmark(name, memory, time_cost, parallelism):
|
|
hasher = PasswordHasher(
|
|
memory_cost=memory,
|
|
time_cost=time_cost,
|
|
parallelism=parallelism,
|
|
hash_len=32,
|
|
salt_len=16,
|
|
type=Type.ID,
|
|
)
|
|
|
|
samples = []
|
|
|
|
# Warm-up, excluded from measurements.
|
|
sample_hash = hasher.hash(PASSWORD)
|
|
assert hasher.verify(sample_hash, PASSWORD)
|
|
|
|
for _ in range(ITERATIONS):
|
|
start = time.perf_counter()
|
|
password_hash = hasher.hash(PASSWORD)
|
|
elapsed = (time.perf_counter() - start) * 1000
|
|
|
|
assert hasher.verify(password_hash, PASSWORD)
|
|
assert password_hash.startswith("$argon2id$")
|
|
|
|
samples.append(elapsed)
|
|
|
|
print(
|
|
f"{name}: "
|
|
f"memory={memory} KiB, "
|
|
f"t={time_cost}, "
|
|
f"p={parallelism}, "
|
|
f"median={statistics.median(samples):.2f} ms, "
|
|
f"min={min(samples):.2f} ms, "
|
|
f"max={max(samples):.2f} ms"
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
for configuration in CONFIGURATIONS:
|
|
benchmark(*configuration)
|