There are great posts on how to use JAVA to create a MYSQL MD5 hash, however is it possible to go the other way – use SQL to produce what java does ?
in SQL:
Select MD5('secret') = 5ebe2294ecd0e0f08eab7690d2a6ee69
in Java, the MD5 algo yields: 94-6634-108-20-48-32-16-114-85118-112-46-90-18105
First off I can see the Java one is a decimal representation, so pairing off the SQL output and converting to DECIMAL gives: 9419034148236208224240142171118144210166238105
The only part that’s the same is the Front 2 and last 4.
Anyone have ideas on how to use SQL to generate what Java would produce?
I don’t have this fully worked out, but hopefully, these hints will help:
In Java, if you take the direct result of the md.digest() call, you have a byte array.
Instead of converting that to a String representation, just output the bytes as Integers.
You’ll see that they are:
94
-66
34
-108
-20
-48
-32
-16
-114
-85
118
-112
-46
-90
-18
105
So, the dashes in the resulting string conversion aren’t separators, they are arithmetic signs.
The java hash is a simple concatenation of the byte values as (signed) ints.
When you Select MD5(‘secret’), you getting a string of 32 hex digits.
I verified that if in Java, you convert each byte to it’s hex representation, and concatenate them, you get the same string that MySQL returns.
So, it seems to me that what you would need to do in MySQL is to iterate the result of Hex(), one hex char at a time (perhaps using SUBSTRING()), convert each to decimal, and concatenate them.
Edit – looks like you are almost doing it correctly, but you need to convert each pair of hex digits to Byte instead of int, so for example, ‘0xbe’ is -66, not 190
Edit #2 – I was intrigued by how relatively difficult this is to do in MySQL, so I persisted (joke intended). The following stored function seems to do the job: