Search in sources :

Example 71 with Hasher

use of com.google.common.hash.Hasher in project buck by facebook.

the class HttpArtifactCacheBinaryProtocol method createMetadataHeader.

@VisibleForTesting
static byte[] createMetadataHeader(ImmutableSet<RuleKey> ruleKeys, ImmutableMap<String, String> metadata, ByteSource data) throws IOException {
    ByteArrayOutputStream rawOut = new ByteArrayOutputStream();
    Hasher hasher = HASH_FUNCTION.newHasher();
    try (DataOutputStream out = new DataOutputStream(new HasherOutputStream(hasher, rawOut))) {
        // Write the rule keys to the raw metadata, including them in the end-to-end checksum.
        out.writeInt(ruleKeys.size());
        for (RuleKey ruleKey : ruleKeys) {
            out.writeUTF(ruleKey.toString());
        }
        // Write out the metadata map to the raw metadata, including it in the end-to-end checksum.
        out.writeInt(metadata.size());
        for (Map.Entry<String, String> ent : metadata.entrySet()) {
            out.writeUTF(ent.getKey());
            byte[] val = ent.getValue().getBytes(Charsets.UTF_8);
            out.writeInt(val.length);
            out.write(val);
            if (out.size() > MAX_METADATA_HEADER_SIZE) {
                throw new IOException("Metadata header too big.");
            }
        }
    }
    // Add the file data contents to the end-to-end checksum.
    data.copyTo(new HasherOutputStream(hasher, ByteStreams.nullOutputStream()));
    // Finish the checksum, adding it to the raw metadata
    rawOut.write(hasher.hash().asBytes());
    // Finally, base64 encode the raw bytes to make usable in a HTTP header.
    byte[] bytes = rawOut.toByteArray();
    if (bytes.length > MAX_METADATA_HEADER_SIZE) {
        throw new IOException("Metadata header too big.");
    }
    return bytes;
}
Also used : Hasher(com.google.common.hash.Hasher) HasherOutputStream(com.facebook.buck.util.hash.HasherOutputStream) RuleKey(com.facebook.buck.rules.RuleKey) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 72 with Hasher

use of com.google.common.hash.Hasher in project buck by facebook.

the class StringHashingTest method separateHashDifferentFromSameStringsConcatenated.

@Test
public void separateHashDifferentFromSameStringsConcatenated() {
    Hasher separateHasher = Hashing.sha1().newHasher();
    StringHashing.hashStringAndLength(separateHasher, "foo");
    StringHashing.hashStringAndLength(separateHasher, "bar");
    Hasher concatenatedHasher = Hashing.sha1().newHasher();
    StringHashing.hashStringAndLength(separateHasher, "foobar");
    assertThat(separateHasher.hash(), not(equalTo(concatenatedHasher.hash())));
}
Also used : Hasher(com.google.common.hash.Hasher) Test(org.junit.Test)

Example 73 with Hasher

use of com.google.common.hash.Hasher in project buck by facebook.

the class StringHashingTest method emptyStringHasExpectedHash.

@Test
public void emptyStringHasExpectedHash() {
    Hasher hasher = Hashing.sha1().newHasher();
    StringHashing.hashStringAndLength(hasher, "");
    assertThat(hasher.hash(), equalTo(HashCode.fromString("9069ca78e7450a285173431b3e52c5c25299e473")));
}
Also used : Hasher(com.google.common.hash.Hasher) Test(org.junit.Test)

Example 74 with Hasher

use of com.google.common.hash.Hasher in project druid by druid-io.

the class CardinalityAggregator method hashRow.

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());
}
Also used : Hasher(com.google.common.hash.Hasher) CardinalityAggregatorColumnSelectorStrategy(io.druid.query.aggregation.cardinality.types.CardinalityAggregatorColumnSelectorStrategy)

Example 75 with Hasher

use of com.google.common.hash.Hasher in project elastic-job by dangdangdotcom.

the class RegistryCenterFactory method createCoordinatorRegistryCenter.

/**
     * 创建注册中心.
     *
     * @param connectString 注册中心连接字符串
     * @param namespace 注册中心命名空间
     * @param digest 注册中心凭证
     * @return 注册中心对象
     */
public static CoordinatorRegistryCenter createCoordinatorRegistryCenter(final String connectString, final String namespace, final Optional<String> digest) {
    Hasher hasher = Hashing.md5().newHasher().putString(connectString, Charsets.UTF_8).putString(namespace, Charsets.UTF_8);
    if (digest.isPresent()) {
        hasher.putString(digest.get(), Charsets.UTF_8);
    }
    HashCode hashCode = hasher.hash();
    CoordinatorRegistryCenter result = REG_CENTER_REGISTRY.get(hashCode);
    if (null != result) {
        return result;
    }
    ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(connectString, namespace);
    if (digest.isPresent()) {
        zkConfig.setDigest(digest.get());
    }
    result = new ZookeeperRegistryCenter(zkConfig);
    result.init();
    REG_CENTER_REGISTRY.put(hashCode, result);
    return result;
}
Also used : ZookeeperRegistryCenter(com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter) Hasher(com.google.common.hash.Hasher) HashCode(com.google.common.hash.HashCode) ZookeeperConfiguration(com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration) CoordinatorRegistryCenter(com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter)

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