Example usage for com.google.common.hash Hasher putString

List of usage examples for com.google.common.hash Hasher putString

Introduction

In this page you can find the example usage for com.google.common.hash Hasher putString.

Prototype

@Override
Hasher putString(CharSequence charSequence, Charset charset);

Source Link

Document

Equivalent to putBytes(charSequence.toString().getBytes(charset)) .

Usage

From source file:org.apache.marmotta.commons.http.ETagGenerator.java

public static String getETag(RepositoryConnection conn, URI resource) throws RepositoryException {
    if (resource == null)
        return "";

    Hasher hasher = buildHasher();
    hasher.putString(resource.stringValue(), Charset.defaultCharset());
    //FIXME: The order of the statements is not defined -> might result in different hash!
    RepositoryResult<Statement> outgoing = conn.getStatements(resource, null, null, true);
    try {/*ww  w .  j  a  v a 2 s  . c  o  m*/
        while (outgoing.hasNext()) {
            Statement statement = outgoing.next();
            hasher.putString(statement.getPredicate().stringValue(), Charset.defaultCharset());
            hasher.putString(statement.getObject().stringValue(), Charset.defaultCharset());
            //TODO: statement modification date?
        }
    } finally {
        outgoing.close();
    }
    RepositoryResult<Statement> incoming = conn.getStatements(null, null, resource, true);
    try {
        while (incoming.hasNext()) {
            Statement statement = incoming.next();
            hasher.putString(statement.getSubject().stringValue(), Charset.defaultCharset());
            hasher.putString(statement.getPredicate().stringValue(), Charset.defaultCharset());
            //TODO: statement modification date?
        }
    } finally {
        incoming.close();
    }
    return hasher.hash().toString();
}

From source file:edu.cmu.lti.oaqa.ecd.phase.ProcessingStepUtils.java

/**
 * Execution hash is computed from JCas currentExperimentId, trace and sequenceId
 * @return MD5 hash corresponding to the above mentioned elements
 *//*from w ww  .  j a v  a2s .c om*/
public static String getExecutionIdHash(String experimentId, Trace trace, String sequenceId) {
    HashFunction hf = Hashing.md5();
    Hasher hasher = hf.newHasher();
    hasher.putString(experimentId, StandardCharsets.UTF_8);
    hasher.putString(trace.getTrace(), StandardCharsets.UTF_8);
    hasher.putString(String.valueOf(sequenceId), StandardCharsets.UTF_8);
    HashCode hash = hasher.hash();
    final String traceHash = hash.toString();
    return traceHash;
}

From source file:edu.cmu.lti.oaqa.ecd.phase.ProcessingStepUtils.java

/**
 * Execution hash is computed from JCas currentExperimentId, trace and sequenceId
 * @return MD5 hash corresponding to the above mentioned elements
 *///from www.  j a va 2  s. c om
public static String getExecutionIdHash(JCas jcas) {
    String experimentId = ProcessingStepUtils.getCurrentExperimentId(jcas);
    Trace trace = ProcessingStepUtils.getTrace(jcas);
    String sequenceId = ProcessingStepUtils.getSequenceId(jcas);
    HashFunction hf = Hashing.md5();
    Hasher hasher = hf.newHasher();
    hasher.putString(experimentId, StandardCharsets.UTF_16LE);
    hasher.putString(trace.getTrace(), StandardCharsets.UTF_16LE);
    hasher.putString(String.valueOf(sequenceId), StandardCharsets.UTF_16LE);
    HashCode hash = hasher.hash();
    final String traceHash = hash.toString();
    return traceHash;
}

From source file:com.google.api.control.aggregator.Signing.java

/**
 * Updates {@code h} with the contents of {@code labels}.
 *
 * {@code labels} can be any Map&lt;String, String&gt;, but intended to be used for the labels of
 * one of the model protobufs./*  www.j  a  v a  2  s  . c  om*/
 *
 * @param h a {@link Hasher}
 * @param labels some labels
 * @return the {@code Hasher}, to allow fluent-style usage
 */
public static Hasher putLabels(Hasher h, Map<String, String> labels) {
    for (Map.Entry<String, String> labelsEntry : labels.entrySet()) {
        h.putChar('\0');
        h.putString(labelsEntry.getKey(), StandardCharsets.UTF_8);
        h.putChar('\0');
        h.putString(labelsEntry.getValue(), StandardCharsets.UTF_8);
    }
    return h;
}

From source file:org.apache.marmotta.commons.sesame.model.LiteralCommons.java

/**
 * Create a cache key for a literal with the given content, locale and type
 *
 * @param content  string content representing the literal (can be an MD5 sum for binary types)
 * @param language language of the literal (optional)
 * @param type     datatype URI of the literal (optional)
 * @return a 64bit hash key for the literal
 *//*from ww  w . j  a  v  a2 s  .c om*/
public static String createCacheKey(String content, String language, String type) {
    Hasher hasher = Hashing.goodFastHash(HASH_BITS).newHasher();
    hasher.putString(content, Charset.defaultCharset());
    if (type != null) {
        hasher.putString(type, Charset.defaultCharset());
    }
    if (language != null) {
        hasher.putString(language.toLowerCase(), Charset.defaultCharset());
    }
    return hasher.hash().toString();
}

From source file:com.facebook.buck.parser.cache.impl.Fingerprinter.java

/**
 * Calculates a strong fingerprint.//from  w w w .j  a  v  a  2s .com
 *
 * @param fs the {@link ProjectFilesystem} that we use to calculate the strong fingerprint.
 * @param includes the list of included build files for which we calculate the strong fingerprint.
 * @param fileHashCache the {@link FileHashCache} object to use to get the content hash of the
 *     loaded files.
 * @return a strong fingerprint - {@link HashCode} that represent a unique hash value.
 * @throws IOException can throw if there is a problem getting the content of the main BUCK build
 *     spec or an included file.
 */
public static HashCode getStrongFingerprint(ProjectFilesystem fs, ImmutableSortedSet<String> includes,
        FileHashCache fileHashCache) throws IOException {
    Hasher hasher = Hashing.sha256().newHasher();

    for (String value : includes) {
        Path value_path = fs.getPath(value);
        hasher.putString(fs.relativize(value_path).toString(), StandardCharsets.UTF_8);
        hasher.putBytes(fileHashCache.get(value_path).asBytes());
    }

    return hasher.hash();
}

From source file:com.dangdang.ddframe.job.internal.reg.RegistryCenterFactory.java

/**
 * ./*from   w  ww .  j  a v  a 2 s. c  o m*/
 *
 * @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();
    if (registryCenterMap.containsKey(hashCode)) {
        return registryCenterMap.get(hashCode);
    }
    ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(connectString, namespace);
    if (digest.isPresent()) {
        zkConfig.setDigest(digest.get());
    }
    CoordinatorRegistryCenter result = new ZookeeperRegistryCenter(zkConfig);
    result.init();
    registryCenterMap.putIfAbsent(hashCode, result);
    return result;
}

From source file:com.dangdang.ddframe.job.lite.internal.reg.RegistryCenterFactory.java

/**
 * ./*  ww w.j  av  a2 s  . c o  m*/
 *
 * @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();
    if (regCenterMap.containsKey(hashCode)) {
        return regCenterMap.get(hashCode);
    }
    ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(connectString, namespace);
    if (digest.isPresent()) {
        zkConfig.setDigest(digest.get());
    }
    CoordinatorRegistryCenter result = new ZookeeperRegistryCenter(zkConfig);
    result.init();
    regCenterMap.putIfAbsent(hashCode, result);
    return result;
}

From source file:com.dangdang.ddframe.job.lite.lifecycle.internal.reg.RegistryCenterFactory.java

/**
 * .//from   w  w  w .  ja va  2s.  c  o m
 *
 * @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;
}

From source file:co.indexia.antiquity.graph.ElementUtils.java

/**
 * <p>//w  w  w.j a  v a 2s.com
 * Calculate the private hash of an {@link Element}.
 * </p>
 * 
 * <p>
 * The private hash contains only the properties of the {@link Element},
 * without its associated elements.
 * </p>
 * 
 * <p>
 * The hash is calculated based on SHA1 algorithm
 * </p>
 * 
 * TODO Handle arrays values properly.
 * 
 * @param element The element to calculate the private hash for.
 * @param excludedKeys the keys to exclude when hash is calculated.
 * @return A string representation of the hash
 * @see HashCode#toString()
 */
public static String calculateElementPrivateHash(Element element, Set<String> excludedKeys) {
    Map<String, Object> props = ElementUtils.getPropertiesAsMap(element, excludedKeys);
    Joiner.MapJoiner propsJoiner = Joiner.on('&').withKeyValueSeparator("=");

    HashFunction hf = Hashing.sha1();
    Hasher h = hf.newHasher();

    //h.putString("[" + element.getId().toString() + "]");
    h.putString(propsJoiner.join(props), Charset.defaultCharset());

    return h.hash().toString();
}