com.google.common.hash
Class Hashing

java.lang.Object
  extended by com.google.common.hash.Hashing

@Beta
public final class Hashing
extends java.lang.Object

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

Since:
11.0

Method Summary
static HashFunction adler32()
          Returns a hash function implementing the Adler-32 checksum algorithm (32 hash bits) by delegating to the Adler32 Checksum.
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 goodFastHash(int minimumBits)
          Returns a general-purpose, non-cryptographic-strength, streaming hash function that produces hash codes of length at least minimumBits.
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 long padToLong(HashCode hashCode)
          Deprecated. Use HashCode.padToLong() instead. This method is scheduled to be removed in Guava 15.0.
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

goodFastHash

public static HashFunction goodFastHash(int minimumBits)
Returns a general-purpose, non-cryptographic-strength, streaming hash function that produces hash codes of length at least minimumBits. Users without specific compatibility requirements and who do not persist the hash codes are encouraged to choose this hash function.

Repeated calls to goodFastHash(int) with the same minimumBits value will return HashFunction instances with identical behavior (but not necessarily the same instance) for the duration of the current virtual machine.

Warning: the implementation is unspecified and is subject to change.

Throws:
java.lang.IllegalArgumentException - if minimumBits is not positive

murmur3_32

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

The exact C++ equivalent is the MurmurHash3_x86_32 function (Murmur3A).


murmur3_32

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

The exact C++ equivalent is the MurmurHash3_x86_32 function (Murmur3A).


murmur3_128

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.

The exact C++ equivalent is the MurmurHash3_x64_128 function (Murmur3F).


murmur3_128

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.

The exact C++ equivalent is the MurmurHash3_x64_128 function (Murmur3F).


md5

public static HashFunction md5()
Returns a hash function implementing the MD5 hash algorithm (128 hash bits) by delegating to the MD5 MessageDigest.


sha1

public static HashFunction sha1()
Returns a hash function implementing the SHA-1 algorithm (160 hash bits) by delegating to the SHA-1 MessageDigest.


sha256

public static HashFunction sha256()
Returns a hash function implementing the SHA-256 algorithm (256 hash bits) by delegating to the SHA-256 MessageDigest.


sha512

public static HashFunction sha512()
Returns a hash function implementing the SHA-512 algorithm (512 hash bits) by delegating to the SHA-512 MessageDigest.


crc32

public static HashFunction crc32()
Returns a hash function implementing the CRC-32 checksum algorithm (32 hash bits) by delegating to the CRC32 Checksum.

To get the long value equivalent to Checksum.getValue() for a HashCode produced by this function, use HashCode.padToLong().

Since:
14.0

adler32

public static HashFunction adler32()
Returns a hash function implementing the Adler-32 checksum algorithm (32 hash bits) by delegating to the Adler32 Checksum.

To get the long value equivalent to Checksum.getValue() for a HashCode produced by this function, use HashCode.padToLong().

Since:
14.0

padToLong

@Deprecated
public static long padToLong(HashCode hashCode)
Deprecated. Use HashCode.padToLong() instead. This method is scheduled to be removed in Guava 15.0.

If hashCode has enough bits, returns hashCode.asLong(), otherwise returns a long value with hashCode.asInt() as the least-significant four bytes and 0x00 as each of the most-significant four bytes.


consistentHash

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. That is, consistentHash(h, n) equals:

See the wikipedia article on consistent hashing for more information.

If you might want to have weights for the buckets in the future, take a look at weightedConsistentHash.


consistentHash

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. That is, consistentHash(h, n) equals:

See the wikipedia article on consistent hashing for more information.

If you might want to have weights for the buckets in the future, take a look at weightedConsistentHash.


combineOrdered

public 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. 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:
java.lang.IllegalArgumentException - if hashCodes is empty, or the hash codes do not all have the same bit length

combineUnordered

public 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. 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:
java.lang.IllegalArgumentException - if hashCodes is empty, or the hash codes do not all have the same bit length