List of usage examples for com.google.common.hash Hasher putString
@Override Hasher putString(CharSequence charSequence, Charset charset);
From source file:com.heliosapm.jmx.alarm.AlarmWindow.java
/** * Hashes the metric//from ww w. j a v a2s. co m * @param metricName The metric name * @param tags The metric tags * @return the metric hash code */ public static long hashCode(final String metricName, final Map<String, String> tags) { final Hasher hasher = hashFunction.newHasher(); hasher.putString(metricName, CHARSET); for (Map.Entry<String, String> entry : tags.entrySet()) { hasher.putString(entry.getKey(), CHARSET).putString(entry.getValue(), CHARSET); } return hasher.hash().asLong(); }
From source file:com.google.api.control.aggregator.ReportRequestAggregator.java
/** * Obtains the {@hashCode} for the contents of {@code value}. * * @param value a {@code Operation} to be signed * @return the {@code HashCode} corresponding to {@code value} *//*from w w w . ja va 2 s. c o m*/ private static HashCode sign(Operation value) { Hasher h = Hashing.md5().newHasher(); h.putString(value.getConsumerId(), StandardCharsets.UTF_8); h.putChar('\0'); h.putString(value.getOperationName(), StandardCharsets.UTF_8); h.putChar('\0'); return Signing.putLabels(h, value.getLabels()).hash(); }
From source file:org.ambraproject.wombat.util.CacheKey.java
/** * Create a hash from the sequence of identifiers and return it, with a prefix appended, for use as a cache key. * <p>//from ww w.j ava2 s . co m * This is useful because the strings may contain characters that are invalid for a cache key. Also, when they are * concatenated, they may be too long (by environmental default, >250 chars) to be used as a key. * * @param identifiers a sequence of identifiers that uniquely identify a cache value * @return a digest of bounded length */ public static String createKeyHash(List<String> identifiers) { Hasher hasher = HASH_ALGORITHM.newHasher(); for (Iterator<String> iterator = identifiers.iterator(); iterator.hasNext();) { hasher.putString(iterator.next(), HASH_CHARSET); if (iterator.hasNext()) { hasher.putChar(HASH_SEPARATOR); } } return HASH_BASE.encode(hasher.hash().asBytes()); }
From source file:com.attribyte.relay.wp.PostMeta.java
/** * Creates a unique fingerprint for a post. * @param post The post.//from ww w. j av a2s . c o m * @param hasher The hasher. */ public static final void fingerprint(final Post post, final Hasher hasher) { if (post == null) { return; } hasher.putString(Strings.nullToEmpty(post.slug), Charsets.UTF_8); hasher.putString(Strings.nullToEmpty(post.title), Charsets.UTF_8); hasher.putString(Strings.nullToEmpty(post.excerpt), Charsets.UTF_8); hasher.putString(Strings.nullToEmpty(post.content), Charsets.UTF_8); hasher.putLong(post.authorId); hasher.putLong(post.publishTimestamp); hasher.putString(post.status != null ? post.status.toString() : "", Charsets.UTF_8); hasher.putString(Strings.nullToEmpty(post.guid), Charsets.UTF_8); post.metadata.stream().sorted(Comparator.comparing(o -> o.key)).forEach(meta -> { hasher.putString(meta.key, Charsets.UTF_8).putString(Strings.nullToEmpty(meta.value), Charsets.UTF_8); }); hasher.putString(post.type != null ? post.type.toString() : "", Charsets.UTF_8); hasher.putString(Strings.nullToEmpty(post.mimeType), Charsets.UTF_8); post.tags().stream().sorted(Comparator.comparing(o -> o.term.name)).forEach(term -> { hasher.putString(term.term.name, Charsets.UTF_8); }); post.categories().stream().sorted(Comparator.comparing(o -> o.term.name)).forEach(term -> { hasher.putString(term.term.name, Charsets.UTF_8); }); post.children.forEach(childPost -> fingerprint(childPost, hasher)); }
From source file:com.google.devtools.kythe.analyzers.base.EntrySet.java
protected static String buildSignature(ImmutableList<String> salts, ImmutableSortedMap<String, byte[]> properties) { Hasher signature = SIGNATURE_HASH_FUNCTION.newHasher(); for (String salt : salts) { signature.putString(salt, PROPERTY_VALUE_CHARSET); }// www. j av a 2 s. c o m for (Map.Entry<String, byte[]> property : properties.entrySet()) { signature.putString(property.getKey(), PROPERTY_VALUE_CHARSET).putBytes(property.getValue()); } return signature.hash().toString(); }
From source file:org.gradle.internal.classloader.DefaultHashingClassLoaderFactory.java
private static void addToHash(Hasher hasher, Set<String> items) { int count = items.size(); hasher.putInt(count);//w w w . j a va 2s. c o m if (count == 0) { return; } String[] sortedItems = items.toArray(new String[count]); Arrays.sort(sortedItems); for (String item : sortedItems) { hasher.putInt(0); hasher.putString(item, Charsets.UTF_8); } }
From source file:com.facebook.buck.codegen.SourceSigner.java
/** * Given the contents of a source file containing 0 or more existing signatures * or signature placeholders, calculates an updated signature and updates * the signatures in the source file.//w w w . ja v a 2s. co m * * If the source file contains at least one signature, returns an Optional * containing the updated source file. Otherwise, returns Optional.absent(). */ public static final Optional<String> sign(String source) { Matcher matcher = SIGNATURE_OR_PLACEHOLDER_PATTERN.matcher(source); Hasher md5Hasher = Hashing.md5().newHasher(); int pos = 0; while (matcher.find()) { if (matcher.groupCount() == 1) { // Update the digest up to the matched md5, but then hash the // placeholder token instead of the md5. md5Hasher.putString(source.substring(pos, matcher.start()), Charsets.UTF_8); md5Hasher.putString(PLACEHOLDER, Charsets.UTF_8); } else { md5Hasher.putString(source.substring(pos, matcher.end()), Charsets.UTF_8); } pos = matcher.end(); } if (pos > 0) { md5Hasher.putString(source.substring(pos), Charsets.UTF_8); matcher.reset(); String newSignature = formatSignature(md5Hasher.hash().toString()); return Optional.of(matcher.replaceAll(newSignature)); } else { return Optional.absent(); } }
From source file:com.facebook.buck.versions.ParallelVersionedTargetGraphBuilder.java
/** @return a flavor to which summarizes the given version selections. */ static Flavor getVersionedFlavor(SortedMap<BuildTarget, Version> versions) { Preconditions.checkArgument(!versions.isEmpty()); Hasher hasher = Hashing.md5().newHasher(); for (Map.Entry<BuildTarget, Version> ent : versions.entrySet()) { hasher.putString(ent.getKey().toString(), Charsets.UTF_8); hasher.putString(ent.getValue().getName(), Charsets.UTF_8); }/*from w w w.j a v a 2 s . c o m*/ return InternalFlavor.of("v" + hasher.hash().toString().substring(0, 7)); }
From source file:com.facebook.buck.features.rust.RustCompileUtils.java
/** * Approximate what Cargo does - it computes a hash based on the crate version and its * dependencies. Buck will deal with the dependencies and we don't need to worry about the * version, but we do need to make sure that two crates with the same name in the build are * distinct - so compute the hash from the full target path. * * @param target Which target we're computing the hash for * @return Truncated MD5 hash of the target path *//*from w w w .j a va 2 s . c om*/ static String hashForTarget(BuildTarget target) { String name = target.getUnflavoredBuildTarget().getFullyQualifiedName(); Hasher hasher = Hashing.md5().newHasher(); HashCode hash = hasher.putString(name, StandardCharsets.UTF_8).hash(); return hash.toString().substring(0, 16); }
From source file:com.facebook.buck.versions.VersionedTargetGraphBuilder.java
/** * @return a flavor to which summarizes the given version selections. *//*from ww w . j a v a 2s.c o m*/ static Flavor getVersionedFlavor(SortedMap<BuildTarget, Version> versions) { Preconditions.checkArgument(!versions.isEmpty()); Hasher hasher = Hashing.md5().newHasher(); for (Map.Entry<BuildTarget, Version> ent : versions.entrySet()) { hasher.putString(ent.getKey().toString(), Charsets.UTF_8); hasher.putString(ent.getValue().getName(), Charsets.UTF_8); } return ImmutableFlavor.of("v" + hasher.hash().toString().substring(0, 7)); }