From string to hex MD5 hash and back

Posted by Pablo Fernandez on Stack Overflow See other posts from Stack Overflow or by Pablo Fernandez
Published on 2010-06-11T13:29:12Z Indexed on 2010/06/11 13:32 UTC
Read the original article Hit count: 297

Filed under:
|
|
|

I have this pseudo-code in java:

bytes[] hash = MD5.hash("example");

String hexString = toHexString(hash); //This returns something like a0394dbe93f

bytes[] hexBytes = hexString.getBytes("UTF-8");

Now, hexBytes[] and hash[] are different.

I know I'm doing something wrong since hash.length() is 16 and hexBytes.length() is 32. Maybe it has something to do with java using Unicode for chars (just a wild guess here).

Anyways, the question would be: how to get the original hash[] array from the hexString.

The whole code is here if you want to look at it (it's ~ 40 LOC) http://gist.github.com/434466

The output of that code is:

16
[-24, 32, -69, 74, -70, 90, -41, 76, 90, 111, -15, -84, -95, 102, 65, -10]
32
[101, 56, 50, 48, 98, 98, 52, 97, 98, 97, 53, 97, 100, 55, 52, 99, 53, 97, 54, 102, 102, 49, 97, 99, 97, 49, 54, 54, 52, 49, 102, 54]

Thanks a lot!

© Stack Overflow or respective owner

Related posts about java

Related posts about hash