Possible Duplicate:
Generate a Hash from string in Javascript/jQuery
Can anyone suggest a simple (i.e. tens of lines of code, not hundreds of lines) hash function written in (browser-compatible) JavaScript? Ideally I'd like something that, when passed a string as input, produces something similar to the 32 character hexadecimal string that's the typical output of MD5, SHA1, etc. It doesn't have to be cryptographically secure, just reasonably resistant to collisions. (My initial use case is URLs, but I'll probably want to use it on other strings in the future.)
I didn't verify this myself, but you can look at this JavaScript implementation of Java's String.hashCode() method. Seems reasonably short.
With this prototype you can simply call .hashCode()
on any string, e.g. "some string".hashCode()
, and receive a numerical hash code (more specifically, a Java equivalent) such as 1395333309.
String.prototype.hashCode = function() {
var hash = 0;
if (this.length == 0) {
return hash;
}
for (var i = 0; i < this.length; i++) {
var char = this.charCodeAt(i);
hash = ((hash<<5)-hash)+char;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
There are many realizations of hash functions written in JS. For example:
- SHA-1: http://www.webtoolkit.info/javascript-sha1.html
- SHA-256: http://www.webtoolkit.info/javascript-sha256.html
- MD5: http://www.webtoolkit.info/javascript-md5.html
If you don't need security, you can also use base64 which is not hash-function, has not fixed output and could be simply decoded by user, but looks more lightweight and could be used for hide values: http://www.webtoolkit.info/javascript-base64.html
Check out these implementations
- http://www.movable-type.co.uk/scripts/sha1.html (SHA-1 algorithm)
- http://pajhome.org.uk/crypt/md5/ (implementations for SHA-1, MD5, HMAC and others)
This article explains simple hash functions in some detail and provides some sample code (in C) that is pretty straighforward. Looks like Bob Jenkins' hash function could be appropriate for your needs (this Dr Dobbs article has more details and a survey of other hash functions, both of which could be helpful).
Simple object hasher:
(function () {
Number.prototype.toHex = function () {
var ret = ((this<0?0x8:0)+((this >> 28) & 0x7)).toString(16) + (this & 0xfffffff).toString(16);
while (ret.length < 8) ret = '0'+ret;
return ret;
};
Object.hashCode = function hashCode(o, l) {
l = l || 2;
var i, c, r = [];
for (i=0; i<l; i++)
r.push(i*268803292);
function stringify(o) {
var i,r;
if (o === null) return 'n';
if (o === true) return 't';
if (o === false) return 'f';
if (o instanceof Date) return 'd:'+(0+o);
i=typeof o;
if (i === 'string') return 's:'+o.replace(/([\\\\;])/g,'\\$1');
if (i === 'number') return 'n:'+o;
if (o instanceof Function) return 'm:'+o.toString().replace(/([\\\\;])/g,'\\$1');
if (o instanceof Array) {
r=[];
for (i=0; i<o.length; i++)
r.push(stringify(o[i]));
return 'a:'+r.join(';');
}
r=[];
for (i in o) {
r.push(i+':'+stringify(o[i]))
}
return 'o:'+r.join(';');
}
o = stringify(o);
for (i=0; i<o.length; i++) {
for (c=0; c<r.length; c++) {
r[c] = (r[c] << 13)-(r[c] >> 19);
r[c] += o.charCodeAt(i) << (r[c] % 24);
r[c] = r[c] & r[c];
}
}
for (i=0; i<r.length; i++) {
r[i] = r[i].toHex();
}
return r.join('');
}
}());
The meat here is the stringifier, which simply converts any object into a unique string. hashCode then runs over the object, hashing together the characters of the stringified object.
For extra points, export the stringifier and create a parser.
Check out this MD5 implementation for JavaScript. Its BSD Licensed and really easy to use. Example:
md5 = hex_md5("message to digest")
// Simple but unreliable function to create string hash by Sergey.Shuchkin [t] gmail.com
// alert( strhash('http://www.w3schools.com/js/default.asp') ); // 6mn6tf7st333r2q4o134o58888888888
function strhash( str ) {
if (str.length % 32 > 0) str += Array(33 - str.length % 32).join("z");
var hash = '', bytes = [], i = j = k = a = 0, dict = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','1','2','3','4','5','6','7','8','9'];
for (i = 0; i < str.length; i++ ) {
ch = str.charCodeAt(i);
bytes[j++] = (ch < 127) ? ch & 0xFF : 127;
}
var chunk_len = Math.ceil(bytes.length / 32);
for (i=0; i<bytes.length; i++) {
j += bytes[i];
k++;
if ((k == chunk_len) || (i == bytes.length-1)) {
a = Math.floor( j / k );
if (a < 32)
hash += '0';
else if (a > 126)
hash += 'z';
else
hash += dict[ Math.floor( (a-32) / 2.76) ];
j = k = 0;
}
}
return hash;
}