@Beta public final class Hashing extends java.lang.Object
HashFunction
instances, and other static hashing-related
utilities.
A comparison of the various hash functions can be found here.
Modifier and Type | Class and Description |
---|---|
private static class |
Hashing.Adler32Holder |
(package private) static class |
Hashing.ChecksumType |
(package private) static class |
Hashing.ConcatenatedHashFunction |
private static class |
Hashing.Crc32cHolder |
private static class |
Hashing.Crc32Holder |
private static class |
Hashing.LinearCongruentialGenerator
Linear CongruentialGenerator to use for consistent hashing.
|
private static class |
Hashing.Md5Holder |
private static class |
Hashing.Murmur3_128Holder |
private static class |
Hashing.Murmur3_32Holder |
private static class |
Hashing.Sha1Holder |
private static class |
Hashing.Sha256Holder |
private static class |
Hashing.Sha512Holder |
private static class |
Hashing.SipHash24Holder |
Modifier and Type | Field and Description |
---|---|
private static int |
GOOD_FAST_HASH_SEED
Used to randomize
goodFastHash(int) instances, so that programs which persist anything
dependent on the hash codes they produce will fail sooner. |
Modifier | Constructor and Description |
---|---|
private |
Hashing() |
Modifier and Type | Method and Description |
---|---|
static HashFunction |
adler32()
Returns a hash function implementing the Adler-32 checksum algorithm (32 hash bits) by
delegating to the
Adler32 Checksum . |
(package private) static int |
checkPositiveAndMakeMultipleOf32(int bits)
Checks that the passed argument is positive, and ceils it to a multiple of 32.
|
private static HashFunction |
checksumHashFunction(Hashing.ChecksumType type,
java.lang.String toString) |
static HashCode |
combineOrdered(java.lang.Iterable<HashCode> hashCodes)
Returns a hash code, having the same bit length as each of the input hash codes,
that combines the information of these hash codes in an ordered fashion.
|
static HashCode |
combineUnordered(java.lang.Iterable<HashCode> hashCodes)
Returns a hash code, having the same bit length as each of the input hash codes,
that combines the information of these hash codes in an unordered fashion.
|
static int |
consistentHash(HashCode hashCode,
int buckets)
Assigns to
hashCode a "bucket" in the range [0, buckets) , in a uniform
manner that minimizes the need for remapping as buckets grows. |
static int |
consistentHash(long input,
int buckets)
Assigns to
input a "bucket" in the range [0, buckets) , in a uniform
manner that minimizes the need for remapping as buckets grows. |
static HashFunction |
crc32()
Returns a hash function implementing the CRC-32 checksum algorithm (32 hash bits) by delegating
to the
CRC32 Checksum . |
static HashFunction |
crc32c()
Returns a hash function implementing the CRC32C checksum algorithm (32 hash bits) as described
by RFC 3720, Section 12.1.
|
static HashFunction |
goodFastHash(int minimumBits)
Returns a general-purpose, temporary-use, non-cryptographic hash function.
|
static HashFunction |
md5()
Returns a hash function implementing the MD5 hash algorithm (128 hash bits) by delegating to
the MD5
MessageDigest . |
static HashFunction |
murmur3_128()
Returns a hash function implementing the
128-bit murmur3 algorithm, x64 variant (little-endian variant),
using a seed value of zero.
|
static HashFunction |
murmur3_128(int seed)
Returns a hash function implementing the
128-bit murmur3 algorithm, x64 variant (little-endian variant),
using the given seed value.
|
static HashFunction |
murmur3_32()
Returns a hash function implementing the
32-bit murmur3 algorithm, x86 variant (little-endian variant),
using a seed value of zero.
|
static HashFunction |
murmur3_32(int seed)
Returns a hash function implementing the
32-bit murmur3 algorithm, x86 variant (little-endian variant),
using the given seed value.
|
static HashFunction |
sha1()
Returns a hash function implementing the SHA-1 algorithm (160 hash bits) by delegating to the
SHA-1
MessageDigest . |
static HashFunction |
sha256()
Returns a hash function implementing the SHA-256 algorithm (256 hash bits) by delegating to
the SHA-256
MessageDigest . |
static HashFunction |
sha512()
Returns a hash function implementing the SHA-512 algorithm (512 hash bits) by delegating to the
SHA-512
MessageDigest . |
static HashFunction |
sipHash24()
Returns a hash function implementing the
64-bit SipHash-2-4 algorithm
using a seed value of
k = 00 01 02 ... . |
static HashFunction |
sipHash24(long k0,
long k1)
Returns a hash function implementing the
64-bit SipHash-2-4 algorithm
using the given seed.
|
private static final int GOOD_FAST_HASH_SEED
goodFastHash(int)
instances, so that programs which persist anything
dependent on the hash codes they produce will fail sooner.public static HashFunction goodFastHash(int minimumBits)
Warning: a new random seed for these functions is chosen each time the Hashing
class is loaded. Do not use this method if hash codes may escape the current
process in any way, for example being sent over RPC, or saved to disk.
Repeated calls to this method on the same loaded Hashing
class, using the same value
for minimumBits
, will return identically-behaving HashFunction
instances.
minimumBits
- a positive integer (can be arbitrarily large)minimumBits
or greaterpublic static HashFunction murmur3_32(int seed)
The exact C++ equivalent is the MurmurHash3_x86_32 function (Murmur3A).
public static HashFunction murmur3_32()
The exact C++ equivalent is the MurmurHash3_x86_32 function (Murmur3A).
public static HashFunction murmur3_128(int seed)
The exact C++ equivalent is the MurmurHash3_x64_128 function (Murmur3F).
public static HashFunction murmur3_128()
The exact C++ equivalent is the MurmurHash3_x64_128 function (Murmur3F).
public static HashFunction sipHash24()
k = 00 01 02 ...
.public static HashFunction sipHash24(long k0, long k1)
public static HashFunction md5()
MessageDigest
.public static HashFunction sha1()
MessageDigest
.public static HashFunction sha256()
MessageDigest
.public static HashFunction sha512()
MessageDigest
.public static HashFunction crc32c()
public static HashFunction crc32()
CRC32
Checksum
.
To get the long
value equivalent to Checksum.getValue()
for a
HashCode
produced by this function, use HashCode.padToLong()
.
public static HashFunction adler32()
Adler32
Checksum
.
To get the long
value equivalent to Checksum.getValue()
for a
HashCode
produced by this function, use HashCode.padToLong()
.
private static HashFunction checksumHashFunction(Hashing.ChecksumType type, java.lang.String toString)
public static int consistentHash(HashCode hashCode, int buckets)
hashCode
a "bucket" in the range [0, buckets)
, in a uniform
manner that minimizes the need for remapping as buckets
grows. That is,
consistentHash(h, n)
equals:
n - 1
, with approximate probability 1/n
consistentHash(h, n - 1)
, otherwise (probability 1 - 1/n
)
See the wikipedia article on consistent hashing for more information.
public static int consistentHash(long input, int buckets)
input
a "bucket" in the range [0, buckets)
, in a uniform
manner that minimizes the need for remapping as buckets
grows. That is,
consistentHash(h, n)
equals:
n - 1
, with approximate probability 1/n
consistentHash(h, n - 1)
, otherwise (probability 1 - 1/n
)
See the wikipedia article on consistent hashing for more information.
public static HashCode combineOrdered(java.lang.Iterable<HashCode> hashCodes)
java.lang.IllegalArgumentException
- if hashCodes
is empty, or the hash codes
do not all have the same bit lengthpublic static HashCode combineUnordered(java.lang.Iterable<HashCode> hashCodes)
java.lang.IllegalArgumentException
- if hashCodes
is empty, or the hash codes
do not all have the same bit lengthstatic int checkPositiveAndMakeMultipleOf32(int bits)