Skip to content

Instantly share code, notes, and snippets.

@astopo
Created November 15, 2014 17:56
Show Gist options
  • Save astopo/529569effccfaf02780e to your computer and use it in GitHub Desktop.
Save astopo/529569effccfaf02780e to your computer and use it in GitHub Desktop.
Simple non-secure Javascript Hashing #js #hashing #hash
String.prototype.hashCode = function() {
  var hash = 0, i, chr, len;
  if (this.length == 0) return hash;
  for (i = 0, len = this.length; i < len; i++) {
    chr   = this.charCodeAt(i);
    hash  = ((hash << 5) - hash) + chr;
    hash |= 0; // Convert to 32bit integer
  }
  return hash;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment