My question is concerning the code, which is producing the hash values for strings, summing 4 bytes at a time. It's completely working, but I can't understand some of lines of this code, namely the idea which is performed in some lines. Therefore I need the help of some of yours who is rather familiar with hashing.
Well this is the full code:
long sfold(String s, int M) {
int intLength = s.length() / 4;
long sum = 0;
for (int j = 0; j < intLength; j++) {
char c[] = s.substring(j * 4, (j * 4) + 4).toCharArray();
long mult = 1;
for (int k = 0; k < c.length; k++) {
sum += c[k] * mult;
mult *= 256;
}
}
char c[] = s.substring(intLength * 4).toCharArray();
long mult = 1;
for (int k = 0; k < c.length; k++) {
sum += c[k] * mult;
mult *= 256;
}
return(Math.abs(sum) % M);
}
Here each char value is converted to long integer type, summing the result on each iteration of the for-loop. These 2 doubtful lines of code which I mentioned above are as follows:
sum += c[k] * mult;
mult *= 256;
Well, I can understand the whole code, except these 2 lines...
1) Why we need variable 'mult'? Is it probably a usage of multiplication method for hashing?
2) Why we multiply 'mult' exactly by 256 on each iteration? What is 256 in this case?
If some of you have faced with this code or you know the idea, which is performed in these lines, please help me to understand it too :)