The Hash Class

The Hash class (jxta.security.hash.Hash) calculates message digests or hashes of data. A message digest is a short array of bytes that is calculated based on an arbitrary set of data; you can take millions of bytes of data and calculate a 64-byte hash of the data. Hashes are not guaranteed to be unique, but the odds that two sets of data produce the same hash are quite remote (1 in 264).

By themselves, hashes are not secure. You can transmit a hash value along with some data, and the recipient of the data can recreate the hash. If the newly calculated hash does not match the original hash, then the receiver knows that the data was modified in transit. However, if the hash is sent with the data, then nothing prevents the hash from being modified along with the data, leaving the receiver unaware of the manipulation.

There are two ways to solve this problem. The first is to use a digital signature. A digital signature is essentially (though not exactly the same thing as) an encryption of the hash value. Because anyone who modifies the data used to create the hash lacks the necessary key to produce the digital signature, the signed hash is secure; if the digital signature verification succeeds, you know that the data was not modified in transit.

A second technique is to use a message authentication code (MAC), which we’ll look at in the next section.

The JXTA API supports two hash algorithms: MD5 and SHA1. Hash objects are obtained from the getJxtaHash( ...

Get JXTA in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.