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:com.b2international.index.es.EsDocumentWriter.java

@Override
public void commit() throws IOException {
    if (indexOperations.isEmpty() && deleteOperations.isEmpty() && updateOperations.isEmpty()) {
        return;// w  ww  . j  a  v a2  s.  c  om
    }

    final Set<DocumentMapping> mappingsToRefresh = Collections.synchronizedSet(newHashSet());
    final EsClient client = admin.client();
    // apply bulk updates first
    final ListeningExecutorService executor;
    if (updateOperations.size() > 1) {
        executor = MoreExecutors
                .listeningDecorator(Executors.newFixedThreadPool(Math.min(4, updateOperations.size())));
    } else {
        executor = MoreExecutors.newDirectExecutorService();
    }
    final List<ListenableFuture<?>> updateFutures = newArrayList();
    for (BulkUpdate<?> update : updateOperations) {
        updateFutures.add(executor.submit(() -> bulkUpdate(client, update, mappingsToRefresh)));
    }
    try {
        executor.shutdown();
        Futures.allAsList(updateFutures).get();
        executor.awaitTermination(10, TimeUnit.SECONDS);
    } catch (InterruptedException | ExecutionException e) {
        throw new IndexException("Couldn't execute bulk updates", e);
    }

    // then bulk indexes/deletes
    if (!indexOperations.isEmpty() || !deleteOperations.isEmpty()) {
        final BulkProcessor processor = client.bulk(new BulkProcessor.Listener() {
            @Override
            public void beforeBulk(long executionId, BulkRequest request) {
                admin.log().debug("Sending bulk request {}", request.numberOfActions());
            }

            @Override
            public void afterBulk(long executionId, BulkRequest request, Throwable failure) {
                admin.log().error("Failed bulk request", failure);
            }

            @Override
            public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {
                admin.log().debug("Successfully processed bulk request ({}) in {}.", request.numberOfActions(),
                        response.getTook());
                if (response.hasFailures()) {
                    for (BulkItemResponse itemResponse : response.getItems()) {
                        checkState(!itemResponse.isFailed(), "Failed to commit bulk request in index '%s', %s",
                                admin.name(), itemResponse.getFailureMessage());
                    }
                }
            }
        }).setConcurrentRequests(getConcurrencyLevel()).setBulkActions(10_000)
                .setBulkSize(new ByteSizeValue(10L, ByteSizeUnit.MB)).build();

        for (Class<?> type : ImmutableSet.copyOf(indexOperations.rowKeySet())) {
            final Map<String, Object> indexOperationsForType = indexOperations.row(type);

            final DocumentMapping mapping = admin.mappings().getMapping(type);
            final String typeString = mapping.typeAsString();
            final String typeIndex = admin.getTypeIndex(mapping);

            mappingsToRefresh.add(mapping);

            for (Entry<String, Object> entry : Iterables.consumingIterable(indexOperationsForType.entrySet())) {
                final String id = entry.getKey();
                if (!deleteOperations.containsValue(id)) {
                    final Object obj = entry.getValue();
                    final Set<String> hashedFields = mapping.getHashedFields();
                    final byte[] _source;

                    if (!hashedFields.isEmpty()) {
                        final ObjectNode objNode = mapper.valueToTree(obj);
                        final ObjectNode hashedNode = mapper.createObjectNode();

                        // Preserve property order, share references with objNode
                        for (String hashedField : hashedFields) {
                            JsonNode value = objNode.get(hashedField);
                            if (value != null && !value.isNull()) {
                                hashedNode.set(hashedField, value);
                            }
                        }

                        final byte[] hashedBytes = mapper.writeValueAsBytes(hashedNode);
                        final HashCode hashCode = Hashing.sha1().hashBytes(hashedBytes);

                        // Inject the result as an extra field into the to-be-indexed JSON content
                        objNode.put(DocumentMapping._HASH, hashCode.toString());
                        _source = mapper.writeValueAsBytes(objNode);

                    } else {
                        _source = mapper.writeValueAsBytes(obj);
                    }

                    processor.add(new IndexRequest(typeIndex, typeString, id).opType(OpType.INDEX)
                            .source(_source, XContentType.JSON));
                }
            }

            for (String id : deleteOperations.removeAll(type)) {
                processor.add(new DeleteRequest(typeIndex, typeString, id));
            }

            // Flush processor between index boundaries
            processor.flush();
        }

        // Remaining delete operations can be executed on their own
        for (Class<?> type : ImmutableSet.copyOf(deleteOperations.keySet())) {
            final DocumentMapping mapping = admin.mappings().getMapping(type);
            final String typeString = mapping.typeAsString();
            final String typeIndex = admin.getTypeIndex(mapping);

            mappingsToRefresh.add(mapping);

            for (String id : deleteOperations.removeAll(type)) {
                processor.add(new DeleteRequest(typeIndex, typeString, id));
            }

            // Flush processor between index boundaries
            processor.flush();
        }

        try {
            processor.awaitClose(5, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            throw new IndexException("Interrupted bulk processing part of the commit", e);
        }
    }

    // refresh the index if there were only updates
    admin.refresh(mappingsToRefresh);
}

From source file:com.android.builder.internal.compiler.PreProcessCache.java

@Nullable
private static HashCode getHash(@NonNull File file) {
    try {//from www . j  ava2  s . com
        return Files.hash(file, Hashing.sha1());
    } catch (IOException ignored) {
    }

    return null;
}

From source file:io.macgyver.plugin.cmdb.AppInstanceManager.java

public static Optional<String> computeId(String host, String app, String index) {
    if (Strings.isNullOrEmpty(host)) {
        return Optional.empty();
    }/*  ww  w . j  a  v  a2 s .co m*/
    if (Strings.isNullOrEmpty(app)) {
        return Optional.empty();
    }
    if (Strings.isNullOrEmpty(index)) {
        index = "default";
    }
    return Optional
            .of(Hashing.sha1().hashString(host + "-" + app + "-" + index, Charset.forName("UTF8")).toString());
}

From source file:io.macgyver.plugin.ci.jenkins.JenkinsClientImpl.java

public String getServerId() {
    return Hashing.sha1().hashString(getServerUrl(), Charsets.UTF_8).toString();
}

From source file:org.macgyver.mercator.ucs.UCSScanner.java

protected void recordFabricExtender(Element element) {

    ObjectNode n = toJson(element);//w  w  w.j a  v  a2  s.c om

    String serial = n.get("serial").asText();
    String mercatorId = Hashing.sha1().hashString(serial, Charsets.UTF_8).toString();
    n.put("mercatorId", mercatorId);

    String cypher = "merge (c:UCSFabricExtender {mercatorId:{mercatorId}}) set c+={props}, c.updateTs=timestamp()";

    getProjector().getNeoRxClient().execCypher(cypher, "mercatorId", mercatorId, "props", n);

    cypher = "match (m:UCSManager {mercatorId:{managerId}}), (c:UCSFabricExtender {mercatorId:{fexId}}) merge (m)-[r:MANAGES]->(c)";

    getProjector().getNeoRxClient().execCypher(cypher, "managerId", getUCSClient().getUCSManagerId(), "fexId",
            mercatorId);

}

From source file:org.macgyver.mercator.ucs.UCSClient.java

public String getUCSManagerId() {

    return Hashing.sha1().hashString(target.getUrl().toLowerCase(), Charsets.UTF_8).toString();
}

From source file:org.jclouds.ssh.SshKeys.java

/**
 * Create a SHA-1 digest of the DER encoded private key.
 * //from   w w  w  .j a  va 2s  .  c  o  m
 * @param publicExponent
 * @param modulus
 * 
 * @return hex sha1HexColonDelimited ex. {@code 2b:a9:62:95:5b:8b:1d:61:e0:92:f7:03:10:e9:db:d9}
 */
public static String sha1(RSAPrivateCrtKeySpec privateKey) {
    try {
        byte[] encodedKey = KeyFactory.getInstance("RSA").generatePrivate(privateKey).getEncoded();
        return hexColonDelimited(Hashing.sha1().hashBytes(encodedKey));
    } catch (InvalidKeySpecException e) {
        throw propagate(e);
    } catch (NoSuchAlgorithmException e) {
        throw propagate(e);
    }
}

From source file:com.google.template.soy.base.internal.BaseUtils.java

/**
 * Computes the SHA-1 hash value of the input string's UTF-8 representation and returns the first
 * numBits bits of the result as a hex value in string form.
 *
 * @param strToHash The string to compute SHA-1 of.
 * @param numBits The number of bits worth to return. Must be a positive number at most 160 and
 *     divisible by 8 (since we process the result 8 bits at a time).
 * @return The partial SHA-1 hash value as a hex string.
 *//*  w ww.ja  v a  2 s  .  c o  m*/
public static String computePartialSha1AsHexString(String strToHash, int numBits) {

    Preconditions.checkArgument(numBits > 0 && numBits <= 160 && numBits % 8 == 0);
    int numBytes = numBits / 8;
    return Hashing.sha1().hashString(strToHash, UTF_8).toString().substring(0, numBytes * 2);
}

From source file:com.facebook.buck.jvm.java.intellij.IjProjectWriter.java

@VisibleForTesting
protected void writeToFile(ST contents, Path path) throws IOException {
    StringWriter stringWriter = new StringWriter();
    AutoIndentWriter noIndentWriter = new AutoIndentWriter(stringWriter);
    contents.write(noIndentWriter);/*from  w  w w. j ava2 s. c  o  m*/
    byte[] renderedContentsBytes = noIndentWriter.toString().getBytes(StandardCharsets.UTF_8);
    if (projectFilesystem.exists(path)) {
        Sha1HashCode fileSha1 = projectFilesystem.computeSha1(path);
        Sha1HashCode contentsSha1 = Sha1HashCode.fromHashCode(Hashing.sha1().hashBytes(renderedContentsBytes));
        if (fileSha1.equals(contentsSha1)) {
            return;
        }
    }

    boolean danglingTempFile = false;
    Path tempFile = projectFilesystem.createTempFile(IDEA_CONFIG_DIR_PREFIX, path.getFileName().toString(),
            ".tmp");
    try {
        danglingTempFile = true;
        try (OutputStream outputStream = projectFilesystem.newFileOutputStream(tempFile)) {
            outputStream.write(contents.render().getBytes());
        }
        projectFilesystem.createParentDirs(path);
        projectFilesystem.move(tempFile, path, StandardCopyOption.REPLACE_EXISTING);
        danglingTempFile = false;
    } finally {
        if (danglingTempFile) {
            projectFilesystem.deleteFileAtPath(tempFile);
        }
    }
}

From source file:com.facebook.buck.android.PreDexedFilesSorter.java

/**
 * @see com.facebook.buck.dalvik.CanaryFactory#create(int)
 *///from w  w  w  .j  a va2s . c  o  m
private DexWithClasses createCanary(final int index, ImmutableList.Builder<Step> steps) {
    final FileLike fileLike = CanaryFactory.create(index);
    final String canaryDirName = "canary_" + String.valueOf(index);
    final Path scratchDirectoryForCanaryClass = scratchDirectory.resolve(canaryDirName);

    // Strip the .class suffix to get the class name for the DexWithClasses object.
    final String relativePathToClassFile = fileLike.getRelativePath();
    Preconditions.checkState(relativePathToClassFile.endsWith(".class"));
    final String className = relativePathToClassFile.replaceFirst("\\.class$", "");

    // Write out the .class file.
    steps.add(new AbstractExecutionStep("write_canary_class") {
        @Override
        public int execute(ExecutionContext context) {
            Path classFile = scratchDirectoryForCanaryClass.resolve(relativePathToClassFile);
            ProjectFilesystem projectFilesystem = context.getProjectFilesystem();
            try (InputStream inputStream = fileLike.getInput()) {
                projectFilesystem.createParentDirs(classFile);
                projectFilesystem.copyToPath(inputStream, classFile);
            } catch (IOException e) {
                context.logError(e, "Error writing canary class file: %s.", classFile.toString());
                return 1;
            }
            return 0;
        }
    });

    return new DexWithClasses() {

        @Override
        public int getSizeEstimate() {
            // Because we do not know the units being used for DEX size estimation and the canary should
            // be very small, assume the size is zero.
            return 0;
        }

        @Override
        public Path getPathToDexFile() {
            return scratchDirectoryForCanaryClass;
        }

        @Override
        public ImmutableSet<String> getClassNames() {
            return ImmutableSet.of(className);
        }

        @Override
        public Sha1HashCode getClassesHash() {
            // The only thing unique to canary classes is the index, which is captured by canaryDirName.
            Hasher hasher = Hashing.sha1().newHasher();
            hasher.putString(canaryDirName, Charsets.UTF_8);
            return new Sha1HashCode(hasher.hash().toString());
        }
    };
}