Last active
September 19, 2016 21:05
-
-
Save charterchap/25ab684a6ad8c3c349af to your computer and use it in GitHub Desktop.
fnv1a_32 hash in javascript consistent w/ libhashkit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://find.fnvhash.com/