Skip to content

Instantly share code, notes, and snippets.

@charterchap
Last active September 19, 2016 21:05
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 charterchap/25ab684a6ad8c3c349af to your computer and use it in GitHub Desktop.
Save charterchap/25ab684a6ad8c3c349af to your computer and use it in GitHub Desktop.
fnv1a_32 hash in javascript consistent w/ libhashkit
// J. Charter Chapman
// fnv1a_32 in javascript based on libhashkit
// this one actually outputs consistent with hashkit
// there are a couple of interesting implementations out there
// string to byte array [think char *]
function getBytes(s) {
var buf = [];
for (var i = 0; i < s.length; i++) {
buf.push(s.charCodeAt(i));
}
return buf;
}
function fnv1a_32( str )
{
bytes=getBytes(str);
hash= 0x811c9dc5; //< See libhashkit's algorithms for this magic number
for ( var x = 0; x < str.length; x++ )
{
hash = (hash ^ bytes[x]) ;
// this is: hash *= FNV_32_PRIME; 16777619u again see libhashkit the
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
}
return (hash >>> 0).toString(16); // >>> 0 forces unsigned 32bit int
}
@charterchap
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment