Using the Message Digest Class

Message digests are implemented using the MessageDigest class (java.security.MessageDigest):

public abstract class MessageDigest extends MessageDigestSpi

Implement operations to create and verify a message digest.

Like all engine classes, instances of the message digest are obtained through one of these methods:

public static MessageDigest getInstance(String algorithm)public static MessageDigest getInstance(String algorithm, String provider)

Return an instance of the message digest class that implements the given algorithm, optionally using the given provider. If no provider can be found that implements the given algorithm, a NoSuchAlgorithmException is thrown. If the named provider can’t be found, a NoSuchProviderException is thrown.

Once a message digest object has been obtained, the developer can operate on that object with these methods:

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

Add the specified data to the digest. The first of these methods adds a single byte to the data, the second adds the entire array of bytes, and the third adds only the specified subset of the array of data.

These methods may be called in any order and any number of times to add the desired data to the digest. Consecutive calls to these methods append data to the internal accumulation of data over which the digest will be calculated.

public byte[] digest( )public byte[] digest(byte[] input)

Compute the ...

Get Java Security, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.