• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting hex representataion of an MD5 hash

 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 159
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic