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;
019import static com.google.common.base.Preconditions.checkState;
020
021import com.google.common.annotations.Beta;
022import com.google.common.base.Preconditions;
023import com.google.common.primitives.Ints;
024import com.google.common.primitives.UnsignedInts;
025
026import java.io.Serializable;
027import java.security.MessageDigest;
028
029import javax.annotation.Nullable;
030
031/**
032 * An immutable hash code of arbitrary bit length.
033 *
034 * @author Dimitris Andreou
035 * @author Kurt Alfred Kluever
036 * @since 11.0
037 */
038@Beta
039public abstract class HashCode {
040  HashCode() {}
041
042  /**
043   * Returns the number of bits in this hash code; a positive multiple of 8.
044   */
045  public abstract int bits();
046
047  /**
048   * Returns the first four bytes of {@linkplain #asBytes() this hashcode's bytes}, converted to
049   * an {@code int} value in little-endian order.
050   *
051   * @throws IllegalStateException if {@code bits() < 32}
052   */
053  public abstract int asInt();
054
055  /**
056   * Returns the first eight bytes of {@linkplain #asBytes() this hashcode's bytes}, converted to
057   * a {@code long} value in little-endian order.
058   *
059   * @throws IllegalStateException if {@code bits() < 64}
060   */
061  public abstract long asLong();
062
063  /**
064   * If this hashcode has enough bits, returns {@code asLong()}, otherwise returns a {@code long}
065   * value with {@code asBytes()} as the least-significant bytes and {@code 0x00} as the remaining
066   * most-significant bytes.
067   *
068   * @since 14.0 (since 11.0 as {@code Hashing.padToLong(HashCode)})
069   */
070  public abstract long padToLong();
071
072  /**
073   * Returns the value of this hash code as a byte array. The caller may modify the byte array;
074   * changes to it will <i>not</i> be reflected in this {@code HashCode} object or any other arrays
075   * returned by this method.
076   */
077  // TODO(user): consider ByteString here, when that is available
078  public abstract byte[] asBytes();
079
080  /**
081   * Copies bytes from this hash code into {@code dest}.
082   *
083   * @param dest the byte array into which the hash code will be written
084   * @param offset the start offset in the data
085   * @param maxLength the maximum number of bytes to write
086   * @return the number of bytes written to {@code dest}
087   * @throws IndexOutOfBoundsException if there is not enough room in {@code dest}
088   */
089  public int writeBytesTo(byte[] dest, int offset, int maxLength) {
090    maxLength = Ints.min(maxLength, bits() / 8);
091    Preconditions.checkPositionIndexes(offset, offset + maxLength, dest.length);
092    writeBytesToImpl(dest, offset, maxLength);
093    return maxLength;
094  }
095
096  abstract void writeBytesToImpl(byte[] dest, int offset, int maxLength);
097
098  /**
099   * Creates a 32-bit {@code HashCode} representation of the given int value. The underlying bytes
100   * are interpreted in little endian order.
101   *
102   * @since 15.0 (since 12.0 in HashCodes)
103   */
104  public static HashCode fromInt(int hash) {
105    return new IntHashCode(hash);
106  }
107
108  private static final class IntHashCode extends HashCode implements Serializable {
109    final int hash;
110
111    IntHashCode(int hash) {
112      this.hash = hash;
113    }
114
115    @Override
116    public int bits() {
117      return 32;
118    }
119
120    @Override
121    public byte[] asBytes() {
122      return new byte[] {
123          (byte) hash,
124          (byte) (hash >> 8),
125          (byte) (hash >> 16),
126          (byte) (hash >> 24)};
127    }
128
129    @Override
130    public int asInt() {
131      return hash;
132    }
133
134    @Override
135    public long asLong() {
136      throw new IllegalStateException("this HashCode only has 32 bits; cannot create a long");
137    }
138
139    @Override
140    public long padToLong() {
141      return UnsignedInts.toLong(hash);
142    }
143
144    @Override
145    void writeBytesToImpl(byte[] dest, int offset, int maxLength) {
146      for (int i = 0; i < maxLength; i++) {
147        dest[offset + i] = (byte) (hash >> (i * 8));
148      }
149    }
150
151    private static final long serialVersionUID = 0;
152  }
153
154  /**
155   * Creates a 64-bit {@code HashCode} representation of the given long value. The underlying bytes
156   * are interpreted in little endian order.
157   *
158   * @since 15.0 (since 12.0 in HashCodes)
159   */
160  public static HashCode fromLong(long hash) {
161    return new LongHashCode(hash);
162  }
163
164  private static final class LongHashCode extends HashCode implements Serializable {
165    final long hash;
166
167    LongHashCode(long hash) {
168      this.hash = hash;
169    }
170
171    @Override
172    public int bits() {
173      return 64;
174    }
175
176    @Override
177    public byte[] asBytes() {
178      return new byte[] {
179          (byte) hash,
180          (byte) (hash >> 8),
181          (byte) (hash >> 16),
182          (byte) (hash >> 24),
183          (byte) (hash >> 32),
184          (byte) (hash >> 40),
185          (byte) (hash >> 48),
186          (byte) (hash >> 56)};
187    }
188
189    @Override
190    public int asInt() {
191      return (int) hash;
192    }
193
194    @Override
195    public long asLong() {
196      return hash;
197    }
198
199    @Override
200    public long padToLong() {
201      return hash;
202    }
203
204    @Override
205    void writeBytesToImpl(byte[] dest, int offset, int maxLength) {
206      for (int i = 0; i < maxLength; i++) {
207        dest[offset + i] = (byte) (hash >> (i * 8));
208      }
209    }
210
211    private static final long serialVersionUID = 0;
212  }
213
214  /**
215   * Creates a {@code HashCode} from a byte array. The array is defensively copied to preserve
216   * the immutability contract of {@code HashCode}. The array cannot be empty.
217   *
218   * @since 15.0 (since 12.0 in HashCodes)
219   */
220  public static HashCode fromBytes(byte[] bytes) {
221    checkArgument(bytes.length >= 1, "A HashCode must contain at least 1 byte.");
222    return fromBytesNoCopy(bytes.clone());
223  }
224
225  /**
226   * Creates a {@code HashCode} from a byte array. The array is <i>not</i> copied defensively,
227   * so it must be handed-off so as to preserve the immutability contract of {@code HashCode}.
228   */
229  static HashCode fromBytesNoCopy(byte[] bytes) {
230    return new BytesHashCode(bytes);
231  }
232
233  private static final class BytesHashCode extends HashCode implements Serializable {
234    final byte[] bytes;
235
236    BytesHashCode(byte[] bytes) {
237      this.bytes = checkNotNull(bytes);
238    }
239
240    @Override
241    public int bits() {
242      return bytes.length * 8;
243    }
244
245    @Override
246    public byte[] asBytes() {
247      return bytes.clone();
248    }
249
250    @Override
251    public int asInt() {
252      checkState(bytes.length >= 4,
253          "HashCode#asInt() requires >= 4 bytes (it only has %s bytes).", bytes.length);
254      return (bytes[0] & 0xFF)
255          | ((bytes[1] & 0xFF) << 8)
256          | ((bytes[2] & 0xFF) << 16)
257          | ((bytes[3] & 0xFF) << 24);
258    }
259
260    @Override
261    public long asLong() {
262      checkState(bytes.length >= 8,
263          "HashCode#asLong() requires >= 8 bytes (it only has %s bytes).", bytes.length);
264      return padToLong();
265    }
266
267    @Override
268    public long padToLong() {
269      long retVal = (bytes[0] & 0xFF);
270      for (int i = 1; i < Math.min(bytes.length, 8); i++) {
271        retVal |= (bytes[i] & 0xFFL) << (i * 8);
272      }
273      return retVal;
274    }
275
276    @Override
277    void writeBytesToImpl(byte[] dest, int offset, int maxLength) {
278      System.arraycopy(bytes, 0, dest, offset, maxLength);
279    }
280
281    private static final long serialVersionUID = 0;
282  }
283
284  /**
285   * Creates a {@code HashCode} from a hexadecimal ({@code base 16}) encoded string. The string must
286   * be at least 2 characters long, and contain only valid, lower-cased hexadecimal characters.
287   *
288   * <p>This method accepts the exact format generated by {@link #toString}. If you require more
289   * lenient {@code base 16} decoding, please use
290   * {@link com.google.common.io.BaseEncoding#decode} (and pass the result to {@link #fromBytes}).
291   *
292   * @since 15.0
293   */
294  public static HashCode fromString(String string) {
295    checkArgument(string.length() >= 2,
296        "input string (%s) must have at least 2 characters", string);
297    checkArgument(string.length() % 2 == 0,
298        "input string (%s) must have an even number of characters", string);
299
300    byte[] bytes = new byte[string.length() / 2];
301    for (int i = 0; i < string.length(); i += 2) {
302      int ch1 = decode(string.charAt(i)) << 4;
303      int ch2 = decode(string.charAt(i + 1));
304      bytes[i / 2] = (byte) (ch1 + ch2);
305    }
306    return fromBytesNoCopy(bytes);
307  }
308
309  private static int decode(char ch) {
310    if (ch >= '0' && ch <= '9') {
311      return ch - '0';
312    }
313    if (ch >= 'a' && ch <= 'f') {
314      return ch - 'a' + 10;
315    }
316    throw new IllegalArgumentException("Illegal hexadecimal character: " + ch);
317  }
318
319  @Override
320  public final boolean equals(@Nullable Object object) {
321    if (object instanceof HashCode) {
322      HashCode that = (HashCode) object;
323      // Undocumented: this is a non-short-circuiting equals(), in case this is a cryptographic
324      // hash code, in which case we don't want to leak timing information
325      return MessageDigest.isEqual(this.asBytes(), that.asBytes());
326    }
327    return false;
328  }
329
330  /**
331   * Returns a "Java hash code" for this {@code HashCode} instance; this is well-defined
332   * (so, for example, you can safely put {@code HashCode} instances into a {@code
333   * HashSet}) but is otherwise probably not what you want to use.
334   */
335  @Override
336  public final int hashCode() {
337    // If we have at least 4 bytes (32 bits), just take the first 4 bytes. Since this is
338    // already a (presumably) high-quality hash code, any four bytes of it will do.
339    if (bits() >= 32) {
340      return asInt();
341    }
342    // If we have less than 4 bytes, use them all.
343    byte[] bytes = asBytes();
344    int val = (bytes[0] & 0xFF);
345    for (int i = 1; i < bytes.length; i++) {
346      val |= ((bytes[i] & 0xFF) << (i * 8));
347    }
348    return val;
349  }
350
351  /**
352   * Returns a string containing each byte of {@link #asBytes}, in order, as a two-digit unsigned
353   * hexadecimal number in lower case.
354   *
355   * <p>Note that if the output is considered to be a single hexadecimal number, this hash code's
356   * bytes are the <i>big-endian</i> representation of that number. This may be surprising since
357   * everything else in the hashing API uniformly treats multibyte values as little-endian. But
358   * this format conveniently matches that of utilities such as the UNIX {@code md5sum} command.
359   *
360   * <p>To create a {@code HashCode} from its string representation, see {@link #fromString}.
361   */
362  @Override
363  public final String toString() {
364    byte[] bytes = asBytes();
365    StringBuilder sb = new StringBuilder(2 * bytes.length);
366    for (byte b : bytes) {
367      sb.append(hexDigits[(b >> 4) & 0xf]).append(hexDigits[b & 0xf]);
368    }
369    return sb.toString();
370  }
371
372  private static final char[] hexDigits = "0123456789abcdef".toCharArray();
373}