Created
August 8, 2013 03:42
-
-
Save komiga/6181269 to your computer and use it in GitHub Desktop.
FNV-1a for 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 | |
| // Will produce creepy output if input data is not ASCII | |
| function fnv1a(str) { | |
| var OFFSET_BASIS = 0x811c9dc5; | |
| //var PRIME = 0x01000193; | |
| var h = OFFSET_BASIS; | |
| for (var i = 0; i < str.length; ++i) { | |
| h ^= str.charCodeAt(i); | |
| // This should be h *= PRIME; | |
| // but /JavaScript/ so /magic/ instead | |
| h += (h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24); | |
| } | |
| return h >>> 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment