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

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

Introduction

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

Prototype

@CheckReturnValue
HashCode hash();

Source Link

Document

Computes a hash code based on the data that have been provided to this hasher.

Usage

From source file:org.darkware.wpman.security.ChecksumDatabase.java

/**
 * Perform a checksum calculation on the given {@link ReadableByteChannel}. Other code should not create
 * implementations which are dependant on any particular characteristics of the checksum, but the checksum
 * is very likely to be based on a cryptographic-strength hash. The results of the checksum are encoded as
 * a base64 {@code String}./*from   w w w  .java 2s .  c om*/
 *
 * @param channel The {@code ReadableByteChannel} to read data from.
 * @return A Base64 encoded {@code String} representing the checksum.
 * @throws IOException If there was an error while reading data from the channel.
 * @see Base64#encodeBase64String(byte[])
 */
protected String doChecksum(ReadableByteChannel channel) throws IOException {
    Hasher hasher = Hashing.sha256().newHasher();

    final ByteBuffer block = ByteBuffer.allocate(4096);
    while (channel.isOpen()) {
        int bytesRead = channel.read(block);
        if (bytesRead > 0) {
            block.flip();
            hasher.putBytes(block.array(), 0, block.limit());
            block.clear();
        } else if (bytesRead == -1) {
            channel.close();
        }
    }

    return Base64.encodeBase64String(hasher.hash().asBytes());
}

From source file:org.jooby.assets.SvgSprites.java

private String sha1(final File dir, final File sprite, final File css) throws IOException {
    try (Stream<Path> stream = Files.walk(dir.toPath())) {
        Hasher sha1 = Hashing.sha1().newHasher();
        stream.filter(p -> !Files.isDirectory(p))
                .forEach(p -> Try.run(() -> sha1.putBytes(Files.readAllBytes(p))));
        if (sprite.exists()) {
            sha1.putBytes(Files.readAllBytes(sprite.toPath()));
        }//www .  j av a  2 s. c o m
        if (css.exists()) {
            sha1.putBytes(Files.readAllBytes(css.toPath()));
        }
        return BaseEncoding.base16().encode(sha1.hash().asBytes()).toLowerCase();
    }
}

From source file:com.facebook.buck.core.build.engine.buildinfo.DefaultOnDiskBuildInfo.java

@Override
public void writeOutputHashes(FileHashCache fileHashCache) throws IOException {
    ImmutableSortedSet<Path> pathsForArtifact = getPathsForArtifact();

    // Grab and record the output hashes in the build metadata so that cache hits avoid re-hashing
    // file contents.  Since we use output hashes for input-based rule keys and for detecting
    // non-determinism, we would spend a lot of time re-hashing output paths -- potentially in
    // serialized in a single step. So, do the hashing here to distribute the workload across
    // several threads and cache the results.
    ImmutableSortedMap.Builder<String, String> outputHashes = ImmutableSortedMap.naturalOrder();
    Hasher hasher = Hashing.sha1().newHasher();
    for (Path path : pathsForArtifact) {
        String pathString = path.toString();
        HashCode fileHash = fileHashCache.get(projectFilesystem, path);
        hasher.putBytes(pathString.getBytes(Charsets.UTF_8));
        hasher.putBytes(fileHash.asBytes());
        outputHashes.put(pathString, fileHash.toString());
    }/*  w w w.  java2 s  .c  o  m*/

    projectFilesystem.writeContentsToPath(ObjectMappers.WRITER.writeValueAsString(outputHashes.build()),
            metadataDirectory.resolve(BuildInfo.MetadataKey.RECORDED_PATH_HASHES));

    projectFilesystem.writeContentsToPath(hasher.hash().toString(),
            metadataDirectory.resolve(BuildInfo.MetadataKey.OUTPUT_HASH));
}

From source file:com.vmware.appfactory.common.base.AbstractApiController.java

/**
 * Sets an e-tag on the request calculated from the given DAO.
 *
 * If the request contains the same e-tag, this means the resource
 * has not been modified since the last request, and the browser cache
 * already contains the whole response.//from ww  w  . j  av  a  2  s. c om
 *
 * In this case, we'll set the HTTP headers to indicate that the
 * response has not been modified, and return true.
 *
 * @param request    - web request, used to get client's etag (if any)
 * @param response   - web response, used to set 304 response if necessary
 * @param objCollection - A collection of objects. If set, the e-tag is computed based on the hashCode
 *                      for each of the objects.
 * @param daoList    - one or more DAOs to query.  The e-tag is computed using the current state of each
 *                      of these tables, such that if any changes the computed e-tag should change.
 * @return
 * true - if the client already has a cached version of the resource.
 * When this is returned, this method has written an HTTP response,
 * and the caller should simply return immediately.
 *
 * false - if the E-Tag header has been set and the caller should
 * proceed to generate and write a normal response.
 *
 * @throws IOException
 * if the 304 response could not be written
 */
public boolean checkModified(@Nonnull HttpServletRequest request, @Nonnull HttpServletResponse response,
        Collection<? extends Object> objCollection, @Nonnull AfDao... daoList) throws IOException {

    String charSequence = "optimus-prime";
    Hasher hasher = hashFunction.newHasher().putString(charSequence);

    for (AfDao dao : daoList) {
        long lastModified = dao.lastModified();
        long size = dao.countAll();
        hasher.putLong(lastModified).putLong(size);
    }

    // If the objects are not present, put a 0 value indicating 0 sized objects.
    if (CollectionUtils.isNotEmpty(objCollection)) {
        for (Object o : objCollection) {
            if (o != null) {
                hasher.putInt(o.hashCode());
            } else {
                // Indicating that there was an object but null.
                hasher.putInt(0);
            }
        }
    }

    String newToken = hasher.hash().toString();

    String oldToken = applyETagCache(request, response, newToken, false);
    return newToken.equals(oldToken);
}

From source file:com.facebook.buck.parser.SerialDaemonicParserState.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private TargetNode<?> createTargetNode(BuckEventBus eventBus, Cell cell, Path buildFile, BuildTarget target,
        Map<String, Object> rawNode, TargetNodeListener nodeListener) {
    BuildRuleType buildRuleType = parseBuildRuleTypeFromRawRule(cell, rawNode);

    // Because of the way that the parser works, we know this can never return null.
    Description<?> description = cell.getDescription(buildRuleType);

    if (target.isFlavored()) {
        if (description instanceof Flavored) {
            if (!((Flavored) description).hasFlavors(ImmutableSet.copyOf(target.getFlavors()))) {
                throw new HumanReadableException("Unrecognized flavor in target %s while parsing %s%s.", target,
                        UnflavoredBuildTarget.BUILD_TARGET_PREFIX, MorePaths
                                .pathWithUnixSeparators(target.getBasePath().resolve(cell.getBuildFileName())));
            }//from w  w w  . j  a va2 s  . c  om
        } else {
            LOG.warn(
                    "Target %s (type %s) must implement the Flavored interface "
                            + "before we can check if it supports flavors: %s",
                    target.getUnflavoredBuildTarget(), buildRuleType, target.getFlavors());
            throw new HumanReadableException(
                    "Target %s (type %s) does not currently support flavors (tried %s)",
                    target.getUnflavoredBuildTarget(), buildRuleType, target.getFlavors());
        }
    }

    Cell targetCell = cell.getCell(target);
    BuildRuleFactoryParams factoryParams = new BuildRuleFactoryParams(targetCell.getFilesystem(),
            target.withoutCell(),
            new FilesystemBackedBuildFileTree(cell.getFilesystem(), cell.getBuildFileName()),
            targetCell.isEnforcingBuckPackageBoundaries());
    Object constructorArg = description.createUnpopulatedConstructorArg();
    try {
        ImmutableSet.Builder<BuildTarget> declaredDeps = ImmutableSet.builder();
        ImmutableSet.Builder<BuildTargetPattern> visibilityPatterns = ImmutableSet.builder();
        try (SimplePerfEvent.Scope scope = SimplePerfEvent.scope(eventBus,
                PerfEventId.of("MarshalledConstructorArg"), "target", target)) {
            marshaller.populate(targetCell.getCellRoots(), targetCell.getFilesystem(), factoryParams,
                    constructorArg, declaredDeps, visibilityPatterns, rawNode);
        }
        try (SimplePerfEvent.Scope scope = SimplePerfEvent.scope(eventBus, PerfEventId.of("CreatedTargetNode"),
                "target", target)) {
            Hasher hasher = Hashing.sha1().newHasher();
            hasher.putString(BuckVersion.getVersion(), UTF_8);
            JsonObjectHashing.hashJsonObject(hasher, rawNode);
            targetsCornucopia.put(target.getUnflavoredBuildTarget(), target);
            TargetNode<?> node = new TargetNode(hasher.hash(), description, constructorArg, typeCoercerFactory,
                    factoryParams, declaredDeps.build(), visibilityPatterns.build(), targetCell.getCellRoots());
            nodeListener.onCreate(buildFile, node);
            return node;
        }
    } catch (NoSuchBuildTargetException | TargetNode.InvalidSourcePathInputException e) {
        throw new HumanReadableException(e);
    } catch (ConstructorArgMarshalException e) {
        throw new HumanReadableException("%s: %s", target, e.getMessage());
    } catch (IOException e) {
        throw new HumanReadableException(e.getMessage(), e);
    }
}

From source file:org.apache.beam.runners.fnexecution.artifact.BeamFileSystemArtifactRetrievalService.java

@Override
public void getArtifact(ArtifactApi.GetArtifactRequest request,
        StreamObserver<ArtifactApi.ArtifactChunk> responseObserver) {
    LOG.debug("GetArtifact {}", request);
    String name = request.getName();
    try {/*from  www  . j a va  2 s  .co  m*/
        ArtifactApi.ProxyManifest proxyManifest = MANIFEST_CACHE.get(request.getRetrievalToken());
        // look for file at URI specified by proxy manifest location
        ArtifactApi.ProxyManifest.Location location = proxyManifest.getLocationList().stream()
                .filter(loc -> loc.getName().equals(name)).findFirst()
                .orElseThrow(() -> new StatusRuntimeException(Status.NOT_FOUND
                        .withDescription(String.format("Artifact location not found in manifest: %s", name))));

        List<ArtifactMetadata> existingArtifacts = proxyManifest.getManifest().getArtifactList();
        ArtifactMetadata metadata = existingArtifacts.stream().filter(meta -> meta.getName().equals(name))
                .findFirst().orElseThrow(() -> new StatusRuntimeException(Status.NOT_FOUND
                        .withDescription(String.format("Artifact metadata not found in manifest: %s", name))));

        ResourceId artifactResourceId = FileSystems.matchNewResource(location.getUri(),
                false /* is directory */);
        LOG.debug("Artifact {} located in {}", name, artifactResourceId);
        Hasher hasher = Hashing.sha256().newHasher();
        byte[] data = new byte[ARTIFACT_CHUNK_SIZE_BYTES];
        try (InputStream stream = Channels.newInputStream(FileSystems.open(artifactResourceId))) {
            int len;
            while ((len = stream.read(data)) != -1) {
                hasher.putBytes(data, 0, len);
                responseObserver.onNext(ArtifactApi.ArtifactChunk.newBuilder()
                        .setData(ByteString.copyFrom(data, 0, len)).build());
            }
        }
        if (metadata.getSha256() != null && !metadata.getSha256().isEmpty()) {
            String expected = metadata.getSha256();
            String actual = hasher.hash().toString();
            if (!actual.equals(expected)) {
                throw new StatusRuntimeException(Status.DATA_LOSS.withDescription(String.format(
                        "Artifact %s is corrupt: expected sha256 %s, actual %s", name, expected, actual)));
            }
        }
        responseObserver.onCompleted();
    } catch (IOException | ExecutionException e) {
        LOG.info("GetArtifact {} failed", request, e);
        responseObserver.onError(e);
    }
}

From source file:com.facebook.buck.cxx.PreprocessorDelegate.java

public String hashCommand(ImmutableList<String> flags) {
    Hasher hasher = Hashing.murmur3_128().newHasher();
    String workingDirString = workingDir.toString();
    // Skips the executable argument (the first one) as that is not sanitized.
    for (String part : sanitizer.sanitizeFlags(Iterables.skip(flags, 1))) {
        // TODO(#10251354): find a better way of dealing with getting a project dir normalized hash
        if (part.startsWith(workingDirString)) {
            part = "<WORKINGDIR>" + part.substring(workingDirString.length());
        }//from   w  w w . j a  va  2  s .  c o m
        hasher.putString(part, Charsets.UTF_8);
        hasher.putBoolean(false); // separator
    }
    return hasher.hash().toString();
}

From source file:com.facebook.buck.java.DefaultJavaLibraryRule.java

/**
 * Creates the total ABI key for this rule. If export_deps is true, the total key is computed by
 * hashing the ABI keys of the dependencies together with the ABI key of this rule. If export_deps
 * is false, the standalone ABI key for this rule is used as the total key.
 * @param abiKey the standalone ABI key for this rule.
 * @return total ABI key containing also the ABI keys of the dependencies.
 *///from ww w  .  j a va2 s . c o m
protected Sha1HashCode createTotalAbiKey(Sha1HashCode abiKey) {
    if (getExportedDeps().isEmpty()) {
        return abiKey;
    }

    SortedSet<JavaLibraryRule> depsForAbiKey = getDepsForAbiKey();

    // Hash the ABI keys of all dependencies together with ABI key for the current rule.
    Hasher hasher = createHasherWithAbiKeyForDeps(depsForAbiKey);
    hasher.putUnencodedChars(abiKey.getHash());
    return new Sha1HashCode(hasher.hash().toString());
}

From source file:org.gradle.api.internal.changedetection.state.DefaultClasspathEntryHasher.java

private HashCode hashJar(FileDetails fileDetails, Hasher hasher,
        ClasspathContentHasher classpathContentHasher) {
    ZipFile zipFile = null;//from  ww  w  . j a v a 2 s .  co  m
    try {
        zipFile = new ZipFile(new File(fileDetails.getPath()));
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        // Ensure we visit the zip entries in a deterministic order
        Map<String, ZipEntry> entriesByName = new TreeMap<String, ZipEntry>();
        while (entries.hasMoreElements()) {
            ZipEntry zipEntry = entries.nextElement();
            if (!zipEntry.isDirectory()) {
                entriesByName.put(zipEntry.getName(), zipEntry);
            }
        }
        for (ZipEntry zipEntry : entriesByName.values()) {
            visit(zipFile, zipEntry, hasher, classpathContentHasher);
        }
        return hasher.hash();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        IOUtils.closeQuietly(zipFile);
    }
}

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

@Override
public int execute(final ExecutionContext context) {
    ImmutableList.Builder<Path> allInputs = ImmutableList.builder();
    allInputs.addAll(primaryInputsToDex.get());
    if (secondaryOutputToInputs.isPresent()) {
        allInputs.addAll(secondaryOutputToInputs.get().get().values());
    }//from www.jav  a 2 s  .co m

    final Map<String, HashCode> classNamesToHashes = classNamesToHashesSupplier.get();

    for (Path path : allInputs.build()) {
        try {
            final Hasher hasher = Hashing.sha1().newHasher();
            new DefaultClasspathTraverser().traverse(
                    new ClasspathTraversal(Collections.singleton(path), context.getProjectFilesystem()) {
                        @Override
                        public void visit(FileLike fileLike) throws IOException {
                            String className = fileLike.getRelativePath().replaceAll("\\.class$", "");
                            if (classNamesToHashes.containsKey(className)) {
                                HashCode classHash = classNamesToHashes.get(className);
                                hasher.putBytes(classHash.asBytes());
                            }
                        }
                    });
            dexInputsToHashes.put(path, new Sha1HashCode(hasher.hash().toString()));
        } catch (IOException e) {
            context.logError(e, "Error hashing smart dex input: %s", path);
            return 1;
        }
    }
    stepFinished = true;
    return 0;
}