Last active
August 29, 2015 14:23
-
-
Save gabalafou/c5d0f5ce3cd6c19b98a3 to your computer and use it in GitHub Desktop.
Function Disqus uses for hashing session id to integer http://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery
This file contains 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
/** | |
* The string hash function from Java. | |
* | |
* Borrowed from http://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery | |
* @param {String} str | |
* @return {Number} | |
*/ | |
function hash(str) { | |
var hash = 0, i, l, charCode; | |
if (str.length === 0) | |
return hash; | |
for (i = 0, l = str.length; i < l; i++) { | |
charCode = str.charCodeAt(i); | |
/* jshint bitwise: false */ | |
hash = ((hash << 5) - hash) + charCode; | |
// Convert to 32bit integer | |
hash |= 0; | |
} | |
return hash; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment