Shouldn’t the digest() method in MessageDigest always give the same hash value for the same input?
I tried this and I am getting different set of hashvalues for the same input everytime
md5 = MessageDigest.getInstance("MD5");
System.out.println(md5.digest("stringtodigest".getBytes()));
System.out.println(md5.digest("stringtodigest".getBytes()));
System.out.println(md5.digest("stringtodigest".getBytes()));
Update: Changed the param to digest() method
You’re seeing the results of calling
byte[].toString()– which isn’t showing you the actual hash of the data. You’re basically getting a string which shows that you’ve calledtoStringon a byte array (that’s the[Bpart) and then the hash returned byObject.hashCode()(that’s the hex value after the@). That hash code doesn’t take the data in the array into account.Try
to see the actual data.
EDIT: Quick note about creating an MD5 digest from string data – you should always use the same encoding, explicitly, when hashing. For example: