On average an attacker finds a random password after trying half of the possibilities, 2^(bits − 1). Speed of guessing turns that into time.
What to build
Turn bits of entropy and a guessing speed into an average time to crack, in readable units.
Requirements
- Each line has bits (may have decimals) and guesses per second.
- seconds = 2^(bits − 1) ÷ speed.
- Under 1 second, print
less than a second. - Otherwise use the largest unit that is at least 1: years (365.25 days), days, hours, minutes or seconds, and print the number with one decimal place and the unit, for example
3.5 days. - If the number of years is 1000000 or more, print
over a million years.
Input
One line per question.
Output
One line per question.
Constraints
0 ≤ bits ≤ 200, and the speed is 0.001 to 10¹².
Example
Input:
37.6 10000000000
52.6 10000
Output:
10.4 seconds
10815.4 years
Why it works
Each extra bit doubles the guesses, so a handful of extra bits turns hours into years; dividing by the speed of the attack is what makes the same password safe online and weak against a stolen fast-hash database.
Examples
37.6 10000000000 52.6 10000
10.4 seconds 10815.4 years
40 0.0277778
627145.1 years
Hints
Hint 1
Check the units from the biggest down.
Hint 2
2 ** (bits - 1) works with decimal bits too.
Solution
Show one correct solution (try it yourself first)
const units = [
["years", 365.25 * 24 * 3600],
["days", 24 * 3600],
["hours", 3600],
["minutes", 60],
["seconds", 1],
];
let line = prompt();
while (line !== null) {
const [bits, speed] = line.trim().split(/\s+/).map(Number);
const seconds = 2 ** (bits - 1) / speed;
if (seconds < 1) console.log("less than a second");
else if (seconds / units[0][1] >= 1000000) console.log("over a million years");
else {
for (const [name, size] of units) {
if (seconds >= size) {
console.log(`${(seconds / size).toFixed(1)} ${name}`);
break;
}
}
}
line = prompt();
}
▶ Open this solution in the compiler, then press Visualize to watch it run step by step