10.6. Code Examples

This section presents several examples to illustrate further how you can use the classes discussed in this chapter.

10.6.1. Computing a Message Digest

The first example computes a message digest, or hash, using the SHA-1 algorithm. Suppose that you have a message composed of three byte arrays: i1, i2, and i3. First, you create a properly initialized message digest object. Then you run the three byte arrays through the message digest object to calculate the hash, as follows:

MessageDigest sha = MessageDigest.getInstance("SHA-1");
byte[] hash = sha.digest();

The call to the digest method signals the end of the input message and causes the digest to be calculated. An alternative ...

Get Inside Java™ 2 Platform Security: Architecture, API Design, and Implementation, Second Edition now with O’Reilly online learning.

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