Class Hashing
HashFunction instances, and other static hashing-related
utilities.
A comparison of the various hash functions can be found here.
- Since:
- 11.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic HashFunctionadler32()static HashCodecombineOrdered(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 HashCodecombineUnordered(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 HashFunctionconcatenating(HashFunction first, HashFunction second, HashFunction... rest) Returns a hash function which computes its hash code by concatenating the hash codes of the underlying hash functions together.static HashFunctionconcatenating(Iterable<HashFunction> hashFunctions) Returns a hash function which computes its hash code by concatenating the hash codes of the underlying hash functions together.static intconsistentHash(long input, int buckets) Assigns toinputa "bucket" in the range[0, buckets), in a uniform manner that minimizes the need for remapping asbucketsgrows.static intconsistentHash(HashCode hashCode, int buckets) Assigns tohashCodea "bucket" in the range[0, buckets), in a uniform manner that minimizes the need for remapping asbucketsgrows.static HashFunctioncrc32()static HashFunctioncrc32c()Returns a hash function implementing the CRC32C checksum algorithm (32 hash bits) as described by RFC 3720, Section 12.1.static HashFunctionReturns a hash function implementing FarmHash's Fingerprint64, an open-source algorithm.static HashFunctiongoodFastHash(int minimumBits) Returns a general-purpose, temporary-use, non-cryptographic hash function.static HashFunctionhmacMd5(byte[] key) Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the MD5 (128 hash bits) hash function and acreated from the given byte array and the MD5 algorithm.invalid reference
SecretSpecKeystatic HashFunctionReturns a hash function implementing the Message Authentication Code (MAC) algorithm, using the MD5 (128 hash bits) hash function and the given secret key.static HashFunctionhmacSha1(byte[] key) Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the SHA-1 (160 hash bits) hash function and acreated from the given byte array and the SHA-1 algorithm.invalid reference
SecretSpecKeystatic HashFunctionReturns a hash function implementing the Message Authentication Code (MAC) algorithm, using the SHA-1 (160 hash bits) hash function and the given secret key.static HashFunctionhmacSha256(byte[] key) Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the SHA-256 (256 hash bits) hash function and acreated from the given byte array and the SHA-256 algorithm.invalid reference
SecretSpecKeystatic HashFunctionhmacSha256(Key key) Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the SHA-256 (256 hash bits) hash function and the given secret key.static HashFunctionhmacSha512(byte[] key) Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the SHA-512 (512 hash bits) hash function and acreated from the given byte array and the SHA-512 algorithm.invalid reference
SecretSpecKeystatic HashFunctionhmacSha512(Key key) Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the SHA-512 (512 hash bits) hash function and the given secret key.static HashFunctionmd5()Returns a hash function implementing the MD5 hash algorithm (128 hash bits) by delegating to the MD5MessageDigest.static HashFunctionReturns a hash function implementing the 128-bit murmur3 algorithm, x64 variant (little-endian variant), using a seed value of zero.static HashFunctionmurmur3_128(int seed) Returns a hash function implementing the 128-bit murmur3 algorithm, x64 variant (little-endian variant), using the given seed value.static HashFunctionReturns a hash function implementing the 32-bit murmur3 algorithm, x86 variant (little-endian variant), using a seed value of zero.static HashFunctionmurmur3_32(int seed) Returns a hash function implementing the 32-bit murmur3 algorithm, x86 variant (little-endian variant), using the given seed value.static HashFunctionsha1()Returns a hash function implementing the SHA-1 algorithm (160 hash bits) by delegating to the SHA-1MessageDigest.static HashFunctionsha256()Returns a hash function implementing the SHA-256 algorithm (256 hash bits) by delegating to the SHA-256MessageDigest.static HashFunctionsha384()Returns a hash function implementing the SHA-384 algorithm (384 hash bits) by delegating to the SHA-384MessageDigest.static HashFunctionsha512()Returns a hash function implementing the SHA-512 algorithm (512 hash bits) by delegating to the SHA-512MessageDigest.static HashFunctionReturns a hash function implementing the 64-bit SipHash-2-4 algorithm using a seed value ofk = 00 01 02 ....static HashFunctionsipHash24(long k0, long k1) Returns a hash function implementing the 64-bit SipHash-2-4 algorithm using the given seed.
-
Method Details
-
goodFastHash
Returns a general-purpose, temporary-use, non-cryptographic hash function. The algorithm the returned function implements is unspecified and subject to change without notice.Warning: a new random seed for these functions is chosen each time the
Hashingclass 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
Hashingclass, using the same value forminimumBits, will return identically-behavingHashFunctioninstances.- Parameters:
minimumBits- a positive integer (can be arbitrarily large)- Returns:
- a hash function, described above, that produces hash codes of length
minimumBitsor greater
-
murmur3_32
Returns a hash function implementing the 32-bit murmur3 algorithm, x86 variant (little-endian variant), using the given seed value.The exact C++ equivalent is the MurmurHash3_x86_32 function (Murmur3A).
-
murmur3_32
Returns a hash function implementing the 32-bit murmur3 algorithm, x86 variant (little-endian variant), using a seed value of zero.The exact C++ equivalent is the MurmurHash3_x86_32 function (Murmur3A).
-
murmur3_128
Returns a hash function implementing the 128-bit murmur3 algorithm, x64 variant (little-endian variant), using the given seed value.The exact C++ equivalent is the MurmurHash3_x64_128 function (Murmur3F).
-
murmur3_128
Returns a hash function implementing the 128-bit murmur3 algorithm, x64 variant (little-endian variant), using a seed value of zero.The exact C++ equivalent is the MurmurHash3_x64_128 function (Murmur3F).
-
sipHash24
Returns a hash function implementing the 64-bit SipHash-2-4 algorithm using a seed value ofk = 00 01 02 ....- Since:
- 15.0
-
sipHash24
Returns a hash function implementing the 64-bit SipHash-2-4 algorithm using the given seed.- Since:
- 15.0
-
md5
Returns a hash function implementing the MD5 hash algorithm (128 hash bits) by delegating to the MD5MessageDigest.Warning: MD5 is not cryptographically secure or collision-resistant and is not recommended for use in new code. It should be used for legacy compatibility reasons only. Please consider using a hash function in the SHA-2 family of functions (e.g., SHA-256).
-
sha1
Returns a hash function implementing the SHA-1 algorithm (160 hash bits) by delegating to the SHA-1MessageDigest.Warning: SHA1 is not cryptographically secure and is not recommended for use in new code. It should be used for legacy compatibility reasons only. Please consider using a hash function in the SHA-2 family of functions (e.g., SHA-256).
-
sha256
Returns a hash function implementing the SHA-256 algorithm (256 hash bits) by delegating to the SHA-256MessageDigest. -
sha384
Returns a hash function implementing the SHA-384 algorithm (384 hash bits) by delegating to the SHA-384MessageDigest.- Since:
- 19.0
-
sha512
Returns a hash function implementing the SHA-512 algorithm (512 hash bits) by delegating to the SHA-512MessageDigest. -
hmacMd5
Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the MD5 (128 hash bits) hash function and the given secret key.- Parameters:
key- the secret key- Throws:
IllegalArgumentException- if the given key is inappropriate for initializing this MAC- Since:
- 20.0
-
hmacMd5
Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the MD5 (128 hash bits) hash function and acreated from the given byte array and the MD5 algorithm.invalid reference
SecretSpecKey- Parameters:
key- the key material of the secret key- Since:
- 20.0
-
hmacSha1
Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the SHA-1 (160 hash bits) hash function and the given secret key.- Parameters:
key- the secret key- Throws:
IllegalArgumentException- if the given key is inappropriate for initializing this MAC- Since:
- 20.0
-
hmacSha1
Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the SHA-1 (160 hash bits) hash function and acreated from the given byte array and the SHA-1 algorithm.invalid reference
SecretSpecKey- Parameters:
key- the key material of the secret key- Since:
- 20.0
-
hmacSha256
Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the SHA-256 (256 hash bits) hash function and the given secret key.- Parameters:
key- the secret key- Throws:
IllegalArgumentException- if the given key is inappropriate for initializing this MAC- Since:
- 20.0
-
hmacSha256
Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the SHA-256 (256 hash bits) hash function and acreated from the given byte array and the SHA-256 algorithm.invalid reference
SecretSpecKey- Parameters:
key- the key material of the secret key- Since:
- 20.0
-
hmacSha512
Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the SHA-512 (512 hash bits) hash function and the given secret key.- Parameters:
key- the secret key- Throws:
IllegalArgumentException- if the given key is inappropriate for initializing this MAC- Since:
- 20.0
-
hmacSha512
Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the SHA-512 (512 hash bits) hash function and acreated from the given byte array and the SHA-512 algorithm.invalid reference
SecretSpecKey- Parameters:
key- the key material of the secret key- Since:
- 20.0
-
crc32c
Returns a hash function implementing the CRC32C checksum algorithm (32 hash bits) as described by RFC 3720, Section 12.1.- Since:
- 18.0
-
crc32
Returns a hash function implementing the CRC-32 checksum algorithm (32 hash bits) by delegating to theCRC32Checksum.To get the
longvalue equivalent toChecksum.getValue()for aHashCodeproduced by this function, useHashCode.padToLong().- Since:
- 14.0
-
adler32
Returns a hash function implementing the Adler-32 checksum algorithm (32 hash bits) by delegating to theAdler32Checksum.To get the
longvalue equivalent toChecksum.getValue()for aHashCodeproduced by this function, useHashCode.padToLong().- Since:
- 14.0
-
farmHashFingerprint64
Returns a hash function implementing FarmHash's Fingerprint64, an open-source algorithm.This is designed for generating persistent fingerprints of strings. It isn't cryptographically secure, but it produces a high-quality hash with fewer collisions than some alternatives we've used in the past. FarmHashFingerprints generated using this are byte-wise identical to those created using the C++ version, but note that this uses unsigned integers (see
UnsignedInts). Comparisons between the two should take this into account.- Since:
- 20.0
-
consistentHash
Assigns tohashCodea "bucket" in the range[0, buckets), in a uniform manner that minimizes the need for remapping asbucketsgrows. That is,consistentHash(h, n)equals:n - 1, with approximate probability1/nconsistentHash(h, n - 1), otherwise (probability1 - 1/n)
This method is suitable for the common use case of dividing work among buckets that meet the following conditions:
- You want to assign the same fraction of inputs to each bucket.
- When you reduce the number of buckets, you can accept that the most recently added buckets
will be removed first. More concretely, if you are dividing traffic among tasks, you can
decrease the number of tasks from 15 and 10, killing off the final 5 tasks, and
consistentHashwill handle it. If, however, you are dividing traffic among serversalpha,bravo, andcharlieand you occasionally need to take each of the servers offline,consistentHashwill be a poor fit: It provides no way for you to specify which of the three buckets is disappearing. Thus, if your buckets change from[alpha, bravo, charlie]to[bravo, charlie], it will assign all the oldalphatraffic tobravoand all the oldbravotraffic tocharlie, rather than lettingbravokeep its traffic.
See the Wikipedia article on consistent hashing for more information.
-
consistentHash
public static int consistentHash(long input, int buckets) Assigns toinputa "bucket" in the range[0, buckets), in a uniform manner that minimizes the need for remapping asbucketsgrows. That is,consistentHash(h, n)equals:n - 1, with approximate probability1/nconsistentHash(h, n - 1), otherwise (probability1 - 1/n)
This method is suitable for the common use case of dividing work among buckets that meet the following conditions:
- You want to assign the same fraction of inputs to each bucket.
- When you reduce the number of buckets, you can accept that the most recently added buckets
will be removed first. More concretely, if you are dividing traffic among tasks, you can
decrease the number of tasks from 15 and 10, killing off the final 5 tasks, and
consistentHashwill handle it. If, however, you are dividing traffic among serversalpha,bravo, andcharlieand you occasionally need to take each of the servers offline,consistentHashwill be a poor fit: It provides no way for you to specify which of the three buckets is disappearing. Thus, if your buckets change from[alpha, bravo, charlie]to[bravo, charlie], it will assign all the oldalphatraffic tobravoand all the oldbravotraffic tocharlie, rather than lettingbravokeep its traffic.
See the Wikipedia article on consistent hashing for more information.
-
combineOrdered
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. That is, whenever two equal hash codes are produced by two calls to this method, it is as likely as possible that each was computed from the same input hash codes in the same order.- Throws:
IllegalArgumentException- ifhashCodesis empty, or the hash codes do not all have the same bit length
-
combineUnordered
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. That is, whenever two equal hash codes are produced by two calls to this method, it is as likely as possible that each was computed from the same input hash codes in some order.- Throws:
IllegalArgumentException- ifhashCodesis empty, or the hash codes do not all have the same bit length
-
concatenating
public static HashFunction concatenating(HashFunction first, HashFunction second, HashFunction... rest) Returns a hash function which computes its hash code by concatenating the hash codes of the underlying hash functions together. This can be useful if you need to generate hash codes of a specific length.For example, if you need 1024-bit hash codes, you could join two
sha512()hash functions together:Hashing.concatenating(Hashing.sha512(), Hashing.sha512()).- Since:
- 19.0
-
concatenating
Returns a hash function which computes its hash code by concatenating the hash codes of the underlying hash functions together. This can be useful if you need to generate hash codes of a specific length.For example, if you need 1024-bit hash codes, you could join two
sha512()hash functions together:Hashing.concatenating(Hashing.sha512(), Hashing.sha512()).- Since:
- 19.0
-