Salt slows a dictionary attack

An attacker with a stolen database tries a list of common passwords. With a salt per user, every guess must be hashed again for every user.

What to build

Run a dictionary attack on salted hashes, report which passwords were found, and count the hashes the attacker had to compute.

Requirements

  • FNV-1a 32-bit: start at 2166136261; for each character, XOR the hash with the character code, then multiply by 16777619 keeping 32 bits (Math.imul(hash, 16777619) >>> 0). Print it as 8 lower-case hex digits.
  • Line 1 is N, then N lines name salt hash. Line N + 2 is W, then W candidate passwords, one per line.
  • For each user in input order, try the candidates in list order, hashing salt + candidate, until one matches. Count every hash computed.
  • Print name: password or name: not found for each user.
  • Then print hashes computed: H and without salts: W, the hashes one unsalted list would need (each candidate hashed once).

Input

As described.

Output

N + 2 lines.

Constraints

Up to 20 users and 200 candidates.

Example

Input:

3
rahim q7Lp f77e9c1c
anika Z2xe 5f94f75a
babul M3n0 c792f299
6
123456
password
dhaka2024
qwerty
iloveyou
letmein

Output:

rahim: dhaka2024
anika: qwerty
babul: not found
hashes computed: 13
without salts: 6

Why it works

Each salt makes every user's hashes different, so a guess hashed for one user is useless for the next; the work grows with users × candidates, while without salts one hash of each candidate checks everyone at once.

Examples

Example 1 · Input
3
rahim q7Lp f77e9c1c
anika Z2xe 5f94f75a
babul M3n0 c792f299
6
123456
password
dhaka2024
qwerty
iloveyou
letmein
Expected output
rahim: dhaka2024
anika: qwerty
babul: not found
hashes computed: 13
without salts: 6
Example 2 · Input
1
mina s 72f3132d
2
123456
password
Expected output
mina: 123456
hashes computed: 1
without salts: 2

Hints

Hint 1

Stop trying candidates for a user as soon as one matches.

Hint 2

A user whose password is not in the list costs W hashes.

Solution

Show one correct solution (try it yourself first)
function fnv1a(text) {
  let hash = 2166136261;
  for (let k = 0; k < text.length; k++) {
    hash = hash ^ text.charCodeAt(k);
    hash = Math.imul(hash, 16777619) >>> 0;
  }
  return hash.toString(16).padStart(8, "0");
}
const n = Number(prompt());
const users = [];
for (let k = 0; k < n; k++) {
  const [name, salt, hash] = prompt().trim().split(/\s+/);
  users.push({ name, salt, hash });
}
const w = Number(prompt());
const words = [];
for (let k = 0; k < w; k++) words.push(prompt().trim());
let hashes = 0;
for (const u of users) {
  let found = null;
  for (const word of words) {
    hashes++;
    if (fnv1a(u.salt + word) === u.hash) {
      found = word;
      break;
    }
  }
  console.log(`${u.name}: ${found ?? "not found"}`);
}
console.log(`hashes computed: ${hashes}`);
console.log(`without salts: ${w}`);

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Storing passwords: salt and slow hashes →