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.openwhisk.example.maven.App.java

public static JsonObject main(JsonObject args) {
    JsonObject response = new JsonObject();
    if (args.has("text")) {
        String text = args.getAsJsonPrimitive("text").getAsString();
        try {/*  w w w . java2  s . c  o m*/
            Hasher hasher = Hashing.md5().newHasher();
            hasher.putString(text.toString(), Charset.forName("UTF-8"));
            response.addProperty("text", text);
            response.addProperty("md5", hasher.hash().toString());
        } catch (Exception e) {
            response.addProperty("Error", e.getMessage());
        }
    }
    return response;
}

From source file:io.github.maxymania.powercache.hash.Util.java

public static byte[] hash(HashFunction hf, String catname, Object[] obj) {
    Hasher digest = hf.newHasher();
    digest.putString(catname, UTF);
    digest.putObject(obj, funnel);/*from   w ww. j a  va2  s .com*/
    return digest.hash().asBytes();
}

From source file:org.liquigraph.model.Checksums.java

public static String checksum(Collection<String> queries) {
    Hasher hasher = Hashing.sha1().newHasher();
    for (String query : queries) {
        hasher = hasher.putString(query, Charsets.UTF_8);
    }/*  w  w w . j  av  a2s  .c  om*/
    return hasher.hash().toString();
}

From source file:io.servicecomb.demo.signature.SignatureUtils.java

public static String genSignature(HttpServletRequestEx requestEx) {
    Hasher hasher = Hashing.sha256().newHasher();
    hasher.putString(requestEx.getRequestURI(), StandardCharsets.UTF_8);
    for (String paramName : paramNames) {
        String paramValue = requestEx.getHeader(paramName);
        if (paramValue != null) {
            hasher.putString(paramName, StandardCharsets.UTF_8);
            hasher.putString(paramValue, StandardCharsets.UTF_8);
            System.out.printf("%s %s\n", paramName, paramValue);
        }/*from   w  w  w  .ja  va 2 s .c o m*/
    }

    byte[] bytes = requestEx.getBodyBytes();
    if (bytes != null) {
        hasher.putBytes(bytes, 0, requestEx.getBodyBytesLength());
    }

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

From source file:com.google.devtools.build.lib.worker.WorkerFilesHash.java

public static HashCode getWorkerFilesHash(Iterable<? extends ActionInput> toolFiles,
        ActionExecutionContext actionExecutionContext) throws IOException {
    Hasher hasher = Hashing.sha256().newHasher();
    for (ActionInput tool : toolFiles) {
        hasher.putString(tool.getExecPathString(), Charset.defaultCharset());
        hasher.putBytes(actionExecutionContext.getActionInputFileCache().getDigest(tool));
    }//  w  w  w  .ja  v  a  2 s.c  o  m
    return hasher.hash();
}

From source file:org.apache.servicecomb.demo.signature.SignatureUtils.java

public static String genSignature(HttpServletRequestEx requestEx) {
    Hasher hasher = Hashing.sha256().newHasher();
    hasher.putString(requestEx.getRequestURI(), StandardCharsets.UTF_8);
    for (String paramName : paramNames) {
        String paramValue = requestEx.getHeader(paramName);
        if (paramValue != null) {
            hasher.putString(paramName, StandardCharsets.UTF_8);
            hasher.putString(paramValue, StandardCharsets.UTF_8);
            System.out.printf("%s %s\n", paramName, paramValue);
        }// w  w  w .j  av a 2 s  .co m
    }

    if (!StringUtils.startsWithIgnoreCase(requestEx.getContentType(), MediaType.APPLICATION_FORM_URLENCODED)) {
        byte[] bytes = requestEx.getBodyBytes();
        if (bytes != null) {
            hasher.putBytes(bytes, 0, requestEx.getBodyBytesLength());
        }
    }

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

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

/**
 * Gets the weak fingerprint for this configuration.
 *
 * @param buildFile the path to the BUCK file build spec of interest.
 * @param config the {@link Config} object to calculate the weak fingerprint for
 * @return a weak fingerprint - {@link com.google.common.hash.HashCode} that represent a unique
 *     hash value./*from  ww w  .  j  ava  2s  . co  m*/
 */
public static HashCode getWeakFingerprint(Path buildFile, Config config) {
    Hasher hasher = Hashing.sha256().newHasher();
    return hasher.putString(buildFile.toString(), StandardCharsets.UTF_8)
            .putBytes(config.getOrderIndependentHashCode().asBytes())
            .putString(Platform.detect().name(), StandardCharsets.UTF_8)
            .putString(Architecture.detect().name(), StandardCharsets.UTF_8).hash();
}

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

public static String getWeakETag(RepositoryConnection conn, Resource 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> statements = conn.getStatements(resource, null, null, true);
    try {//w w w .  j  a v a2  s. c  om
        while (statements.hasNext()) {
            Statement statement = statements.next();
            hasher.putString(statement.getPredicate().stringValue(), Charset.defaultCharset());
            hasher.putString(statement.getObject().stringValue(), Charset.defaultCharset());
            //TODO: statement modification date?
        }
    } finally {
        statements.close();
    }
    return hasher.hash().toString();
}

From source file:com.ibm.vicos.common.util.Utils.java

public static String hashMurmur3_32(String input) {
    Hasher hasher = Hashing.murmur3_32().newHasher();
    hasher.putString(input, Charsets.UTF_8);
    return hasher.hash().toString();
}

From source file:fr.inria.eventcloud.api.Skolemizator.java

private static Node createSkolemUri(Node subjectOrObject, Map<Node, Node> assignedSkolems) {
    UUID randomId = UUID.randomUUID();

    Hasher hasher = Hashing.murmur3_128().newHasher();
    hasher.putString(subjectOrObject.toString(), Charsets.UTF_8);
    hasher.putLong(randomId.getMostSignificantBits());
    hasher.putLong(randomId.getLeastSignificantBits());

    Node skolem = NodeFactory// w  w  w  .  j av a 2 s  .  c  o  m
            .createURI(SKOLEM_URI_SUFFIX + SKOLEM_URI_PATH_COMPONENT + hasher.hash().toString());

    assignedSkolems.put(subjectOrObject, skolem);

    return skolem;
}