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:

public static String md5OfFile(File file) throws Exception{
    MessageDigest md = MessageDigest.getInstance("MD5");
    FileInputStream fs = new FileInputStream(file);
    BufferedInputStream bs = new BufferedInputStream(fs);
    byte[] buffer = new byte[1024];
    int bytesRead;

    while((bytesRead = bs.read(buffer, 0, buffer.length)) != -1){
       md.update(buffer, 0, bytesRead);
    }
    byte[] digest = md.digest();

    StringBuilder sb = new StringBuilder();
    for(byte bite : digest){
        sb.append(String.format("%02x", bite & 0xff));
    }
    return sb.toString();
}

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

Get the MD5 Hash Code of a File with Java