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