Hi everybody,
I'm facing kind of a strange issue which is related MD5-Hashes in Java and php5. I figured that unter certain circumstances the following code does not generate correct MD5 hashes:
public static String getMD5Hash(String string)
{
try
{
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(string.getBytes());
byte[] digest = md5.digest();
string = byteArrToHexString(digest);
}
catch (NoSuchAlgorithmException e1)
{
e1.printStackTrace();
}
return string;
}
private static String byteArrToHexString(byte[] bArr)
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bArr.length; i++)
{
int unsigned = bArr[i] & 0xff;
sb.append(Integer.toHexString((unsigned)));
}
return sb.toString();
}
I had to migrate an existing user database where the passwords where stored in php5 MD5. Now some of the users, not all, can't login because my Java code does not produce the correct MD5 hash.
Any ideas what's wrong with the above?