Files
mtusi2026lab7/scripts/benchmark_argon2.py

36 lines
1.3 KiB
Python

#!/usr/bin/env python3
"""Measure the local Argon2id password-hash budget without printing credentials."""
from __future__ import annotations
import argparse
import statistics
import time
import secrets
from argon2 import PasswordHasher, Type
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--rounds", type=int, default=7)
args = parser.parse_args()
if args.rounds < 3 or args.rounds > 30:
parser.error("--rounds must be between 3 and 30")
hasher = PasswordHasher(time_cost=2, memory_cost=19456, parallelism=1, hash_len=32, salt_len=16, type=Type.ID)
password = secrets.token_urlsafe(24)
durations = []
for _ in range(args.rounds):
started = time.perf_counter()
encoded = hasher.hash(password)
hasher.verify(encoded, password)
durations.append((time.perf_counter() - started) * 1000)
ordered = sorted(durations)
p95_index = min(len(ordered) - 1, int(0.95 * len(ordered)))
print(f"algorithm=Argon2id memory_kib=19456 time_cost=2 parallelism=1 rounds={args.rounds}")
print(f"hash_plus_verify_median_ms={statistics.median(durations):.2f} p95_ms={ordered[p95_index]:.2f}")
print("synthetic password and encoded hashes were not printed or saved")
if __name__ == "__main__":
main()