comparing MessageDigest, MD5, results

Hello all,
      I am new to java security and I have been trying to get two message digests to output the same results for the same input string.  Here is sample of  the code I am using.

               byte[] tester1 = test1.getBytes();
                        
            MessageDigest sign1 = MessageDigest.getInstance("MD5");
               MessageDigest sign2 = MessageDigest.getInstance("MD5");
            
               byte[] result1 = sign1.digest(tester1);
             byte[] result2 = sign2.digest(tester1);
            
               boolean equal = result1.equals(result2);
             System.out.println("The hahes are equal is: " + equal);
             System.out.println(result1.toString() + "\n" + result2.toString());
            
            
The output of this is that the byte arrays are never equal.  Shouldn't MessageDigest.digest(byte[] b)  always return the same array for the same input?  Do I misunderstand what MessageDigest does?  I have to hash encode a string to be compared for equality later.  Thanks for your help.
 
alewand8Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vinod_VijayanVinCommented:

>>boolean equal = result1.equals(result2);
The above checks for equality of object references which obviously are different
for different objects.
You want here to really check the contents of the two byte arrays.
0
objectsCommented:
your not checking whether the *contents* of the arrays are equal. should be:

boolean equal = Arrays.equals(result1, result2);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
yes it must be Arrays.equals.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

alewand8Author Commented:
Thanks Very much, especially for the speed of response.  I have another question.  When I use System.out.prinln(result1 + "\n" result2), why are the strings not the same even though the boolean equal = Arrays.equals(result1,results2) is true.  Thanks
0
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
can you just put forward the result of  System.out.prinln(result1 + "\n" result2);

to be clear do this  System.out.prinln("Result1:"+ result1);System.out.prinln("Result2:"+ result2);

and present the results here.
0
alewand8Author Commented:
Here are the results:

Result1: [B@e7b241
Result2: [B@167d940

Thanks for the help
0
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
sorry they will surely be diffrent as they are the Hashcodes of your byte array objects.

Actually Arrays.equals(result1,result2); will check if the content inside the byte arrays result1 and result2 are same or not and not weather the objects are same or not.

What's happening is correct so don't worry.
0
alewand8Author Commented:
System.out.println("Result2: "+ result2);
System.out.println("Result1: "+ result1);

Result2: [B@e7b241
Result1: [B@167d940

I was just messing around and when I changed the order that they print out the (see that result2 is printed on first line) but the output remains the same.  Very strange to me.
0
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
Are you sure you did this

System.out.println("Result2: "+ result2);
System.out.println("Result1: "+ result1);

I think You might have done

System.out.println("Result2: "+ result1);
System.out.println("Result1: "+ result2);

please Just check.
0
alewand8Author Commented:
System.out.println("Result2: "+ result2);
System.out.println("Result1: "+ result1);

This is just cut and paste from my code.

Results in:

Result2: [B@e7b241
Result1: [B@167d940

If I change it to

System.out.println("Result1: "+ result1);
System.out.println("Result2: "+ result2);

Again just copy and pasted.

Results in:

Result1: [B@e7b241
Result2: [B@167d940

0
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
hey this real fun and what you are telling is correct.

which ever Byte array you print 1st it will have a value [B@e7b241 in your case and the second one [B@167d940.

and if you want try this

System.out.println("Result1: "+ "shiva");
System.out.println("Result2: "+ result2);

or

System.out.println("Result1: "+ "shiva");
System.out.println("Result2: "+ result1);

you will get in both the cases

Result1:shiva
Result2:[B@e7b241

i will try getting the exact reason.
Bye.

0
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
and i think the actual problem is with the toString method you may need to override in your class.

If you want check result1.hashCode(); and result2.hashCode(); they are diffrent clearly indicating that they are two diffrent objects.

As such the System.out.prinln(); automatically calls the toString method of the Objects before printing as you have not overridden that method in your class you are getting one such out put.

In the result "B" stands for Byte array similarly "I" stands for Integer array and "J" stand for long array and so on.

Thank You.
0
alewand8Author Commented:
I am not exactly sure how I can overide the byte[].toString().  I understand I can put       

        public String toString()
      {
            return "Result1: " + result1 + "\n" + "Result2: " + result2;
      }

in my class, but I am still using the toString() method of the byte array, which calls the Object.toString().  I am pretty new to java so maybe there is something I am missing.

Thanks for all the help,

Bye
0
Vinod_VijayanVinCommented:
         If you want to print the results simply use :

            System.out.println("Result1:");
            int i=0;
            for(;i<result1.length;i++)
            {
                  System.out.print(result1[i]);
            }
            System.out.println();
           
            System.out.println("Result1:");
            for(i=0;i<result2.length;i++)
            {
                  System.out.print(result2[i]);
            }
            System.out.println();  
0
Vinod_VijayanVinCommented:
>>System.out.println("Result1:");

That should be System.out.println("Result2:"); the second time
0
CEHJCommented:
System.out.println(Arrays.toString(result1));
System.out.println(Arrays.toString(result2));
0
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
good one CHEJ.

alewand8 I think this clears your problem.

Bye.
0
CEHJCommented:
You can represent your digests by turning them into hex strings


    public static String byteArrayToHexString(byte[] rawBytes) {
      String charIndex = "0123456789abcdef";
      StringBuffer sb = new StringBuffer(rawBytes.length * 2);
      for (int i = 0; i < rawBytes.length; i++) {
        int ix = (rawBytes[i] >> 4) & 0xF;
        sb.append(charIndex.charAt(ix));
        ix = rawBytes[i] & 0xF;
        sb.append(charIndex.charAt(ix));
      }
      return sb.toString();
    }
0
alewand8Author Commented:
Thanks to everyone, Its all starting to make sense.  The help was great.
0
CEHJCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.