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 />

• It should be difficult to find two documents that share a hash code. You cannot easily<br />

find two documents with the same hash code, regardless of what that hash code is. The<br />

previous criterion means that you can't change the document to match a hash code.<br />

This criterion says you can't change two documents to match each other.<br />

• Small changes in documents produce large changes in the hash code. Mathematicians<br />

call this criterion "sensitive dependence on initial conditions." Without this<br />

requirement, somebody attempting to create a document with a given hash code could<br />

modify the document a little at a time until the hash code matched, much as you might<br />

adjust the hot and cold water faucets gradually until the water reaches a desired<br />

temperature. A hash function should act more like a faucet that can scald or freeze you<br />

after the tiniest nudge.<br />

• The hash code does not say anything about the document it represents. The one-way<br />

hash function is not even partially invertible. For instance, knowing that the hash code<br />

is even should not suggest that the document being hashed contains an even number of<br />

bytes. Nor should it suggest that the document being hashed is 60% more likely to<br />

contain an even number of bytes than an odd number. While one-way hash functions<br />

need to be reproducible—that is, the same document always has the same hash code—<br />

they should otherwise be completely random. It is extremely hard, perhaps impossible,<br />

to prove that any function meets this criterion. Nonetheless, stronger functions come<br />

closer than weaker functions, and years of experience among cryptographers allow<br />

them to make reasonable guesses about what are and are not strong hash functions,<br />

even if their hunches can't be proved to a mathematical certainty.<br />

The proper design of one-way hash functions is a well-studied field. It's easy to create weak<br />

one-way hash functions. However, it is much harder to create truly strong, reliable, one-way<br />

hash functions. Nonexperts tend to make nonobvious but serious mistakes when<br />

implementing hash functions. Therefore, this is a task that's best left to the experts.<br />

Fortunately, the <strong>Java</strong> core API contains some hash functions designed by experts that the rest<br />

of us can use without earning a Ph.D. in applied mathematics first.<br />

The hash codes used by the java.util.Hashtable class and returned<br />

by the hashCode() method of any <strong>Java</strong> object are only intended to be<br />

used as IDs for elements of a hash table, not as cryptographically strong<br />

digest. These sorts of hash codes have different requirements for utility.<br />

Most of the time, they only need to meet the first two of the six criteria<br />

given earlier, and in practice they often don't meet even that. The<br />

hashCode() method is a hash function but not necessarily a one-way<br />

hash function.<br />

10.2 The MessageDigest Class<br />

The java.security.MessageDigest class is an abstract class that represents a hash code<br />

and its associated algorithm. Concrete subclasses (actually concrete subclasses of<br />

java.security.MessageDigestSPI, though the difference isn't relevant from a client's point<br />

of view) implement particular, professionally designed, well-known hash code algorithms.<br />

Thus, rather than constructing instances of this class directly, you ask the static<br />

MessageDigest.getInstance() factory method to provide an implementation of an<br />

algorithm with a particular name. Table 10.1 lists the standard names for message digest<br />

195

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

Saved successfully!

Ooh no, something went wrong!