Implementing the Different Message Digest Algorithms in Java

  

To understand which message digests are supported in Java, you can get a listing of the properties of the service providers.

Cross-Reference  

Refer to Listing 4-14 in Chapter 4 for an example in how to get provider information.

As you can see in Listing 4-14, the service providers in the JDK 1.4 for the message digests are the MD-5 and SHA-1. Listing 9-2 demonstrates the SHA-1 message digest code using a service provider as well as the algorithm from the specification. Listing 9-1 demonstrates the MD5 message digest code using a service provider as well as the algorithm from the specification. In Listing 9-1, the getInstance method from the java.security.MessageDigest class defines which algorithm to initialize. This example loads the "MD5" algorithm.

The update method of the MessageDigest class adds the input data to the algorithm. Multiple updates can be done to the message digest to be hashed . The final phase will not complete until the digest method is executed. The variable chain starts at the point of the getInstance or digest method and ends at the next digest method. What this means is that if the program does a getInstance , then does several updates, and finally a digest call, the digest is the total on all messages passed through the updates. Refer to Figure 9-2 to understand this better.

When the digest method is called, the variables and buffers will be reset to an initial state so that a new digest can start from that point on. Being able to provide multiple updates is one of the features that Java provides in addition to abstracting the algorithms. Another feature worth noting is the java.security.DigestInputStream class, which associates a message digest with an input stream. When data is read into the input stream, it is sent directly to the update of the message digest. Classes such as the DigestInputStream can alleviate several method calls that would be required to read and call the updates.

  


Java Security Solutions
Java Security Solutions
ISBN: 0764549286
EAN: 2147483647
Year: 2001
Pages: 222

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net