Hashing
Date: 2016-05-24

com.google.common.hash

## Class Hashing

- java.lang.Object
- - com.google.common.hash.Hashing

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

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

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

  Since:
    11.0

- - ### Method Summary

    Methods
    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`.
    `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 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.
    `static HashFunction` | `concatenating(java.lang.Iterable<HashFunction> hashFunctions)` Returns a hash function which computes its hash code by concatenating the hash codes of the underlying hash functions together.
    `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` | `sha384()` Returns a hash function implementing the SHA-384 algorithm (384 hash bits) by delegating to the SHA-384 `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.

    - ### 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, 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 `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.

      Parameters:
        `minimumBits` - a positive integer (can be arbitrarily large)
      Returns:
        a hash function, described above, that produces hash codes of length `minimumBits` or greater

    - #### 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).

    - #### sipHash24

          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 ...`.
      Since:
        15.0

    - #### sipHash24

          public static HashFunction sipHash24(long k0,
                               long k1)

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

    - #### 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`.

    - #### sha384

          public static HashFunction sha384()

      Returns a hash function implementing the SHA-384 algorithm (384 hash bits) by delegating to the SHA-384 `MessageDigest`.
      Since:
        19.0

    - #### sha512

          public static HashFunction sha512()

      Returns a hash function implementing the SHA-512 algorithm (512 hash bits) by delegating to the SHA-512 `MessageDigest`.

    - #### crc32c

          public static HashFunction 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

          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

    - #### 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:
      - `n - 1`, with approximate probability `1/n`
      - `consistentHash(h, n - 1)`, otherwise (probability `1 - 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 `consistentHash` will handle it. If, however, you are dividing traffic among servers `alpha`, `bravo`, and `charlie` and you occasionally need to take each of the servers offline, `consistentHash` will 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 old `alpha` traffic to `bravo` and all the old `bravo` traffic to `charlie`, rather than letting `bravo` keep its traffic.

      See the Wikipedia article on consistent hashing for more information.

    - #### 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:
      - `n - 1`, with approximate probability `1/n`
      - `consistentHash(h, n - 1)`, otherwise (probability `1 - 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 `consistentHash` will handle it. If, however, you are dividing traffic among servers `alpha`, `bravo`, and `charlie` and you occasionally need to take each of the servers offline, `consistentHash` will 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 old `alpha` traffic to `bravo` and all the old `bravo` traffic to `charlie`, rather than letting `bravo` keep its traffic.

      See the Wikipedia article on consistent hashing for more information.

    - #### 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

    - #### 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

          public static HashFunction concatenating(java.lang.Iterable<HashFunction> hashFunctions)

      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