Skip to content

Instantly share code, notes, and snippets.

@di3
Created May 16, 2019 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save di3/d3f517228853679d7ea9d7f7ff956704 to your computer and use it in GitHub Desktop.
Save di3/d3f517228853679d7ea9d7f7ff956704 to your computer and use it in GitHub Desktop.
const FNV_OFFSET_32 = 0x811c9dc5;
export const hash_fnv32a (input) => {
let hval = FNV_OFFSET_32;
// Strips unicode bits, only the lower 8 bits of the values are used
for (var i = 0; i < input.length; i++) {
hval = hval ^ (input.charCodeAt(i) & 0xFF);
hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
}
return hval >>> 0;
}
//assert.equal(hash_fnv32a('aaaa'), 1290481081);
//assert.equal(hash_fnv32a(123456), 2166136261);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment