use of com.google.common.hash.Hasher in project guava by google.
the class ByteSource method 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();
}
use of com.google.common.hash.Hasher in project presto by prestodb.
the class TestTimeZoneKey method testZoneKeyData.
@Test
public void testZoneKeyData() {
Hasher hasher = Hashing.murmur3_128().newHasher();
SortedSet<TimeZoneKey> timeZoneKeysSortedByKey = ImmutableSortedSet.copyOf(new Comparator<TimeZoneKey>() {
@Override
public int compare(TimeZoneKey left, TimeZoneKey right) {
return Short.compare(left.getKey(), right.getKey());
}
}, TimeZoneKey.getTimeZoneKeys());
for (TimeZoneKey timeZoneKey : timeZoneKeysSortedByKey) {
hasher.putShort(timeZoneKey.getKey());
hasher.putString(timeZoneKey.getId(), StandardCharsets.UTF_8);
}
// Zone file should not (normally) be changed, so let's make this more difficult
assertEquals(hasher.hash().asLong(), 6334606028834602490L, "zone-index.properties file contents changed!");
}
use of com.google.common.hash.Hasher in project cassandra by apache.
the class ChecksumBench method benchHasherCrc32c.
@Benchmark
@Fork(value = 1, jvmArgsAppend = { "-Xmx512M", "-Djmh.executor=CUSTOM", "-Djmh.executor.class=org.apache.cassandra.test.microbench.FastThreadExecutor" })
public byte[] benchHasherCrc32c() {
Hasher crc32cHasher = Hashing.crc32c().newHasher();
crc32cHasher.putBytes(array);
return crc32cHasher.hash().asBytes();
}
use of com.google.common.hash.Hasher in project cassandra by apache.
the class HashingBench method benchHasherMD5.
@Benchmark
public byte[] benchHasherMD5() {
Hasher md5Hasher = Hashing.md5().newHasher();
md5Hasher.putBytes(array);
return md5Hasher.hash().asBytes();
}
use of com.google.common.hash.Hasher in project java-chassis by ServiceComb.
the class EdgeSignatureResponseFilter method beforeSendResponse.
@Override
public void beforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx) {
if (invocation == null) {
return;
}
EncryptContext encryptContext = (EncryptContext) invocation.getHandlerContext().get(EdgeConst.ENCRYPT_CONTEXT);
if (encryptContext == null) {
return;
}
Hcr hcr = encryptContext.getHcr();
// bad practice: it's better to set signature in response header
Buffer bodyBuffer = responseEx.getBodyBuffer();
String body = bodyBuffer.toString();
if (body.endsWith("}")) {
Hasher hasher = Hashing.sha256().newHasher();
hasher.putString(hcr.getSignatureKey(), StandardCharsets.UTF_8);
hasher.putString(body, StandardCharsets.UTF_8);
String signature = hasher.hash().toString();
LOGGER.info("beforeSendResponse signature: {}", signature);
body = body.substring(0, body.length() - 1) + ",\"signature\":\"" + signature + "\"}";
responseEx.setBodyBuffer(Buffer.buffer(body));
}
}
Aggregations