Re: MD5 algorithm



Mh... several more problems fixed. For example, I missed another Endianness reversal: The words of the message itself are processed with the lowest byte first.

My fix doesn't look too pretty, but I think it's the fastest it gets without heavily optimizing:

private static int littleEndian(int in)
{
int out=0;
if (in<0)
{
in-=Integer.MIN_VALUE;
out+=128;
}
for (int i=0;i<4;i++)
{
out+=in%256*Math.pow(256,3-i);
in/=256;
}
return out;
}


Also, I missed a crucial part of the algorithm:

b := ((a + f + k[i] + w[g]) leftrotate r[i]) + b

And finally, it appears that the bitwise shift operator isn't doing what I thought it would - no rotation; bits to the left are removed, 0s are added to the right. Too bad - I'd hoped I could use the built-in << operator; now I need a new function for it. (i=i*2+(i<0?1:0)) is reasonably elegant for this, however.

Oh, and the final problem: I forgot to reverse the Endianness of the hash result after calculation.

With all this, I am now finally getting the correct result for the zero-length message (d41d8cd98f00b204e9800998ecf8427e). I'm still getting incorrect results for actual messages, but they have nothing in common and I can be reasonably sure that's because of incorrect padding or character encoding, not the algorithm.

--
cb
.