Example usage for com.google.common.hash Hashing sha1

List of usage examples for com.google.common.hash Hashing sha1

Introduction

In this page you can find the example usage for com.google.common.hash Hashing sha1.

Prototype

public static HashFunction sha1() 

Source Link

Document

Returns a hash function implementing the SHA-1 algorithm (160 hash bits) by delegating to the SHA-1 MessageDigest .

Usage

From source file:org.dcache.gridsite.ServletDelegation.java

private String generateDelegationId() throws DelegationException {
    String generator = getClientDn() + getFqanList();
    byte[] raw = generator.getBytes(Charsets.UTF_8);
    byte[] digest = Hashing.sha1().hashBytes(raw).asBytes();
    return BaseEncoding.base16().encode(digest, 0, 20);
}

From source file:org.gsafeproject.storage.StorageClient.java

private FormDataMultiPart buildQueryParams(String container, String path, File file) throws IOException {
    // TODO : default algo is sha1
    // TODO add algo
    String fingerprint = com.google.common.io.Files.hash(file, Hashing.sha1()).toString();

    FormDataMultiPart queryParams = new FormDataMultiPart();
    queryParams.bodyPart(new FormDataBodyPart("container", container, MediaType.TEXT_PLAIN_TYPE));
    queryParams.bodyPart(new FormDataBodyPart("path", path, MediaType.TEXT_PLAIN_TYPE));
    queryParams.bodyPart(new FormDataBodyPart("fingerprint", fingerprint, MediaType.TEXT_PLAIN_TYPE));
    queryParams.bodyPart(new FileDataBodyPart("on", file, MediaType.APPLICATION_OCTET_STREAM_TYPE));

    return queryParams;
}

From source file:com.macrossx.wechat.servlet.WechatCoreServlet.java

private boolean checkSignature(final HttpServletRequest req) {
    // System.out.println(wechatToken);
    // System.out.println(req.getParameter("timestamp"));
    // System.out.println(req.getParameter("nonce"));
    // System.out.println(req.getParameter("signature"));
    String[] params = { wechatToken, req.getParameter("timestamp"), req.getParameter("nonce") };
    Arrays.sort(params);/*from w  ww  .ja v  a 2 s  . com*/
    StringBuilder builder = new StringBuilder();
    for (String param : params) {
        builder.append(param);
    }
    return Hashing.sha1().hashString(builder.toString(), Charsets.UTF_8).toString()
            .equals(req.getParameter("signature"));

}

From source file:io.druid.indexing.common.task.MergeTaskBase.java

private static String computeProcessingID(final String dataSource, final List<DataSegment> segments) {
    final String segmentIDs = Joiner.on("_").join(
            Iterables.transform(Ordering.natural().sortedCopy(segments), new Function<DataSegment, String>() {
                @Override/*from   ww  w  . j a  v a 2  s  .c o  m*/
                public String apply(DataSegment x) {
                    return String.format("%s_%s_%s_%s", x.getInterval().getStart(), x.getInterval().getEnd(),
                            x.getVersion(), x.getShardSpec().getPartitionNum());
                }
            }));

    return String.format("%s_%s", dataSource, Hashing.sha1().hashString(segmentIDs, Charsets.UTF_8).toString());
}

From source file:com.facebook.buck.testutil.FakeProjectFilesystem.java

@Override
public String computeSha1(Path path) throws IOException {
    if (!exists(path)) {
        throw new FileNotFoundException(path.toString());
    }// ww w  .  j  a  v a 2  s.c o m
    return Hashing.sha1().hashBytes(getFileBytes(path)).toString();
}

From source file:com.facebook.buck.rules.modern.builders.ActionRunner.java

private HashCode hashFile(Path file) throws IOException {
    return MoreFiles.asByteSource(file).hash(Hashing.sha1());
}

From source file:org.glowroot.agent.model.QueryCollector.java

public @Nullable String getFullQueryText(String fullQueryTextSha1) {
    for (Map.Entry<String, Map<String, MutableQuery>> entry : queries.entrySet()) {
        for (String fullQueryText : entry.getValue().keySet()) {
            if (fullQueryText.length() <= Constants.AGGREGATE_QUERY_TEXT_TRUNCATE) {
                continue;
            }//  w  w  w.jav  a 2s.  c o m
            String sha1 = Hashing.sha1().hashString(fullQueryText, UTF_8).toString();
            if (fullQueryTextSha1.equals(sha1)) {
                return fullQueryText;
            }
        }
    }
    return null;
}

From source file:dodola.anole.lib.FileUtils.java

public static String sha1(File file) throws IOException {
    return Hashing.sha1().hashBytes(Files.toByteArray(file)).toString();
}

From source file:com.metamx.druid.indexing.common.task.MergeTaskBase.java

private static String computeProcessingID(final String dataSource, final List<DataSegment> segments) {
    final String segmentIDs = Joiner.on("_").join(
            Iterables.transform(Ordering.natural().sortedCopy(segments), new Function<DataSegment, String>() {
                @Override//from  www  . j a v  a  2  s .c  o m
                public String apply(DataSegment x) {
                    return String.format("%s_%s_%s_%s", x.getInterval().getStart(), x.getInterval().getEnd(),
                            x.getVersion(), x.getShardSpec().getPartitionNum());
                }
            }));

    return String.format("%s_%s", dataSource,
            Hashing.sha1().hashString(segmentIDs, Charsets.UTF_8).toString().toLowerCase());
}

From source file:com.facebook.buck.autodeps.AutodepsWriter.java

/**
 * Writes the file only if the contents are different to avoid creating noise for Watchman/buckd.
 * @param deps Keys must be sorted so the output is generated consistently.
 * @param includeSignature Whether to insert a signature for the contents of the file.
 * @param generatedFile Where to write the generated output.
 * @param mapper To aid in JSON serialization.
 * @return whether the file was written/*from   w  w  w.  jav  a 2  s  .c  o m*/
 */
private static boolean writeSignedFile(SortedMap<String, SortedMap<String, Iterable<String>>> deps,
        boolean includeSignature, Path generatedFile, ObjectMapper mapper) throws IOException {
    try (ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            HashingOutputStream hashingOutputStream = new HashingOutputStream(Hashing.sha1(), bytes)) {
        ObjectWriter jsonWriter = mapper.writer(PRETTY_PRINTER.get());
        jsonWriter.writeValue(includeSignature ? hashingOutputStream : bytes, deps);

        // Flush a trailing newline through the HashingOutputStream so it is included both the
        // output and the signature calculation.
        hashingOutputStream.write('\n');

        String serializedJson = bytes.toString(Charsets.UTF_8.name());
        String contentsToWrite;
        if (includeSignature) {
            HashCode hash = hashingOutputStream.hash();
            contentsToWrite = String.format(AUTODEPS_CONTENTS_FORMAT_STRING, hash, serializedJson);
        } else {
            contentsToWrite = serializedJson;
        }

        // Do not write file unless the contents have changed. Writing the file will cause the daemon
        // to indiscriminately invalidate any cached build rules for the associated build file.
        if (generatedFile.toFile().isFile()) {
            String existingContents = com.google.common.io.Files.toString(generatedFile.toFile(),
                    Charsets.UTF_8);
            if (contentsToWrite.equals(existingContents)) {
                return false;
            }
        }

        try (Writer writer = Files.newBufferedWriter(generatedFile, Charsets.UTF_8)) {
            writer.write(contentsToWrite);
        }
        return true;
    }
}