Top Description Inners Fields Constructors Methods
com.google.common.hash

public final Class Hashing

extends Object
Class Inheritance
Annotations
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, .Throwables.throwIfUnchecked, java.lang.invoke.MethodType.methodType

Static methods to obtain HashFunction instances, and other static hashing-related utilities.

A comparison of the various hash functions can be found here.

Authors
Kevin Bourrillion, Dimitris Andreou, Kurt Alfred Kluever
Since
11.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static enum
private static class
private static class
private static enum
private static class
Hashing.LinearCongruentialGenerator

Linear CongruentialGenerator to use for consistent hashing.

private static class
private static class
private static class
private static class
private static class

Field Summary

Modifier and TypeField and Description
pack-priv static final int
GOOD_FAST_HASH_SEED

Used to randomize goodFastHash instances, so that programs which persist anything dependent on the hash codes they produce will fail sooner.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static HashFunction
adler32()

Returns a hash function implementing the Adler-32 checksum algorithm (32 hash bits).

pack-priv static int
checkPositiveAndMakeMultipleOf32(int bits)

Checks that the passed argument is positive, and ceils it to a multiple of 32.

public static HashCode
combineOrdered(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.

public static HashCode
combineUnordered(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.

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.

public static HashFunction
concatenating(Iterable<HashFunction> hashFunctions)

Returns a hash function which computes its hash code by concatenating the hash codes of the underlying hash functions together.

public 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.

public 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.

public static HashFunction
crc32()

Returns a hash function implementing the CRC-32 checksum algorithm (32 hash bits).

public static HashFunction
crc32c()

Returns a hash function implementing the CRC32C checksum algorithm (32 hash bits) as described by RFC 3720, Section 12.1.

public static HashFunction
farmHashFingerprint64()

Returns a hash function implementing FarmHash's Fingerprint64, an open-source algorithm.

public static HashFunction
fingerprint2011()

Returns a hash function implementing the Fingerprint2011 hashing function (64 hash bits).

public static HashFunction

Returns:

a hash function, described above, that produces hash codes of length minimumBits or greater
goodFastHash
(int
a positive integer. This can be arbitrarily large. The returned HashFunction instance may use memory proportional to this integer.
minimumBits
)

Returns a general-purpose, temporary-use, non-cryptographic hash function.

public static HashFunction
hmacMd5(Key
the secret key
key
)

Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the MD5 (128 hash bits) hash function and the given secret key.

public static HashFunction
hmacMd5(byte[]
the key material of the secret key
key
)

Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the MD5 (128 hash bits) hash function and a SecretKeySpec created from the given byte array and the MD5 algorithm.

public static HashFunction
hmacSha1(Key
the secret key
key
)

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.

public static HashFunction
hmacSha1(byte[]
the key material of the secret key
key
)

Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the SHA-1 (160 hash bits) hash function and a SecretKeySpec created from the given byte array and the SHA-1 algorithm.

public static HashFunction
hmacSha256(Key
the secret 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.

public static HashFunction
hmacSha256(byte[]
the key material of the secret key
key
)

Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the SHA-256 (256 hash bits) hash function and a SecretKeySpec created from the given byte array and the SHA-256 algorithm.

public static HashFunction
hmacSha512(Key
the secret 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.

public static HashFunction
hmacSha512(byte[]
the key material of the secret key
key
)

Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the SHA-512 (512 hash bits) hash function and a SecretKeySpec created from the given byte array and the SHA-512 algorithm.

private static String
hmacToString(String methodName, Key key)

public static HashFunction
md5()

Deprecated If you must interoperate with a system that requires MD5, then use this method, despite its deprecation.
Returns a hash function implementing the MD5 hash algorithm (128 hash bits).
public 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.

public 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.

public static HashFunction
murmur3_32(int seed)

Deprecated This implementation produces incorrect hash values from the HashFunction#hashString method if the string contains non-BMP characters.
Returns a hash function implementing the 32-bit murmur3 algorithm, x86 variant (little-endian variant), using the given seed value, with a known bug as described in the deprecation text.
public static HashFunction
murmur3_32()

Deprecated This implementation produces incorrect hash values from the HashFunction#hashString method if the string contains non-BMP characters.
Returns a hash function implementing the 32-bit murmur3 algorithm, x86 variant (little-endian variant), using the given seed value, with a known bug as described in the deprecation text.
public static HashFunction
murmur3_32_fixed(int seed)

Returns a hash function implementing the 32-bit murmur3 algorithm, x86 variant (little-endian variant), using the given seed value.

public static HashFunction
murmur3_32_fixed()

Returns a hash function implementing the 32-bit murmur3 algorithm, x86 variant (little-endian variant), using a seed value of zero.

public static HashFunction
sha1()

Deprecated If you must interoperate with a system that requires SHA-1, then use this method, despite its deprecation.
Returns a hash function implementing the SHA-1 algorithm (160 hash bits).
public static HashFunction
sha256()

Returns a hash function implementing the SHA-256 algorithm (256 hash bits).

public static HashFunction
sha384()

Returns a hash function implementing the SHA-384 algorithm (384 hash bits).

public static HashFunction
sha512()

Returns a hash function implementing the SHA-512 algorithm (512 hash bits).

public static HashFunction
sipHash24()

Returns a hash function implementing the 64-bit SipHash-2-4 algorithm using a seed value of k = 00 01 02 ...

public static HashFunction
sipHash24(long k0, long k1)

Returns a hash function implementing the 64-bit SipHash-2-4 algorithm using the given seed.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait