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
MessageDigestfor 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
MessageDigestfor 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
inputarray to the message digest’s input data.- public void update(byte[] input, int offset, int len)
This method adds
lenbytes of the given array, starting atoffset, 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access