Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to convert strings to some form of hash. In anyway, is this possible in Javascript/jQuery.

I did some Google searches but they all seem to be referring to the # character.

I'm not utilizing a server-side language so I can't do it that way.

share|improve this question
You want to look up something like Javascript md5. – rlemon Sep 30 '11 at 21:55
add comment (requires an account with 50 reputation)

3 Answers

up vote 62 down vote accepted
String.prototype.hashCode = function(){
    var hash = 0, i, char;
    if (this.length == 0) return hash;
    for (i = 0, l = this.length; i < l; i++) {
        char  = this.charCodeAt(i);
        hash  = ((hash<<5)-hash)+char;
        hash |= 0; // Convert to 32bit integer
    }
    return hash;
};

Described here.

share|improve this answer
2  
This is the same one used in Java. The hash << 5 - hash is the same as hash * 31 + char but a LOT faster. It's nice because it's so fast, and 31 is a small prime. Win win there. – corsiKa Sep 30 '11 at 21:59
1  
Very lightweight to add on the page too. I like this. Just wondering what resistance to reversing the hashes this has? sequences of numbers for example result in predictable hashes sometimes. – Dan Lehmann Sep 30 '11 at 22:07
5  
I did a few tests on jsperf (jsperf.com/hashing-strings) and the bitwise function is actually more slow than the number based function. – skerit Jun 29 '12 at 21:23
1  
number based is unusable for larger strings. – Peter Aron Zentai Feb 26 at 23:38
1  
@hdgarrood -- Math.pow( 2, 33 ) & <anything> === 0 because the & op does an implicit cast to a 32-bit int and the last 32 bits of that are 0, so the 1 at bit 34 gets thrown away. Anything using the & op will be exactly the same, including & 0xffffffff – cwolves May 31 at 0:19
show 8 more commentsadd comment (requires an account with 50 reputation)

if anyone is interested, here is an improved ( faster ) version, which will fail on older browsers who lack the reduce array function.

hashCode = function(s){
  return s.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0);              
}
share|improve this answer
1  
Much faster. ~ 4 times faster based on my tests. Thanks for the tip. Here's info on browser support for the Array.reduce() method: developer.mozilla.org/en-US/docs/JavaScript/Reference/… – jake May 8 at 2:01
add comment (requires an account with 50 reputation)

I needed a similar function (but different) to generate a unique-ish ID based on the username and current time. So:

window.newId = ->
  # create a number based on the username
  unless window.userNumber?
  window.userNumber = 0
  for c,i in window.MyNamespace.userName
    char = window.MyNamespace.userName.charCodeAt(i)
    window.MyNamespace.userNumber+=char
  ((window.MyNamespace.userNumber + Math.floor(Math.random() * 1e15) + new Date().getMilliseconds()).toString(36)).toUpperCase()

Produces:

2DVFXJGEKL
6IZPAKFQFL
ORGOENVMG
... etc 
share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.