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