• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Why is Java's MD5 different from MySQL MD5?  RSS feed

 
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ivan Jouikov
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I solved the problem by downloading somebody's custom MD5 implementation in Java, which works perfect with MySQL.

Screw you, MessageDigest.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It Sure does:

 
I'm THIS CLOSE to ruling the world! Right after reading this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!