what is possibility of collision in djb2 function?

1.1k Views Asked by At

I am trying to generate unique id using djb2 hash function for string like

"114.143.227.82Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0"

What is the possibility of collision using this algorithm which is given below in javascript.

String.prototype.hashCode = function(){ var hash = 5381; if (this.length === 0) return hash; for (var i = 0; i < this.length; i++) { var character = this.charCodeAt(i); hash = (( hash << 5 ) + hash ) ^ character; } return hash; }

Usage:

var hash = new String("114.143.227.82"+navigator.userAgent).hashCode();

alert(hash);

Reference:

http://www.cse.yorku.ca/~oz/hash.html

For the above string I am getting -ve integer value. How to return +ve integer for those types of string?

1

There are 1 best solutions below

0
On BEST ANSWER

I got the solution after modifying the above code.

String.prototype.hashCode = function(){ var hash = 5381; if (this.length === 0) return hash; for (var i = 0; i < this.length; i++) { var character = this.charCodeAt(i);
hash = hash * 33 ^character; } return hash >>>0; }