Simple (non-secure) hash function for JavaScript?

JavascriptHashMd5Sha1

Javascript Problem Overview


> 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.)

Javascript Solutions


Solution 1 - Javascript

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;
	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;
}

Solution 2 - Javascript

There are many realizations of hash functions written in JS. For example:

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

Solution 3 - Javascript

Check out these implementations

Solution 4 - Javascript

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.

Solution 5 - Javascript

Check out this MD5 implementation for JavaScript. Its BSD Licensed and really easy to use. Example:

md5 = hex_md5("message to digest")

Solution 6 - Javascript

// 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 = 0, j = 0, k = 0, 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;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmjsView Question on Stackoverflow
Solution 1 - JavascriptBarakView Answer on Stackoverflow
Solution 2 - JavascriptsilexView Answer on Stackoverflow
Solution 3 - JavascriptStefan FilipView Answer on Stackoverflow
Solution 4 - JavascriptFordiView Answer on Stackoverflow
Solution 5 - JavascriptjsalonenView Answer on Stackoverflow
Solution 6 - JavascriptSergey ShuchkinView Answer on Stackoverflow