Search in sources :

Example 91 with Hasher

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();
}
Also used : Hasher(com.google.common.hash.Hasher)

Example 92 with Hasher

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!");
}
Also used : Hasher(com.google.common.hash.Hasher) Test(org.testng.annotations.Test)

Example 93 with Hasher

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();
}
Also used : Hasher(com.google.common.hash.Hasher) Fork(org.openjdk.jmh.annotations.Fork) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 94 with Hasher

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();
}
Also used : Hasher(com.google.common.hash.Hasher) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 95 with Hasher

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));
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) Hasher(com.google.common.hash.Hasher) Hcr(org.apache.servicecomb.demo.edge.authentication.encrypt.Hcr) EncryptContext(org.apache.servicecomb.demo.edge.service.encrypt.EncryptContext)

Aggregations

Hasher (com.google.common.hash.Hasher)106 HashCode (com.google.common.hash.HashCode)14 IOException (java.io.IOException)12 Test (org.junit.Test)10 HashFunction (com.google.common.hash.HashFunction)6 InputStream (java.io.InputStream)6 Map (java.util.Map)5 BuildTarget (com.facebook.buck.model.BuildTarget)4 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)4 SettableFakeClock (com.facebook.buck.timing.SettableFakeClock)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 Hcr (org.apache.servicecomb.demo.edge.authentication.encrypt.Hcr)4 EncryptContext (org.apache.servicecomb.demo.edge.service.encrypt.EncryptContext)4 Hcr (org.apache.servicecomb.it.authentication.encrypt.Hcr)4 EncryptContext (org.apache.servicecomb.it.edge.encrypt.EncryptContext)4 Buffer (io.vertx.core.buffer.Buffer)3 OutputStream (java.io.OutputStream)3 Path (java.nio.file.Path)3 StringUtils.byteToHexString (org.apache.flink.util.StringUtils.byteToHexString)3 EObject (org.eclipse.emf.ecore.EObject)3