Find the edited log entry

A hash chain makes a log tamper-evident: each entry's stored hash covers the previous hash too, so changing any entry breaks every link after it.

What to build

Recompute a hash chain over a log and report the first entry that no longer matches its stored hash.

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.
  • The chain starts at h₀ = 00000000. For entry i, hᵢ = fnv1a(hᵢ₋₁ + entryᵢ), joining the previous hash's 8 hex digits and the entry text.
  • Input: N, then N log entries, then the N stored hashes h₁ … hₙ.
  • Print chain intact if every stored hash matches, otherwise first change at entry K (counting from 1).

Input

As described.

Output

One line.

Constraints

1 to 50 entries of up to 100 characters.

Example

Input:

4
09:00 admin login
09:05 grades exported
09:12 backup started
09:30 admin logout
3323fbaa
835ca839
06124c2a
fd8f9f0b

Output:

chain intact

Why it works

Each hash depends on everything before it, so the first mismatch is exactly where the log was changed; later mismatches only follow from that one.

Examples

Example 1 · Input
4
09:00 admin login
09:05 grades exported
09:12 backup started
09:30 admin logout
3323fbaa
835ca839
06124c2a
fd8f9f0b
Expected output
chain intact
Example 2 · Input
4
09:00 admin login
09:05 grades edited
09:12 backup started
09:30 admin logout
3323fbaa
835ca839
06124c2a
fd8f9f0b
Expected output
first change at entry 2

Hints

Hint 1

Read all the entries before the hashes.

Hint 2

Stop at the first mismatch.

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 entries = [];
for (let k = 0; k < n; k++) entries.push(prompt());
const stored = [];
for (let k = 0; k < n; k++) stored.push(prompt().trim());
let prev = "00000000";
let bad = -1;
for (let k = 0; k < n; k++) {
  prev = fnv1a(prev + entries[k]);
  if (prev !== stored[k]) {
    bad = k + 1;
    break;
  }
}
console.log(bad < 0 ? "chain intact" : `first change at entry ${bad}`);

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

Lesson: Hash functions and the avalanche effect →