hello .
I am trying to simulate the MD5 hashing algo.
My code:
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
try {
MessageDigest object = MessageDigest.getInstance("MD5");
String str = jTextField1.getText();
byte arr [] = str.getBytes();
object.update(arr);//update the md5 object with the message
arr = object.digest();//calculate the digest
str = IntegerToHex(arr);//convert to hex
jTextField2.setText(str);
}
catch (NoSuchAlgorithmException ex) {
}
}
This is the IntegerToHex() method:
private static String IntegerToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i=0;i<data.length;i++) {
buf.append(Integer.toHexString(0xFF & data[i]));
}
return buf.toString();
}
Why should I do this(in bold)? :
buf.append(Integer.toHexString([B]0xFF & data[i][/B]));
If I dont AND 0xFF and data, my output is :
Input : abhi
Output is: ffffffd76f3d5ffffffccffffff9affffffc9ffffff8f1fffffff9160274a39fffffffe33
But if I AND 0xFF and data, my output is :
Input: abhi
Output: d76f3d5cc9ac98f1f9160274a39fe33
Also my python output is:
>>> md5.new("abhi").digest()
"\xd7o=\x05\xcc\x9a\xc9\x8f\x1f\x91`'J9\xfe3"
Isnt it the incorrect output?
Thanks...