Refine search
/** * Hashes the contents of this byte source using the given hash function. * * @throws IOException if an I/O error occurs while reading from this source */ public HashCode hash(HashFunction hashFunction) throws IOException { Hasher hasher = hashFunction.newHasher(); copyTo(Funnels.asOutputStream(hasher)); return hasher.hash(); }
/** * Hashes the contents of this byte source using the given hash function. * * @throws IOException if an I/O error occurs while reading from this source */ public HashCode hash(HashFunction hashFunction) throws IOException { Hasher hasher = hashFunction.newHasher(); copyTo(Funnels.asOutputStream(hasher)); return hasher.hash(); }
/** * Hashes the contents of this byte source using the given hash function. * * @throws IOException if an I/O error occurs while reading from this source */ public HashCode hash(HashFunction hashFunction) throws IOException { Hasher hasher = hashFunction.newHasher(); copyTo(Funnels.asOutputStream(hasher)); return hasher.hash(); }
private static void assertHashByteBufferEquivalence(HashFunction hashFunction, Random random) { int size = random.nextInt(2048); byte[] bytes = new byte[size]; random.nextBytes(bytes); assertEquals( hashFunction.hashBytes(ByteBuffer.wrap(bytes)), hashFunction.newHasher(size).putBytes(ByteBuffer.wrap(bytes)).hash()); int off = random.nextInt(size); int len = random.nextInt(size - off); assertEquals( hashFunction.hashBytes(ByteBuffer.wrap(bytes, off, len)), hashFunction.newHasher(size).putBytes(ByteBuffer.wrap(bytes, off, len)).hash()); }
private static void assertHashBytesEquivalence(HashFunction hashFunction, Random random) { int size = random.nextInt(2048); byte[] bytes = new byte[size]; random.nextBytes(bytes); assertEquals( hashFunction.hashBytes(bytes), hashFunction.newHasher(size).putBytes(bytes).hash()); int off = random.nextInt(size); int len = random.nextInt(size - off); assertEquals( hashFunction.hashBytes(bytes, off, len), hashFunction.newHasher(size).putBytes(bytes, off, len).hash()); }
static void assertHasherByteBufferPreservesByteOrder(HashFunction hashFunction) { Random rng = new Random(0L); byte[] bytes = new byte[rng.nextInt(256) + 1]; rng.nextBytes(bytes); ByteBuffer littleEndian = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN); ByteBuffer bigEndian = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN); assertEquals( hashFunction.newHasher().putBytes(littleEndian).hash(), hashFunction.newHasher().putBytes(littleEndian).hash()); assertEquals(ByteOrder.LITTLE_ENDIAN, littleEndian.order()); assertEquals(ByteOrder.BIG_ENDIAN, littleEndian.order()); }
public void testHashTwice() { Hasher hasher = Hashing.hmacMd5(MD5_KEY).newHasher(); assertEquals( "9753980fe94daa8ecaa82216519393a9", hasher.putString("The quick brown fox jumps over the lazy dog", UTF_8).hash().toString()); try { hasher.hash(); fail(); } catch (IllegalStateException expected) { } }
public void testHashTwice() { Hasher sha1 = Hashing.sha1().newHasher(); assertEquals( "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", sha1.putString("The quick brown fox jumps over the lazy dog", Charsets.UTF_8) .hash() .toString()); try { sha1.hash(); fail(); } catch (IllegalStateException expected) { } }
public void testEmptyInputs_mixedAlgorithms() throws Exception { String knownOutput = "8cbf764cbe2e4623d99a41354adfd390"; Mac mac = Mac.getInstance("HmacMD5"); mac.init(SHA1_KEY); assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal()).toString()); assertEquals(knownOutput, Hashing.hmacMd5(SHA1_KEY).newHasher().hash().toString()); }
public void testEmptyInputs() throws Exception { String knownOutput = "8cbf764cbe2e4623d99a41354adfd390"; Mac mac = Mac.getInstance("HmacMD5"); mac.init(MD5_KEY); assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal()).toString()); assertEquals(knownOutput, Hashing.hmacMd5(MD5_KEY).newHasher().hash().toString()); }
public void testMultipleUpdates() throws Exception { Mac mac = Mac.getInstance("HmacSHA1"); mac.init(SHA1_KEY); mac.update("hello".getBytes(UTF_8)); mac.update("world".getBytes(UTF_8)); assertEquals( HashCode.fromBytes(mac.doFinal()), Hashing.hmacSha1(SHA1_KEY) .newHasher() .putString("hello", UTF_8) .putString("world", UTF_8) .hash()); }
static void hashRow( ColumnSelectorPlus<CardinalityAggregatorColumnSelectorStrategy>[] selectorPluses, HyperLogLogCollector collector ) { final Hasher hasher = hashFn.newHasher(); for (int k = 0; k < selectorPluses.length; ++k) { if (k != 0) { hasher.putByte((byte) 0); } ColumnSelectorPlus<CardinalityAggregatorColumnSelectorStrategy> selectorPlus = selectorPluses[k]; selectorPlus.getColumnSelectorStrategy().hashRow(selectorPlus.getSelector(), hasher); } collector.add(hasher.hash().asBytes()); }