001/*
002 * Copyright (C) 2011 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.hash;
016
017import static com.google.common.base.Preconditions.checkArgument;
018import static com.google.common.base.Preconditions.checkNotNull;
019
020import com.google.common.annotations.Beta;
021import com.google.errorprone.annotations.Immutable;
022import java.security.Key;
023import java.util.ArrayList;
024import java.util.Arrays;
025import java.util.Iterator;
026import java.util.List;
027import java.util.zip.Adler32;
028import java.util.zip.CRC32;
029import java.util.zip.Checksum;
030import javax.crypto.spec.SecretKeySpec;
031import org.checkerframework.checker.nullness.qual.Nullable;
032
033/**
034 * Static methods to obtain {@link HashFunction} instances, and other static hashing-related
035 * utilities.
036 *
037 * <p>A comparison of the various hash functions can be found <a
038 * href="http://goo.gl/jS7HH">here</a>.
039 *
040 * @author Kevin Bourrillion
041 * @author Dimitris Andreou
042 * @author Kurt Alfred Kluever
043 * @since 11.0
044 */
045@Beta
046public final class Hashing {
047  /**
048   * Returns a general-purpose, <b>temporary-use</b>, non-cryptographic hash function. The algorithm
049   * the returned function implements is unspecified and subject to change without notice.
050   *
051   * <p><b>Warning:</b> a new random seed for these functions is chosen each time the {@code
052   * Hashing} class is loaded. <b>Do not use this method</b> if hash codes may escape the current
053   * process in any way, for example being sent over RPC, or saved to disk. For a general-purpose,
054   * non-cryptographic hash function that will never change behavior, we suggest {@link
055   * #murmur3_128}.
056   *
057   * <p>Repeated calls to this method on the same loaded {@code Hashing} class, using the same value
058   * for {@code minimumBits}, will return identically-behaving {@link HashFunction} instances.
059   *
060   * @param minimumBits a positive integer (can be arbitrarily large)
061   * @return a hash function, described above, that produces hash codes of length {@code
062   *     minimumBits} or greater
063   */
064  public static HashFunction goodFastHash(int minimumBits) {
065    int bits = checkPositiveAndMakeMultipleOf32(minimumBits);
066
067    if (bits == 32) {
068      return Murmur3_32HashFunction.GOOD_FAST_HASH_32;
069    }
070    if (bits <= 128) {
071      return Murmur3_128HashFunction.GOOD_FAST_HASH_128;
072    }
073
074    // Otherwise, join together some 128-bit murmur3s
075    int hashFunctionsNeeded = (bits + 127) / 128;
076    HashFunction[] hashFunctions = new HashFunction[hashFunctionsNeeded];
077    hashFunctions[0] = Murmur3_128HashFunction.GOOD_FAST_HASH_128;
078    int seed = GOOD_FAST_HASH_SEED;
079    for (int i = 1; i < hashFunctionsNeeded; i++) {
080      seed += 1500450271; // a prime; shouldn't matter
081      hashFunctions[i] = murmur3_128(seed);
082    }
083    return new ConcatenatedHashFunction(hashFunctions);
084  }
085
086  /**
087   * Used to randomize {@link #goodFastHash} instances, so that programs which persist anything
088   * dependent on the hash codes they produce will fail sooner.
089   */
090  static final int GOOD_FAST_HASH_SEED = (int) System.currentTimeMillis();
091
092  /**
093   * Returns a hash function implementing the <a
094   * href="https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp">32-bit murmur3
095   * algorithm, x86 variant</a> (little-endian variant), using the given seed value.
096   *
097   * <p>The exact C++ equivalent is the MurmurHash3_x86_32 function (Murmur3A).
098   */
099  public static HashFunction murmur3_32(int seed) {
100    return new Murmur3_32HashFunction(seed);
101  }
102
103  /**
104   * Returns a hash function implementing the <a
105   * href="https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp">32-bit murmur3
106   * algorithm, x86 variant</a> (little-endian variant), using a seed value of zero.
107   *
108   * <p>The exact C++ equivalent is the MurmurHash3_x86_32 function (Murmur3A).
109   */
110  public static HashFunction murmur3_32() {
111    return Murmur3_32HashFunction.MURMUR3_32;
112  }
113
114  /**
115   * Returns a hash function implementing the <a
116   * href="https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp">128-bit murmur3
117   * algorithm, x64 variant</a> (little-endian variant), using the given seed value.
118   *
119   * <p>The exact C++ equivalent is the MurmurHash3_x64_128 function (Murmur3F).
120   */
121  public static HashFunction murmur3_128(int seed) {
122    return new Murmur3_128HashFunction(seed);
123  }
124
125  /**
126   * Returns a hash function implementing the <a
127   * href="https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp">128-bit murmur3
128   * algorithm, x64 variant</a> (little-endian variant), using a seed value of zero.
129   *
130   * <p>The exact C++ equivalent is the MurmurHash3_x64_128 function (Murmur3F).
131   */
132  public static HashFunction murmur3_128() {
133    return Murmur3_128HashFunction.MURMUR3_128;
134  }
135
136  /**
137   * Returns a hash function implementing the <a href="https://131002.net/siphash/">64-bit
138   * SipHash-2-4 algorithm</a> using a seed value of {@code k = 00 01 02 ...}.
139   *
140   * @since 15.0
141   */
142  public static HashFunction sipHash24() {
143    return SipHashFunction.SIP_HASH_24;
144  }
145
146  /**
147   * Returns a hash function implementing the <a href="https://131002.net/siphash/">64-bit
148   * SipHash-2-4 algorithm</a> using the given seed.
149   *
150   * @since 15.0
151   */
152  public static HashFunction sipHash24(long k0, long k1) {
153    return new SipHashFunction(2, 4, k0, k1);
154  }
155
156  /**
157   * Returns a hash function implementing the MD5 hash algorithm (128 hash bits).
158   *
159   * @deprecated If you must interoperate with a system that requires MD5, then use this method,
160   *     despite its deprecation. But if you can choose your hash function, avoid MD5, which is
161   *     neither fast nor secure. As of January 2017, we suggest:
162   *     <ul>
163   *       <li>For security:
164   *           {@link Hashing#sha256} or a higher-level API.
165   *       <li>For speed: {@link Hashing#goodFastHash}, though see its docs for caveats.
166   *     </ul>
167   */
168  @Deprecated
169  public static HashFunction md5() {
170    return Md5Holder.MD5;
171  }
172
173  private static class Md5Holder {
174    static final HashFunction MD5 = new MessageDigestHashFunction("MD5", "Hashing.md5()");
175  }
176
177  /**
178   * Returns a hash function implementing the SHA-1 algorithm (160 hash bits).
179   *
180   * @deprecated If you must interoperate with a system that requires SHA-1, then use this method,
181   *     despite its deprecation. But if you can choose your hash function, avoid SHA-1, which is
182   *     neither fast nor secure. As of January 2017, we suggest:
183   *     <ul>
184   *       <li>For security:
185   *           {@link Hashing#sha256} or a higher-level API.
186   *       <li>For speed: {@link Hashing#goodFastHash}, though see its docs for caveats.
187   *     </ul>
188   */
189  @Deprecated
190  public static HashFunction sha1() {
191    return Sha1Holder.SHA_1;
192  }
193
194  private static class Sha1Holder {
195    static final HashFunction SHA_1 = new MessageDigestHashFunction("SHA-1", "Hashing.sha1()");
196  }
197
198  /** Returns a hash function implementing the SHA-256 algorithm (256 hash bits). */
199  public static HashFunction sha256() {
200    return Sha256Holder.SHA_256;
201  }
202
203  private static class Sha256Holder {
204    static final HashFunction SHA_256 =
205        new MessageDigestHashFunction("SHA-256", "Hashing.sha256()");
206  }
207
208  /**
209   * Returns a hash function implementing the SHA-384 algorithm (384 hash bits).
210   *
211   * @since 19.0
212   */
213  public static HashFunction sha384() {
214    return Sha384Holder.SHA_384;
215  }
216
217  private static class Sha384Holder {
218    static final HashFunction SHA_384 =
219        new MessageDigestHashFunction("SHA-384", "Hashing.sha384()");
220  }
221
222  /** Returns a hash function implementing the SHA-512 algorithm (512 hash bits). */
223  public static HashFunction sha512() {
224    return Sha512Holder.SHA_512;
225  }
226
227  private static class Sha512Holder {
228    static final HashFunction SHA_512 =
229        new MessageDigestHashFunction("SHA-512", "Hashing.sha512()");
230  }
231
232  /**
233   * Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the
234   * MD5 (128 hash bits) hash function and the given secret key.
235   *
236   *
237   * @param key the secret key
238   * @throws IllegalArgumentException if the given key is inappropriate for initializing this MAC
239   * @since 20.0
240   */
241  public static HashFunction hmacMd5(Key key) {
242    return new MacHashFunction("HmacMD5", key, hmacToString("hmacMd5", key));
243  }
244
245  /**
246   * Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the
247   * MD5 (128 hash bits) hash function and a {@link SecretKeySpec} created from the given byte array
248   * and the MD5 algorithm.
249   *
250   *
251   * @param key the key material of the secret key
252   * @since 20.0
253   */
254  public static HashFunction hmacMd5(byte[] key) {
255    return hmacMd5(new SecretKeySpec(checkNotNull(key), "HmacMD5"));
256  }
257
258  /**
259   * Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the
260   * SHA-1 (160 hash bits) hash function and the given secret key.
261   *
262   *
263   * @param key the secret key
264   * @throws IllegalArgumentException if the given key is inappropriate for initializing this MAC
265   * @since 20.0
266   */
267  public static HashFunction hmacSha1(Key key) {
268    return new MacHashFunction("HmacSHA1", key, hmacToString("hmacSha1", key));
269  }
270
271  /**
272   * Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the
273   * SHA-1 (160 hash bits) hash function and a {@link SecretKeySpec} created from the given byte
274   * array and the SHA-1 algorithm.
275   *
276   *
277   * @param key the key material of the secret key
278   * @since 20.0
279   */
280  public static HashFunction hmacSha1(byte[] key) {
281    return hmacSha1(new SecretKeySpec(checkNotNull(key), "HmacSHA1"));
282  }
283
284  /**
285   * Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the
286   * SHA-256 (256 hash bits) hash function and the given secret key.
287   *
288   *
289   * @param key the secret key
290   * @throws IllegalArgumentException if the given key is inappropriate for initializing this MAC
291   * @since 20.0
292   */
293  public static HashFunction hmacSha256(Key key) {
294    return new MacHashFunction("HmacSHA256", key, hmacToString("hmacSha256", key));
295  }
296
297  /**
298   * Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the
299   * SHA-256 (256 hash bits) hash function and a {@link SecretKeySpec} created from the given byte
300   * array and the SHA-256 algorithm.
301   *
302   *
303   * @param key the key material of the secret key
304   * @since 20.0
305   */
306  public static HashFunction hmacSha256(byte[] key) {
307    return hmacSha256(new SecretKeySpec(checkNotNull(key), "HmacSHA256"));
308  }
309
310  /**
311   * Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the
312   * SHA-512 (512 hash bits) hash function and the given secret key.
313   *
314   *
315   * @param key the secret key
316   * @throws IllegalArgumentException if the given key is inappropriate for initializing this MAC
317   * @since 20.0
318   */
319  public static HashFunction hmacSha512(Key key) {
320    return new MacHashFunction("HmacSHA512", key, hmacToString("hmacSha512", key));
321  }
322
323  /**
324   * Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the
325   * SHA-512 (512 hash bits) hash function and a {@link SecretKeySpec} created from the given byte
326   * array and the SHA-512 algorithm.
327   *
328   *
329   * @param key the key material of the secret key
330   * @since 20.0
331   */
332  public static HashFunction hmacSha512(byte[] key) {
333    return hmacSha512(new SecretKeySpec(checkNotNull(key), "HmacSHA512"));
334  }
335
336  private static String hmacToString(String methodName, Key key) {
337    return String.format(
338        "Hashing.%s(Key[algorithm=%s, format=%s])",
339        methodName, key.getAlgorithm(), key.getFormat());
340  }
341
342  /**
343   * Returns a hash function implementing the CRC32C checksum algorithm (32 hash bits) as described
344   * by RFC 3720, Section 12.1.
345   *
346   * <p>This function is best understood as a <a
347   * href="https://en.wikipedia.org/wiki/Checksum">checksum</a> rather than a true <a
348   * href="https://en.wikipedia.org/wiki/Hash_function">hash function</a>.
349   *
350   * @since 18.0
351   */
352  public static HashFunction crc32c() {
353    return Crc32cHashFunction.CRC_32_C;
354  }
355
356  /**
357   * Returns a hash function implementing the CRC-32 checksum algorithm (32 hash bits).
358   *
359   * <p>To get the {@code long} value equivalent to {@link Checksum#getValue()} for a {@code
360   * HashCode} produced by this function, use {@link HashCode#padToLong()}.
361   *
362   * <p>This function is best understood as a <a
363   * href="https://en.wikipedia.org/wiki/Checksum">checksum</a> rather than a true <a
364   * href="https://en.wikipedia.org/wiki/Hash_function">hash function</a>.
365   *
366   * @since 14.0
367   */
368  public static HashFunction crc32() {
369    return ChecksumType.CRC_32.hashFunction;
370  }
371
372  /**
373   * Returns a hash function implementing the Adler-32 checksum algorithm (32 hash bits).
374   *
375   * <p>To get the {@code long} value equivalent to {@link Checksum#getValue()} for a {@code
376   * HashCode} produced by this function, use {@link HashCode#padToLong()}.
377   *
378   * <p>This function is best understood as a <a
379   * href="https://en.wikipedia.org/wiki/Checksum">checksum</a> rather than a true <a
380   * href="https://en.wikipedia.org/wiki/Hash_function">hash function</a>.
381   *
382   * @since 14.0
383   */
384  public static HashFunction adler32() {
385    return ChecksumType.ADLER_32.hashFunction;
386  }
387
388  @Immutable
389  enum ChecksumType implements ImmutableSupplier<Checksum> {
390    CRC_32("Hashing.crc32()") {
391      @Override
392      public Checksum get() {
393        return new CRC32();
394      }
395    },
396    ADLER_32("Hashing.adler32()") {
397      @Override
398      public Checksum get() {
399        return new Adler32();
400      }
401    };
402
403    public final HashFunction hashFunction;
404
405    ChecksumType(String toString) {
406      this.hashFunction = new ChecksumHashFunction(this, 32, toString);
407    }
408  }
409
410  /**
411   * Returns a hash function implementing FarmHash's Fingerprint64, an open-source algorithm.
412   *
413   * <p>This is designed for generating persistent fingerprints of strings. It isn't
414   * cryptographically secure, but it produces a high-quality hash with fewer collisions than some
415   * alternatives we've used in the past.
416   *
417   * <p>FarmHash fingerprints are encoded by {@link HashCode#asBytes} in little-endian order. This
418   * means {@link HashCode#asLong} is guaranteed to return the same value that
419   * farmhash::Fingerprint64() would for the same input (when compared using {@link
420   * com.google.common.primitives.UnsignedLongs}'s encoding of 64-bit unsigned numbers).
421   *
422   * <p>This function is best understood as a <a
423   * href="https://en.wikipedia.org/wiki/Fingerprint_(computing)">fingerprint</a> rather than a true
424   * <a href="https://en.wikipedia.org/wiki/Hash_function">hash function</a>.
425   *
426   * @since 20.0
427   */
428  public static HashFunction farmHashFingerprint64() {
429    return FarmHashFingerprint64.FARMHASH_FINGERPRINT_64;
430  }
431
432  /**
433   * Assigns to {@code hashCode} a "bucket" in the range {@code [0, buckets)}, in a uniform manner
434   * that minimizes the need for remapping as {@code buckets} grows. That is, {@code
435   * consistentHash(h, n)} equals:
436   *
437   * <ul>
438   *   <li>{@code n - 1}, with approximate probability {@code 1/n}
439   *   <li>{@code consistentHash(h, n - 1)}, otherwise (probability {@code 1 - 1/n})
440   * </ul>
441   *
442   * <p>This method is suitable for the common use case of dividing work among buckets that meet the
443   * following conditions:
444   *
445   * <ul>
446   *   <li>You want to assign the same fraction of inputs to each bucket.
447   *   <li>When you reduce the number of buckets, you can accept that the most recently added
448   *       buckets will be removed first. More concretely, if you are dividing traffic among tasks,
449   *       you can decrease the number of tasks from 15 and 10, killing off the final 5 tasks, and
450   *       {@code consistentHash} will handle it. If, however, you are dividing traffic among
451   *       servers {@code alpha}, {@code bravo}, and {@code charlie} and you occasionally need to
452   *       take each of the servers offline, {@code consistentHash} will be a poor fit: It provides
453   *       no way for you to specify which of the three buckets is disappearing. Thus, if your
454   *       buckets change from {@code [alpha, bravo, charlie]} to {@code [bravo, charlie]}, it will
455   *       assign all the old {@code alpha} traffic to {@code bravo} and all the old {@code bravo}
456   *       traffic to {@code charlie}, rather than letting {@code bravo} keep its traffic.
457   * </ul>
458   *
459   *
460   * <p>See the <a href="http://en.wikipedia.org/wiki/Consistent_hashing">Wikipedia article on
461   * consistent hashing</a> for more information.
462   */
463  public static int consistentHash(HashCode hashCode, int buckets) {
464    return consistentHash(hashCode.padToLong(), buckets);
465  }
466
467  /**
468   * Assigns to {@code input} a "bucket" in the range {@code [0, buckets)}, in a uniform manner that
469   * minimizes the need for remapping as {@code buckets} grows. That is, {@code consistentHash(h,
470   * n)} equals:
471   *
472   * <ul>
473   *   <li>{@code n - 1}, with approximate probability {@code 1/n}
474   *   <li>{@code consistentHash(h, n - 1)}, otherwise (probability {@code 1 - 1/n})
475   * </ul>
476   *
477   * <p>This method is suitable for the common use case of dividing work among buckets that meet the
478   * following conditions:
479   *
480   * <ul>
481   *   <li>You want to assign the same fraction of inputs to each bucket.
482   *   <li>When you reduce the number of buckets, you can accept that the most recently added
483   *       buckets will be removed first. More concretely, if you are dividing traffic among tasks,
484   *       you can decrease the number of tasks from 15 and 10, killing off the final 5 tasks, and
485   *       {@code consistentHash} will handle it. If, however, you are dividing traffic among
486   *       servers {@code alpha}, {@code bravo}, and {@code charlie} and you occasionally need to
487   *       take each of the servers offline, {@code consistentHash} will be a poor fit: It provides
488   *       no way for you to specify which of the three buckets is disappearing. Thus, if your
489   *       buckets change from {@code [alpha, bravo, charlie]} to {@code [bravo, charlie]}, it will
490   *       assign all the old {@code alpha} traffic to {@code bravo} and all the old {@code bravo}
491   *       traffic to {@code charlie}, rather than letting {@code bravo} keep its traffic.
492   * </ul>
493   *
494   *
495   * <p>See the <a href="http://en.wikipedia.org/wiki/Consistent_hashing">Wikipedia article on
496   * consistent hashing</a> for more information.
497   */
498  public static int consistentHash(long input, int buckets) {
499    checkArgument(buckets > 0, "buckets must be positive: %s", buckets);
500    LinearCongruentialGenerator generator = new LinearCongruentialGenerator(input);
501    int candidate = 0;
502    int next;
503
504    // Jump from bucket to bucket until we go out of range
505    while (true) {
506      next = (int) ((candidate + 1) / generator.nextDouble());
507      if (next >= 0 && next < buckets) {
508        candidate = next;
509      } else {
510        return candidate;
511      }
512    }
513  }
514
515  /**
516   * Returns a hash code, having the same bit length as each of the input hash codes, that combines
517   * the information of these hash codes in an ordered fashion. That is, whenever two equal hash
518   * codes are produced by two calls to this method, it is <i>as likely as possible</i> that each
519   * was computed from the <i>same</i> input hash codes in the <i>same</i> order.
520   *
521   * @throws IllegalArgumentException if {@code hashCodes} is empty, or the hash codes do not all
522   *     have the same bit length
523   */
524  public static HashCode combineOrdered(Iterable<HashCode> hashCodes) {
525    Iterator<HashCode> iterator = hashCodes.iterator();
526    checkArgument(iterator.hasNext(), "Must be at least 1 hash code to combine.");
527    int bits = iterator.next().bits();
528    byte[] resultBytes = new byte[bits / 8];
529    for (HashCode hashCode : hashCodes) {
530      byte[] nextBytes = hashCode.asBytes();
531      checkArgument(
532          nextBytes.length == resultBytes.length, "All hashcodes must have the same bit length.");
533      for (int i = 0; i < nextBytes.length; i++) {
534        resultBytes[i] = (byte) (resultBytes[i] * 37 ^ nextBytes[i]);
535      }
536    }
537    return HashCode.fromBytesNoCopy(resultBytes);
538  }
539
540  /**
541   * Returns a hash code, having the same bit length as each of the input hash codes, that combines
542   * the information of these hash codes in an unordered fashion. That is, whenever two equal hash
543   * codes are produced by two calls to this method, it is <i>as likely as possible</i> that each
544   * was computed from the <i>same</i> input hash codes in <i>some</i> order.
545   *
546   * @throws IllegalArgumentException if {@code hashCodes} is empty, or the hash codes do not all
547   *     have the same bit length
548   */
549  public static HashCode combineUnordered(Iterable<HashCode> hashCodes) {
550    Iterator<HashCode> iterator = hashCodes.iterator();
551    checkArgument(iterator.hasNext(), "Must be at least 1 hash code to combine.");
552    byte[] resultBytes = new byte[iterator.next().bits() / 8];
553    for (HashCode hashCode : hashCodes) {
554      byte[] nextBytes = hashCode.asBytes();
555      checkArgument(
556          nextBytes.length == resultBytes.length, "All hashcodes must have the same bit length.");
557      for (int i = 0; i < nextBytes.length; i++) {
558        resultBytes[i] += nextBytes[i];
559      }
560    }
561    return HashCode.fromBytesNoCopy(resultBytes);
562  }
563
564  /** Checks that the passed argument is positive, and ceils it to a multiple of 32. */
565  static int checkPositiveAndMakeMultipleOf32(int bits) {
566    checkArgument(bits > 0, "Number of bits must be positive");
567    return (bits + 31) & ~31;
568  }
569
570  /**
571   * Returns a hash function which computes its hash code by concatenating the hash codes of the
572   * underlying hash functions together. This can be useful if you need to generate hash codes of a
573   * specific length.
574   *
575   * <p>For example, if you need 1024-bit hash codes, you could join two {@link Hashing#sha512} hash
576   * functions together: {@code Hashing.concatenating(Hashing.sha512(), Hashing.sha512())}.
577   *
578   * @since 19.0
579   */
580  public static HashFunction concatenating(
581      HashFunction first, HashFunction second, HashFunction... rest) {
582    // We can't use Lists.asList() here because there's no hash->collect dependency
583    List<HashFunction> list = new ArrayList<>();
584    list.add(first);
585    list.add(second);
586    list.addAll(Arrays.asList(rest));
587    return new ConcatenatedHashFunction(list.toArray(new HashFunction[0]));
588  }
589
590  /**
591   * Returns a hash function which computes its hash code by concatenating the hash codes of the
592   * underlying hash functions together. This can be useful if you need to generate hash codes of a
593   * specific length.
594   *
595   * <p>For example, if you need 1024-bit hash codes, you could join two {@link Hashing#sha512} hash
596   * functions together: {@code Hashing.concatenating(Hashing.sha512(), Hashing.sha512())}.
597   *
598   * @since 19.0
599   */
600  public static HashFunction concatenating(Iterable<HashFunction> hashFunctions) {
601    checkNotNull(hashFunctions);
602    // We can't use Iterables.toArray() here because there's no hash->collect dependency
603    List<HashFunction> list = new ArrayList<>();
604    for (HashFunction hashFunction : hashFunctions) {
605      list.add(hashFunction);
606    }
607    checkArgument(list.size() > 0, "number of hash functions (%s) must be > 0", list.size());
608    return new ConcatenatedHashFunction(list.toArray(new HashFunction[0]));
609  }
610
611  private static final class ConcatenatedHashFunction extends AbstractCompositeHashFunction {
612
613    private ConcatenatedHashFunction(HashFunction... functions) {
614      super(functions);
615      for (HashFunction function : functions) {
616        checkArgument(
617            function.bits() % 8 == 0,
618            "the number of bits (%s) in hashFunction (%s) must be divisible by 8",
619            function.bits(),
620            function);
621      }
622    }
623
624    @Override
625    HashCode makeHash(Hasher[] hashers) {
626      byte[] bytes = new byte[bits() / 8];
627      int i = 0;
628      for (Hasher hasher : hashers) {
629        HashCode newHash = hasher.hash();
630        i += newHash.writeBytesTo(bytes, i, newHash.bits() / 8);
631      }
632      return HashCode.fromBytesNoCopy(bytes);
633    }
634
635    @Override
636    public int bits() {
637      int bitSum = 0;
638      for (HashFunction function : functions) {
639        bitSum += function.bits();
640      }
641      return bitSum;
642    }
643
644    @Override
645    public boolean equals(@Nullable Object object) {
646      if (object instanceof ConcatenatedHashFunction) {
647        ConcatenatedHashFunction other = (ConcatenatedHashFunction) object;
648        return Arrays.equals(functions, other.functions);
649      }
650      return false;
651    }
652
653    @Override
654    public int hashCode() {
655      return Arrays.hashCode(functions);
656    }
657  }
658
659  /**
660   * Linear CongruentialGenerator to use for consistent hashing. See
661   * http://en.wikipedia.org/wiki/Linear_congruential_generator
662   */
663  private static final class LinearCongruentialGenerator {
664    private long state;
665
666    public LinearCongruentialGenerator(long seed) {
667      this.state = seed;
668    }
669
670    public double nextDouble() {
671      state = 2862933555777941757L * state + 1;
672      return ((double) ((int) (state >>> 33) + 1)) / (0x1.0p31);
673    }
674  }
675
676  private Hashing() {}
677}