Unpacking this definition:
Hasher), but this is merely a convenience; these are always translated into raw
byte sequences under the covers.
bits). For example, Hashing#sha1 produces a 160-bit number,
while Hashing#murmur3_32() yields only 32 bits. Because a long value is
clearly insufficient to hold all hash code values, this API represents a hash code as an
instance of HashCode.
HashFunction instances should always be
stateless, and therefore thread-safe.
Summarizing the last two points: "equal yield equal always; unequal yield unequal often." This is the most important characteristic of all hash functions.
A high-quality hash function strives for some subset of the following virtues:
Hashing#sha512 are designed to
make it as infeasible as possible to reverse-engineer the input that produced a given hash
code, or even to discover any two distinct inputs that yield the same result. These
are called cryptographic hash functions. But, whenever it is learned that either of
these feats has become computationally feasible, the function is deemed "broken" and should
no longer be used for secure purposes. (This is the likely eventual fate of all
cryptographic hashes.)
The primary way to provide the data that your hash function should act on is via a Hasher. Obtain a new hasher from the hash function using newHasher, "push" the relevant
data into it using methods like Hasher#putBytes(byte[]), and finally ask for the HashCode when finished using Hasher#hash. (See an example of
this.)
If all you want to hash is a single byte array, string or long value, there are
convenient shortcut methods defined directly on HashFunction to make this easier.
Hasher accepts primitive data types, but can also accept any Object of type T provided
that you implement a Funnel<T> to specify how to "feed" data from that object
into the function. (See an example of this.)
Compatibility note: Throughout this API, multibyte values are always interpreted in
little-endian order. That is, hashing the byte array {0x01, 0x02, 0x03, 0x04} is
equivalent to hashing the int value 0x04030201. If this isn't what you need,
methods such as Integer#reverseBytes and Ints#toByteArray will help.
Object#hashCodeJava's baked-in concept of hash codes is constrained to 32 bits, and provides no separation
between hash algorithms and the data they act on, so alternate hash algorithms can't be easily
substituted. Also, implementations of hashCode tend to be poor-quality, in part because
they end up depending on other existing poor-quality hashCode implementations,
including those in many JDK classes.
Object.hashCode implementations tend to be very fast, but have weak collision
prevention and no expectation of bit dispersion. This leaves them perfectly suitable for
use in hash tables, because extra collisions cause only a slight performance hit, while poor bit
dispersion is easily corrected using a secondary hash function (which all reasonable hash table
implementations in Java use). For the many uses of hash functions beyond data structures,
however, Object.hashCode almost always falls short -- hence this library.
| Modifier and Type | Method and Description |
|---|---|
| public int | bits()
Returns the number of bits (a multiple of 32) that each hash code produced by this hash function has. |
| public HashCode | |
| public HashCode | |
| public HashCode | |
| public HashCode | hashInt(int input)
Shortcut for |
| public HashCode | hashLong(long input)
Shortcut for |
| public <T extends @Nullable Object> HashCode | hashObject(T instance, Funnel<? super T> funnel)
Shortcut for |
| public HashCode | hashString(CharSequence input, Charset charset)
Shortcut for |
| public HashCode | |
| public Hasher | newHasher()
Begins a new hash code computation by returning an initialized, stateful |
| public Hasher | newHasher(int expectedInputSize)
Begins a new hash code computation as |
| bits | back to summary |
|---|---|
| public int bits() Returns the number of bits (a multiple of 32) that each hash code produced by this hash function has. | |
| hashBytes | back to summary |
|---|---|
| public HashCode hashBytes(byte[] input) Shortcut for | |
| hashBytes | back to summary |
|---|---|
| public HashCode hashBytes(byte[] input, int off, int len) Shortcut for
| |
| hashBytes | back to summary |
|---|---|
| public HashCode hashBytes(ByteBuffer input) Shortcut for
| |
| hashInt | back to summary |
|---|---|
| public HashCode hashInt(int input) Shortcut for
| |
| hashLong | back to summary |
|---|---|
| public HashCode hashLong(long input) Shortcut for | |
| hashObject | back to summary |
|---|---|
| public <T extends @Nullable Object> HashCode hashObject(T instance, Funnel<? super T> funnel) Shortcut for
| |
| hashString | back to summary |
|---|---|
| public HashCode hashString(CharSequence input, Charset charset) Shortcut for Warning This method, which reencodes the input before hashing it, is useful only for
cross-language compatibility. For other use cases, prefer | |
| hashUnencodedChars | back to summary |
|---|---|
| public HashCode hashUnencodedChars(CharSequence input) Shortcut for Warning This method will produce different output than most other languages do when
running the same hash function on the equivalent input. For cross-language compatibility, use
| |
| newHasher | back to summary |
|---|---|
| public Hasher newHasher() Begins a new hash code computation by returning an initialized, stateful
| |
| newHasher | back to summary |
|---|---|
| public Hasher newHasher(int expectedInputSize) Begins a new hash code computation as | |