A site stores hash(salt + password) for each user, with a random salt per user. Equal passwords then give different stored hashes.
What to build
For each salt and password, print the value the site would store.
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). Use it as a stand-in for a real password hash. - Each line is a salt and a password separated by one space; hash the salt followed directly by the password.
- Print
salt:hashwith the hash as 8 lower-case hex digits.
Input
One salt and password per line.
Output
One line per input.
Constraints
Salts and passwords have 1 to 30 characters and no spaces.
Example
Input:
q7Lp dhaka2024
Z2xe dhaka2024
Output:
q7Lp:f77e9c1c
Z2xe:d4d27d19
Why it works
The salt changes the input, so the same password gives a different hash for each salt, and only someone who knows both salt and password can make the stored value.
Examples
Example 1 · Input
q7Lp dhaka2024 Z2xe dhaka2024
Expected output
q7Lp:f77e9c1c Z2xe:d4d27d19
Example 2 · Input
abc password
Expected output
abc:bcc2f96a
Hints
Hint 1
line.split(" ") gives the salt and the password.
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");
}
let line = prompt();
while (line !== null) {
const [salt, password] = line.trim().split(" ");
console.log(`${salt}:${fnv1a(salt + password)}`);
line = prompt();
}
▶ Open this solution in the compiler, then press Visualize to watch it run step by step