Tech and Media Labs
This site uses cookies to improve the user experience.




Java MessageDigest

Jakob Jenkov
Last update: 2017-12-18

The Java MessageDigest class represents a cryptographic hash function which can calculate a message digest from binary data. When you receive some encrypted data you cannot see from the data itself whether it was modified during transportation. A message digest can help alleviate that problem.

To be able to detect if the encrypted data has been modified in transport, the sender can calculate a message digest from the data and send that along with the data. When you receive the encrypted data and message digest you can recalculate the message digest from the data and check if the message calculated digest matches the message digest received with the data. If the two message digests match there is a probability that the encrypted data was not modified during transport.

There are a couple of conditions that have to be met for a message digest to be useful as a modification detection mechanism. However, the exact conditions are part of cryptographic theory, so you will have to visit that theory to read how to use message digests correctly. This tutorial only explains how to use the Java Cryptography API representation of a message digest in the MessageDigest class.

Creating a MessageDigest Instance

To create a Java MessageDigest instance you call the static getInstance() method of the MessageDigest class. Here is an example of creating a MessageDigest instance:

MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");

The text parameter passed to the getInstance() method is the name of the concrete message digest algorithm to use.

Message Digest Algorithms

The Java Cryptography API supports the following message digest algorithms (although your concrete cryptography provider might support more!):

Algorithm Name
MD2
MD5
SHA-1
SHA-256
SHA-384
SHA-512

Not all of these message digest algorithms are equally secure. At the time of writing it is recommended that you use SHA-256 or higher to get the highest possible amount of security.

Calculate Message Digest

Once you have created a Java MessageDigest instance you can use it to calculate a message digest from data. If you have a single block of data to calculate a message digest from, use the digest() method. Here is how calculating a message digest from a single block of data looks:

byte[] data1 = "0123456789".getBytes("UTF-8");

MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
byte[] digest = messageDigest.digest(data1);

If you have multiple blocks of data to include in the same message digest, call the update() method and finish off with a call to digest(). Here is how calculating a message digest from multiple blocks of data looks:


byte[] data1 = "0123456789".getBytes("UTF-8");
byte[] data2 = "abcdefghijklmnopqrstuvxyz".getBytes("UTF-8");

MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(data1);
messageDigest.update(data2);

byte[] digest = messageDigest.digest();

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC