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