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