File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Why is Java's MD5 different from MySQL MD5? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why is Java Watch "Why is Java New topic
Author

Why is Java's MD5 different from MySQL MD5?

Ivan Jouikov
Ranch Hand

Joined: Jul 22, 2003
Posts: 269
Here's the way I implement MD5 in Java:



However, when I compare hashes of the same string done by this function, and the one done by MySQL's MD5(''), they're very different.

Anybody knows why? My first guess is it has something to do with character encodings. On my MySQL I'm using utf-8 everywhere.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Well MD5 is a standard so presuming MySQL's implementation is correct the only way the two are not going to match is if the values you pass are different. Your guess about character encodings is probably correct - assuming you have checked carefully that it is nothing so simple as a typo? You are probably aware that String.getBytes() returns an encoding of a String in the platform's default character set. You could probably check that this is consistent with the character set used in your MySQL database.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ivan Jouikov
Ranch Hand

Joined: Jul 22, 2003
Posts: 269
Well I solved the problem by downloading somebody's custom MD5 implementation in Java, which works perfect with MySQL.

Screw you, MessageDigest.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
MessageDigest.digest() returns a byte[], and new String(byte[]) converts those bytes directly to characters using the platform's default. However, you're probably expecting a hex-string like "a8ce923f27d1...", and I'd bet the library you found does that conversion for you.
Ivan Jouikov
Ranch Hand

Joined: Jul 22, 2003
Posts: 269
It Sure does:

 
wood burning stoves
 
subject: Why is Java's MD5 different from MySQL MD5?