Message Digests

As you saw in Chapter 2 ,a message digest takes an arbitrary amount of input data and produces a short, digested version of the data. The Java Cryptography Architecture (JCA) makes it very easy to use message digests. The java.security.MessageDigest class encapsulates a cryptographic message digest.

Getting

To obtain a MessageDigest for a particular algorithm use one of its getInstance() factory methods:

public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException

This method returns a MessageDigest for the given algorithm. The first provider supporting the given algorithm is used.

public static MessageDigest getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException

This method returns a MessageDigest for the given algorithm, using the given provider.

Feeding

To feed data into the MessageDigest, use one of the update() methods:

public void update(byte input)

This method adds the specified byte to the message digest’s input data.

public void update(byte[] input)

Use this method to add the entire input array to the message digest’s input data.

public void update(byte[] input, int offset, int len)

This method adds len bytes of the given array, starting at offset, to the message digest’s input data.

You can call the update() methods as many times as you want before calculating the digest value.

Digesting

To find out the digest value, use one of the digest() methods:

public byte[] digest()

The value of the ...

Get Java Cryptography now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.