O'Reilly logo

Inside Java™ 2 Platform Security: Architecture, API Design, and Implementation, Second Edition by Mary Dageforde, Gary Ellison, Li Gong

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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");
sha.update(i1);
sha.update(i2);
sha.update(i3);
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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required