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...

Recommended Answers

All 2 Replies

> Why should I do this(in bold)? :

Because we require the hex equivalent a generated byte and not the integer this byte gets up-casted to. When converting to hex string, we would want our toHexString() method work on an integer which represents the sequence of bytes generated by md5 and not the integer which the byte value gets automatically upcasted to.

For e.g. if the first byte generated by the md5 algorithm in your case (with input "abhi") has a binary pattern "11010111", we would want the hex equivalent of an integer having the same bit pattern i.e. "00000000000000000000000011010111". But the integer which results from the auto-conversion of "11010111" is of the form "11111111111111111111111111010111". Hence you need to AND the byte in question with 0XFF [00000000000000000000000011111111] to make sure all the higher order bits other than the byte in question are reset to 0.

> Isnt it the incorrect output?

Read the python documentation, hexdigest() is the method you should be looking for.

commented: It's very informative +2

Thank you so much ~s.o.s~.
I used the hexdigest() in python and it gave me the correct string.
Cheers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.