• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting hex representataion of an MD5 hash

 
Dennis Putnam
Ranch Hand
Posts: 359
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 359
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 159
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I once found this piece of code and it works fine if you don't want to use an external jar.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic