wood burning stoves*
The moose likes Beginning Java and the fly likes Getting hex representataion of an MD5 hash Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting hex representataion of an MD5 hash" Watch "Getting hex representataion of an MD5 hash" New topic
Author

Getting hex representataion of an MD5 hash

Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 210
I am using MessageDigest to create an MD5 hash string. I need to get the hex representation of that hash. Is there a direct way to do that or do I need to write my own method? TIA.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Just iterate over the byte array and convert each byte to 2 hex digits. You could use String.format, or Integer.toHexString(), though with the latter, you'd have to prefix values < 16 with a leading 0 yourself.
Dennis Putnam
Ranch Hand

Joined: Feb 03, 2012
Posts: 210
Thanks for the reply. I know how to code up a conversion but I was looking to see if there was a direct way either via MessageDigest or some built-in function.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Dennis Putnam wrote:Thanks for the reply. I know how to code up a conversion but I was looking to see if there was a direct way either via MessageDigest or some built-in function.


You can look through MD's methods to see if anything does it. If not (and I suspect it won't be there), other potential candidates might be String, Integer, and Byte, and java.util.Arrays.

And note that if one of them has it, it's not a "built-in function" in the language sense. It'll just be a method written in Java that happens to come bundled with the core API but that'd be no different from one you can write yourself.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18114
    
    8

Apache Commons Codec has some utility methods which do hashing (MD5 and others) and return the result in the hex string you want:

http://commons.apache.org/codec/apidocs/org/apache/commons/codec/digest/DigestUtils.html.

(My google keywords: apache bytes to hex.)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Paul Clapham wrote:
(My google keywords: apache bytes to hex.)


And not knowing ahead of time who might supply it (though apache and sourcefourge are always good starting points), java bytes to hex library brings up apache at about the 6th or 8th spot.
Dennis Grimbergen
Ranch Hand

Joined: Nov 04, 2009
Posts: 138

I once found this piece of code and it works fine if you don't want to use an external jar.



SCJP, SCWCD, SCJD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting hex representataion of an MD5 hash
 
Similar Threads
MD5
MD5 Class
md5 Encryption
Generate unique numbers from a table's primary key, to be used as an external reference.
How to insert password in an encrypted form into a table