Skip to content

Instantly share code, notes, and snippets.

@cm3
Last active April 14, 2021 15:00
Show Gist options
  • Save cm3/773bdd3f7d0706a06ac56d554941fdf2 to your computer and use it in GitHub Desktop.
Save cm3/773bdd3f7d0706a06ac56d554941fdf2 to your computer and use it in GitHub Desktop.
fnv1a_32 in JavaScript. see the history for a without-BigInt version
const fnva = function(data){
hval = BigInt(0x811c9dc5)
for (var i = 0; i < data.length; i++){
hval ^= BigInt(data[i])
hval = (hval * BigInt(0x01000193)) % BigInt(2**32)
}
return hval.toString(16).slice(-8).toUpperCase()
}
const encoder = new TextEncoder()
console.log(fnva(encoder.encode(process.argv[2])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment