23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Java</strong> I/O<br />

you only want to detect significant changes, you have to first filter the insignificant data from<br />

the stream in a predictable fashion before calculating the message digest.<br />

10.2.2 Creating Message Digests<br />

There are no public constructors in java.security.MessageDigest. Instead, you generally<br />

use one of two MessageDigest.getInstance() factory methods to retrieve the appropriate<br />

digest for a particular algorithm.<br />

public static MessageDigest getInstance(String algorithm)<br />

throws NoSuchAlgorithmException<br />

public static MessageDigest getInstance(String algorithm, String provider)<br />

throws NoSuchAlgorithmException, NoSuchProviderException<br />

For example:<br />

MessageDigest sha = MessageDigest.getInstance("SHA");<br />

MessageDigest md2 = MessageDigest.getInstance("MD2", "Cryptix");<br />

Each of these methods returns an instance of a MessageDigest subclass that's configured<br />

with the requested algorithm. These subclasses and the associated MessageDigestSPI classes<br />

that actually implement the algorithms are installed when you install a cryptographic provider.<br />

JDK 1.2 installs the Sun provider, which supplies implementations of SHA-1 and MD5.<br />

Each provider provides a possibly redundant collection of message digest algorithms. The<br />

factory method design pattern used here allows for the possibility that a particular algorithm<br />

may be provided by different classes in different environments. For instance, the SHA<br />

algorithm may be supplied by the sun.security.provider.SHA class in one development<br />

environment and by the cryptix.provider.md.SHA1 class in another. Some standard<br />

algorithm names are listed in Table 10.1. If you request an algorithm none of your installed<br />

providers can supply, a NoSuchAlgorithmException is thrown. Most of the time, you're<br />

content to simply request an algorithm and let any provider that can fulfill your request<br />

provide it. However, if you want to specify a particular provider by name (for instance,<br />

because it's got an especially fast native-code implementation of the algorithm you want), you<br />

can pass the provider name as the second argument to MessageDigest.getInstance(). If<br />

the provider you request isn't found, a NoSuchProviderException is thrown.<br />

MessageDigest has a protected constructor for use by subclasses. If you were writing your<br />

own provider, with your own message digest algorithms and implementations, you'd use this:<br />

protected MessageDigest(String algorithm)<br />

However, as I said earlier, the creation of message digest algorithms and implementations is<br />

harder than it looks and is a task best left to the experts. [3]<br />

3 If you want to design your own one-way hash functions, Applied Cryptography, by Bruce Schneier (John Wiley & Sons, 1995) is a good place to<br />

start. For details about how to implement <strong>Java</strong> security providers that supply new message digest algorithms, see Chapter 9 of <strong>Java</strong> Cryptography, by<br />

Jonathan Knudsen (O'Reilly & Associates, 1998).<br />

199

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!