Get the MD5 Hash Code of a File with Java

Getting the hash code of a file is a common programming task. MD5 is a very popular and commonly used hashing algorithm. Getting the MD5 hash code of a file with Java can be easily done, and is shown in the code below:

The code above does several things:

  • Creates a MessageDigest object that will eventually create the MD5 hash code
  • Uses FileInputStream and BufferedInputStream to buffer the input from a file
  • Updates the MessageDigest object with update() for every 1024 bytes of the file read
  • Uses the digest() method to return the MD5 hash code of the file
  • 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="">