/** * Returns a mutable view of the underlying bytes for the given {@code HashCode} if it is a * byte-based hashcode. Otherwise it returns {@link HashCode#asBytes}. Do <i>not</i> mutate this * array or else you will break the immutability contract of {@code HashCode}. */ byte[] getBytesInternal() { return asBytes(); }
@Override public byte[] hash(byte[] input, int seed) { return murmur3_32(seed).hashBytes(input).asBytes(); } };
@Description("compute md5 hash") @ScalarFunction @SqlType(StandardTypes.VARBINARY) public static Slice md5(@SqlType(StandardTypes.VARBINARY) Slice slice) { return Slices.wrappedBuffer(Hashing.md5().hashBytes(slice.getBytes()).asBytes()); }
@Override public byte[] hash(byte[] input, int seed) { Hasher hasher = murmur3_32(seed).newHasher(); Funnels.byteArrayFunnel().funnel(input, hasher); return hasher.hash().asBytes(); } };
@Override public byte[] hash(byte[] input, int seed) { Hasher hasher = murmur3_128(seed).newHasher(); Funnels.byteArrayFunnel().funnel(input, hasher); return hasher.hash().asBytes(); } };
private void fillCollector(HyperLogLogCollector collector) { Random rand = new Random(758190); for (long i = 0; i < NUM_HASHES; ++i) { collector.add(hashFunction.hashLong(rand.nextLong()).asBytes()); } }
@Description("compute sha1 hash") @ScalarFunction @SqlType(StandardTypes.VARBINARY) public static Slice sha1(@SqlType(StandardTypes.VARBINARY) Slice slice) { return Slices.wrappedBuffer(Hashing.sha1().hashBytes(slice.getBytes()).asBytes()); }
@Description("compute sha512 hash") @ScalarFunction @SqlType(StandardTypes.VARBINARY) public static Slice sha512(@SqlType(StandardTypes.VARBINARY) Slice slice) { return Slices.wrappedBuffer(Hashing.sha512().hashBytes(slice.getBytes()).asBytes()); }
@Description("compute sha256 hash") @ScalarFunction @SqlType(StandardTypes.VARBINARY) public static Slice sha256(@SqlType(StandardTypes.VARBINARY) Slice slice) { return Slices.wrappedBuffer(Hashing.sha256().hashBytes(slice.getBytes()).asBytes()); }
public void testConcatenatingHashFunction_makeHash() { byte[] md5Hash = Hashing.md5().hashLong(42L).asBytes(); byte[] murmur3Hash = Hashing.murmur3_32().hashLong(42L).asBytes(); byte[] combined = new byte[md5Hash.length + murmur3Hash.length]; ByteBuffer buffer = ByteBuffer.wrap(combined); buffer.put(md5Hash); buffer.put(murmur3Hash); HashCode expected = HashCode.fromBytes(combined); assertEquals( expected, Hashing.concatenating(Hashing.md5(), Hashing.murmur3_32()).hashLong(42L)); assertEquals( expected, Hashing.concatenating(asList(Hashing.md5(), Hashing.murmur3_32())).hashLong(42L)); }
private static void assertReadableBytes(HashCode hashCode) { assertTrue(hashCode.bits() >= 32); // sanity byte[] hashBytes = hashCode.asBytes(); int totalBytes = hashCode.bits() / 8; for (int bytes = 0; bytes < totalBytes; bytes++) { byte[] bb = new byte[bytes]; hashCode.writeBytesTo(bb, 0, bb.length); assertTrue(Arrays.equals(Arrays.copyOf(hashBytes, bytes), bb)); } }
public void testIntWriteBytesTo() { byte[] dest = new byte[4]; HashCode.fromInt(42).writeBytesTo(dest, 0, 4); assertTrue(Arrays.equals(HashCode.fromInt(42).asBytes(), dest)); }
public void testLongWriteBytesTo() { byte[] dest = new byte[8]; HashCode.fromLong(42).writeBytesTo(dest, 0, 8); assertTrue(Arrays.equals(HashCode.fromLong(42).asBytes(), dest)); }
@Description("Compute HMAC with MD5") @ScalarFunction @SqlType(StandardTypes.VARBINARY) public static Slice hmacMd5(@SqlType(StandardTypes.VARBINARY) Slice slice, @SqlType(StandardTypes.VARBINARY) Slice key) { return wrappedBuffer(Hashing.hmacMd5(key.getBytes()).hashBytes(slice.getBytes()).asBytes()); }
@Description("Compute HMAC with SHA1") @ScalarFunction @SqlType(StandardTypes.VARBINARY) public static Slice hmacSha1(@SqlType(StandardTypes.VARBINARY) Slice slice, @SqlType(StandardTypes.VARBINARY) Slice key) { return wrappedBuffer(Hashing.hmacSha1(key.getBytes()).hashBytes(slice.getBytes()).asBytes()); }
@Description("Compute HMAC with SHA256") @ScalarFunction @SqlType(StandardTypes.VARBINARY) public static Slice hmacSha256(@SqlType(StandardTypes.VARBINARY) Slice slice, @SqlType(StandardTypes.VARBINARY) Slice key) { return wrappedBuffer(Hashing.hmacSha256(key.getBytes()).hashBytes(slice.getBytes()).asBytes()); }
@Description("Compute HMAC with SHA512") @ScalarFunction @SqlType(StandardTypes.VARBINARY) public static Slice hmacSha512(@SqlType(StandardTypes.VARBINARY) Slice slice, @SqlType(StandardTypes.VARBINARY) Slice key) { return wrappedBuffer(Hashing.hmacSha512(key.getBytes()).hashBytes(slice.getBytes()).asBytes()); } }
@Test public void testSparseEstimation() { final Random random = new Random(0); HyperLogLogCollector collector = HyperLogLogCollector.makeLatestCollector(); for (int i = 0; i < 100; ++i) { collector.add(fn.hashLong(random.nextLong()).asBytes()); } Assert.assertEquals( collector.estimateCardinality(), HyperLogLogCollector.estimateByteBuffer(collector.toByteBuffer()), 0.0d ); }
private static void assertExpectedHashCode(ExpectedHashCode expectedHashCode, HashCode hash) { assertTrue(Arrays.equals(expectedHashCode.bytes, hash.asBytes())); byte[] bb = new byte[hash.bits() / 8]; hash.writeBytesTo(bb, 0, bb.length); assertTrue(Arrays.equals(expectedHashCode.bytes, bb)); assertEquals(expectedHashCode.asInt, hash.asInt()); if (expectedHashCode.asLong == null) { try { hash.asLong(); fail(); } catch (IllegalStateException expected) { } } else { assertEquals(expectedHashCode.asLong.longValue(), hash.asLong()); } assertEquals(expectedHashCode.toString, hash.toString()); assertSideEffectFree(hash); assertReadableBytes(hash); }