I really need halp getting the Java MD5 and PHP MD5 yield the same result.
In PHP I simply use thins: md5($string);
In Java I use this:
try {
//get
hash = snStr+authStr;
byte defaultBytes[] = hash.getBytes();
MessageDigest algorithm = MessageDigest.getInstance(
"MD5");
algorithm.reset();
algorithm.update(defaultBy
tes);
byte messageDigest[] = algorithm.digest();
StringBuffer hexString = new StringBuffer();
for (byte aMessageDigest : messageDigest) {
// We need to pad small values with an extra zero:
http://forum.de.selfhtml.org/archiv/2005/10/t117779/
int val = 0xff & aMessageDigest;
if (val < 16)
hexString.append("0");
hexString.append(Integer.t
oHexString
(val));
}
hash = hexString.toString();
} catch (NoSuchAlgorithmException nsae) {
nsae.printStackTrace();
}
I have also tried withouth the padding.
The even more strange thing is that for short strings the two MD5:s are the same but if the string to be hashed are longer than around 10 chars I will get two different results
Thank you very much for your help!
Open in new window