Create an MD5 Hash Code from a String in Java

Creating a hash codes from strings is a common programming task. MD5 is a very popular and commonly used hashing algorithm. Creating an MD5 hash code from a String in Java can be easily done, and is shown in the code below:

The code above is does several things:

  • Creates a StringBuilder object to hold the MD5 hexadecimal hash code
  • Creates a MessageDigest object that will eventually create the MD5 hash code
  • Updates this MessageDigest object with the byte array representation of the string to be hashed
  • Uses the digest() method to return the MD5 hash code of the string
  • Converts this MD5 hash code to hexadecimal format

This code can be found in the Big Datums GitHub repo

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">