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
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()

Isnt it the incorrect output?

8 Years
Discussion Span
Last Post by abhi_elementx

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

Votes + Comments
It's very informative

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.