com.google.common.hash.HashCode.asBytes()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(6.5k)|赞(0)|评价(0)|浏览(147)

本文整理了Java中com.google.common.hash.HashCode.asBytes()方法的一些代码示例,展示了HashCode.asBytes()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HashCode.asBytes()方法的具体详情如下:
包路径:com.google.common.hash.HashCode
类名称:HashCode
方法名:asBytes

HashCode.asBytes介绍

[英]Returns the value of this hash code as a byte array. The caller may modify the byte array; changes to it will not be reflected in this HashCode object or any other arrays returned by this method.
[中]以字节数组的形式返回此哈希代码的值。调用者可以修改字节数组;对它的更改不会反映在此HashCode对象或此方法返回的任何其他数组中。

代码示例

代码示例来源:origin: google/guava

/**
 * 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();
}

代码示例来源:origin: google/guava

private static void assertSideEffectFree(HashCode hash) {
 byte[] original = hash.asBytes();
 byte[] mutated = hash.asBytes();
 mutated[0]++;
 assertTrue(Arrays.equals(original, hash.asBytes()));
}

代码示例来源:origin: google/guava

@Override
 public byte[] hash(byte[] input, int seed) {
  return murmur3_32(seed).hashBytes(input).asBytes();
 }
};

代码示例来源:origin: prestodb/presto

@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());
}

代码示例来源:origin: google/guava

@Override
 public byte[] hash(byte[] input, int seed) {
  Hasher hasher = murmur3_32(seed).newHasher();
  Funnels.byteArrayFunnel().funnel(input, hasher);
  return hasher.hash().asBytes();
 }
};

代码示例来源:origin: google/guava

@Override
 public byte[] hash(byte[] input, int seed) {
  Hasher hasher = murmur3_128(seed).newHasher();
  Funnels.byteArrayFunnel().funnel(input, hasher);
  return hasher.hash().asBytes();
 }
};

代码示例来源:origin: apache/incubator-druid

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());
 }
}

代码示例来源:origin: prestodb/presto

@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());
}

代码示例来源:origin: prestodb/presto

@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());
}

代码示例来源:origin: prestodb/presto

@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());
}

代码示例来源:origin: google/guava

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));
}

代码示例来源:origin: google/guava

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));
 }
}

代码示例来源:origin: google/guava

public void testIntWriteBytesTo() {
 byte[] dest = new byte[4];
 HashCode.fromInt(42).writeBytesTo(dest, 0, 4);
 assertTrue(Arrays.equals(HashCode.fromInt(42).asBytes(), dest));
}

代码示例来源:origin: google/guava

public void testLongWriteBytesTo() {
 byte[] dest = new byte[8];
 HashCode.fromLong(42).writeBytesTo(dest, 0, 8);
 assertTrue(Arrays.equals(HashCode.fromLong(42).asBytes(), dest));
}

代码示例来源:origin: prestodb/presto

@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());
}

代码示例来源:origin: prestodb/presto

@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());
}

代码示例来源:origin: prestodb/presto

@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());
}

代码示例来源:origin: prestodb/presto

@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());
  }
}

代码示例来源:origin: apache/incubator-druid

@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
 );
}

代码示例来源:origin: google/guava

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);
}

相关文章