-
-
Save PatrickJS/3f6c5d207162f4b32818 to your computer and use it in GitHub Desktop.
FNV-1a Hash (http://isthe.com/chongo/tech/comp/fnv/) in JavaScript.
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
| // 32 bit FNV-1a hash | |
| // Ref.: http://isthe.com/chongo/tech/comp/fnv/ | |
| function fnv32a( str ) | |
| { | |
| var FNV1_32A_INIT = 0x811c9dc5; | |
| var hval = FNV1_32A_INIT; | |
| for ( var i = 0; i < str.length; ++i ) | |
| { | |
| hval ^= str.charCodeAt(i); | |
| hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24); | |
| } | |
| return hval >>> 0; | |
| } | |
| /* | |
| print( "Lorem -> " + fnv32a('Lorem') ); // Lorem -> 1789342528 | |
| */ | |
| /* for Mac OSX JSC: | |
| $ cat words.txt | /System/Library/Frameworks/JavaScriptCore.framework/Resources/jsc -f fnv32a.js > tmp_js.txt | |
| var line; | |
| while((line = readline()) != "") | |
| { | |
| var re = /\"(.*)\"\,/; | |
| var word = re.exec(line); | |
| print( word[1] + " -> " + fnv32a(word[1]) ); | |
| } | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment