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

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

Introduction

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

Prototype

public static HashFunction md5() 

Source Link

Document

Returns a hash function implementing the MD5 hash algorithm (128 hash bits) by delegating to the MD5 MessageDigest .

Usage

From source file:org.jclouds.kinetic.strategy.internal.KineticStorageStrategyImpl.java

@Override
public String putBlob(final String containerName, final Blob blob) throws IOException {
    String blobKey = blob.getMetadata().getName();
    Payload payload = blob.getPayload();

    InputStream payloadStream = payload.openStream();

    HashingInputStream his = new HashingInputStream(Hashing.md5(), payloadStream);
    // Reset input stream back to beginning
    payloadStream.reset();/*from w  ww  . j  a va2 s.c  o  m*/

    kineticContainerNameValidator.validate(containerName);
    kineticBlobKeyValidator.validate(blobKey);
    if (getDirectoryBlobSuffix(blobKey) != null) {
        return putDirectoryBlob(containerName, blob);
    }

    long fileLength = payload.getContentMetadata().getContentLength();
    long chunksRequired = numberOfChunksForSize(fileLength);
    int chunkDataLength = KineticConstants.PROPERTY_CHUNK_SIZE_BYTES
            - KineticConstants.PROPERTY_CHUNK_FULL_HEADER_SIZE_BYTES;
    int currentChunk = 0;
    long fileId = -1;
    try {
        fileId = KineticDatabaseUtils.getInstance().getFileIdFromDatabase(containerName + "/" + blobKey);
    } catch (SQLException sqle) {
        sqle.printStackTrace();
    }
    while (currentChunk < chunksRequired) {
        Chunk chunk = new Chunk(this, fileId, currentChunk);
        byte[] chunkData = new byte[KineticConstants.PROPERTY_CHUNK_SIZE_BYTES];

        // Get header type values
        Map<String, String> headers = getChunkHeaders(containerName, blobKey, currentChunk);
        String chunkKey = getChunkKey(containerName, blobKey, currentChunk);

        // Set header values into the actual data of the chunk
        byte[] headerBytes = chunkKey.getBytes("UTF-8");
        for (int i = 0; i < headerBytes.length; i++) {
            chunkData[i] = headerBytes[i];
        }

        // Read data from blob into chunk
        payload.openStream().read(chunkData, headerBytes.length, chunkDataLength);
        chunk.setData(chunkData);

        // Send data to KDCC
        try {
            KineticDatabaseUtils.getInstance().addChunkToDatabase(chunkKey, chunkData);
        } catch (SQLException sqle) {
            return null;
        }

    }
    try {
        KineticDatabaseUtils.getInstance().addFileToDatabase(containerName + "/" + blobKey, fileLength);
    } catch (SQLException e) {
        e.printStackTrace();
    }

    if (payload != null) {
        payload.release();
    }
    return base16().lowerCase().encode(his.hash().asBytes());
}

From source file:com.facebook.buck.features.rust.RustCompileUtils.java

/**
 * Approximate what Cargo does - it computes a hash based on the crate version and its
 * dependencies. Buck will deal with the dependencies and we don't need to worry about the
 * version, but we do need to make sure that two crates with the same name in the build are
 * distinct - so compute the hash from the full target path.
 *
 * @param target Which target we're computing the hash for
 * @return Truncated MD5 hash of the target path
 *///from   ww w.ja v  a 2 s  .c  o  m
static String hashForTarget(BuildTarget target) {
    String name = target.getUnflavoredBuildTarget().getFullyQualifiedName();
    Hasher hasher = Hashing.md5().newHasher();
    HashCode hash = hasher.putString(name, StandardCharsets.UTF_8).hash();
    return hash.toString().substring(0, 16);
}

From source file:com.android.tools.lint.psi.extract.ExtractPsi.java

private static void writeCheckSumFiles(@NonNull File file) throws IOException {
    byte[] bytes = Files.toByteArray(file);
    Files.write(Hashing.md5().hashBytes(bytes).toString(), new File(file.getPath() + ".md5"), UTF_8);
    Files.write(Hashing.sha1().hashBytes(bytes).toString(), new File(file.getPath() + ".sha1"), UTF_8);
}

From source file:de.monticore.incremental.IncrementalChecker.java

/**
 * Calculate the MD5 checksum for the given file.
 * /*from ww w.j a  va 2  s. c o m*/
 * @param file
 * @return
 */
public static String getChecksum(String file) {
    try {
        return com.google.common.io.Files.hash(new File(file), Hashing.md5()).toString();
    } catch (IOException e) {
        Log.error("0xA1021 Failed to calculate current checksum for file " + file, e);
        return "";
    }
}

From source file:org.apache.twill.yarn.YarnTwillPreparer.java

private void createApplicationJar(final ApplicationBundler bundler, Map<String, LocalFile> localFiles)
        throws IOException {
    try {/*from  w  w w.j  ava  2  s  .c  o m*/
        final Set<Class<?>> classes = Sets.newIdentityHashSet();
        classes.addAll(dependencies);

        ClassLoader classLoader = getClassLoader();
        for (RuntimeSpecification spec : twillSpec.getRunnables().values()) {
            classes.add(classLoader.loadClass(spec.getRunnableSpecification().getClassName()));
        }

        // Add the TwillRunnableEventHandler class
        if (twillSpec.getEventHandler() != null) {
            classes.add(getClassLoader().loadClass(twillSpec.getEventHandler().getClassName()));
        }

        // The location name is computed from the MD5 of all the classes names
        // The localized name is always APPLICATION_JAR
        List<String> classList = Lists.newArrayList(Iterables.transform(classes, CLASS_TO_NAME));
        Collections.sort(classList);
        Hasher hasher = Hashing.md5().newHasher();
        for (String name : classList) {
            hasher.putString(name);
        }
        // Only depends on class list so that it can be reused across different launches
        String name = hasher.hash().toString() + "-" + Constants.Files.APPLICATION_JAR;

        LOG.debug("Create and copy {}", Constants.Files.APPLICATION_JAR);
        Location location = locationCache.get(name, new LocationCache.Loader() {
            @Override
            public void load(String name, Location targetLocation) throws IOException {
                bundler.createBundle(targetLocation, classes);
            }
        });

        LOG.debug("Done {}", Constants.Files.APPLICATION_JAR);

        localFiles.put(Constants.Files.APPLICATION_JAR,
                createLocalFile(Constants.Files.APPLICATION_JAR, location, true));

    } catch (ClassNotFoundException e) {
        throw Throwables.propagate(e);
    }
}

From source file:org.apache.twill.internal.appmaster.RunningContainers.java

private Location saveLogLevels() {
    LOG.debug("save the log level file");
    try {/*from w w w .ja  v  a  2 s  .c  om*/
        Gson gson = new GsonBuilder().serializeNulls().create();
        String jsonStr = gson.toJson(logLevels);
        String fileName = Hashing.md5().hashString(jsonStr) + "." + Constants.Files.LOG_LEVELS;
        Location location = applicationLocation.append(fileName);
        if (!location.exists()) {
            try (Writer writer = new OutputStreamWriter(location.getOutputStream(), Charsets.UTF_8)) {
                writer.write(jsonStr);
            }
        }
        LOG.debug("Done saving the log level file");
        return location;
    } catch (IOException e) {
        LOG.error("Failed to save the log level file.");
        return null;
    }
}

From source file:com.bouncestorage.swiftproxy.v1.ObjectResource.java

private Pair<Long, String> getManifestTotalSizeAndETag(Iterable<ManifestEntry> entries) {
    Hasher hash = Hashing.md5().newHasher();
    long segmentsTotalLength = 0;
    for (ManifestEntry entry : entries) {
        hash.putString(entry.etag, StandardCharsets.UTF_8);
        segmentsTotalLength += entry.size_bytes;
    }/*from   w  w  w  .  jav a  2  s. c o  m*/

    return new Pair<>(segmentsTotalLength, '"' + hash.hash().toString() + '"');
}

From source file:com.android.ide.eclipse.adt.internal.build.BuildHelper.java

private String getDexFileName(File inputFile) {
    // get the filename
    String name = inputFile.getName();
    // remove the extension
    int pos = name.lastIndexOf('.');
    if (pos != -1) {
        name = name.substring(0, pos);//from  www.  j a  v a 2  s  . c  om
    }

    // add a hash of the original file path
    HashFunction hashFunction = Hashing.md5();
    HashCode hashCode = hashFunction.hashString(inputFile.getAbsolutePath());

    return name + "-" + hashCode.toString() + ".jar";
}

From source file:co.cask.cdap.internal.app.runtime.adapter.AdapterService.java

private ApplicationTemplateInfo getTemplateInfo(File jarFile)
        throws InterruptedException, ExecutionException, TimeoutException, IOException {
    ApplicationTemplateInfo existing = fileToTemplateMap.get().get(jarFile.getAbsoluteFile());
    HashCode fileHash = Files.hash(jarFile, Hashing.md5());
    // if the file is the same, just return
    if (existing != null && fileHash.equals(existing.getFileHash())) {
        return existing;
    }/*from w  w  w . j a v a2 s  . com*/

    // instantiate the template application and call configure() on it to determine it's specification
    InMemoryConfigurator configurator = new InMemoryConfigurator(
            new LocalLocationFactory().create(jarFile.toURI()), null);
    ListenableFuture<ConfigResponse> result = configurator.config();
    ConfigResponse response = result.get(2, TimeUnit.MINUTES);
    InputSupplier<? extends Reader> configSupplier = response.get();
    if (response.getExitCode() != 0 || configSupplier == null) {
        throw new IllegalArgumentException("Failed to get template info");
    }
    ApplicationSpecification spec;
    try (Reader configReader = configSupplier.getInput()) {
        spec = GSON.fromJson(configReader, ApplicationSpecification.class);
    }

    // verify that the name is ok
    Id.Application.from(Constants.DEFAULT_NAMESPACE_ID, spec.getName());

    // determine the program type of the template
    ProgramType programType;
    int numWorkflows = spec.getWorkflows().size();
    int numWorkers = spec.getWorkers().size();
    if (numWorkers == 0 && numWorkflows == 1) {
        programType = ProgramType.WORKFLOW;
    } else if (numWorkers == 1 && numWorkflows == 0) {
        programType = ProgramType.WORKER;
    } else {
        throw new IllegalArgumentException(
                "An application template must contain exactly one worker or one workflow.");
    }

    return new ApplicationTemplateInfo(jarFile, spec.getName(), spec.getDescription(), programType, fileHash);
}

From source file:com.bouncestorage.swiftproxy.v1.ObjectResource.java

@PUT
public Response putObject(@NotNull @PathParam("container") String container,
        @NotNull @Encoded @PathParam("object") String objectName, @NotNull @PathParam("account") String account,
        @QueryParam("multipart-manifest") String multiPartManifest, @QueryParam("signature") String signature,
        @QueryParam("expires") String expires, @HeaderParam(DYNAMIC_OBJECT_MANIFEST) String objectManifest,
        @HeaderParam("X-Auth-Token") String authToken,
        @HeaderParam(HttpHeaders.CONTENT_LENGTH) String contentLengthParam,
        @HeaderParam("Transfer-Encoding") String transferEncoding,
        @HeaderParam(HttpHeaders.CONTENT_TYPE) MediaType contentType,
        @HeaderParam("X-Detect-Content-Type") boolean detectContentType,
        @HeaderParam("X-Copy-From") String copyFrom, @HeaderParam("X-Copy-From-Account") String copyFromAccount,
        @HeaderParam(HttpHeaders.ETAG) String eTag,
        @HeaderParam(HttpHeaders.CONTENT_DISPOSITION) String contentDisposition,
        @HeaderParam(HttpHeaders.CONTENT_ENCODING) String contentEncoding,
        @HeaderParam("X-Delete-At") long deleteAt, @HeaderParam("X-Delete-After") long deleteAfter,
        @HeaderParam(HttpHeaders.IF_MATCH) String ifMatch,
        @HeaderParam(HttpHeaders.IF_NONE_MATCH) String ifNoneMatch,
        @HeaderParam(HttpHeaders.IF_MODIFIED_SINCE) Date ifModifiedSince,
        @HeaderParam(HttpHeaders.IF_UNMODIFIED_SINCE) Date ifUnmodifiedSince,
        @HeaderParam(SwiftHeaders.OBJECT_COPY_FRESH_METADATA) boolean freshMetadata, @Context Request request) {
    //objectName = normalizePath(objectName);
    if (objectName.length() > InfoResource.CONFIG.swift.max_object_name_length) {
        return badRequest();
    }// w ww .j ava 2s .  c om
    if (transferEncoding != null && !"chunked".equals(transferEncoding)) {
        return Response.status(Response.Status.NOT_IMPLEMENTED).build();
    }

    if (contentLengthParam == null && !"chunked".equals(transferEncoding)) {
        return Response.status(Response.Status.LENGTH_REQUIRED).build();
    }
    long contentLength = contentLengthParam == null ? 0 : Long.parseLong(contentLengthParam);

    logger.info("PUT {}", objectName);

    if (copyFromAccount == null) {
        copyFromAccount = account;
    }

    if (copyFrom != null) {
        Pair<String, String> copy = validateCopyParam(copyFrom);
        return copyObject(copy.getFirst(), copy.getSecond(), copyFromAccount, authToken,
                container + "/" + objectName, account, null, contentType.toString(), contentEncoding,
                contentDisposition, ifMatch, ifModifiedSince, ifUnmodifiedSince, freshMetadata, request);
    }

    Map<String, String> metadata = getUserMetadata(request);
    validateUserMetadata(metadata);
    InputStream copiedStream = null;

    BlobStore blobStore = getBlobStore(authToken).get(container, objectName);
    if ("put".equals(multiPartManifest)) {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        try (TeeInputStream tee = new TeeInputStream(request.getInputStream(), buffer, true)) {
            ManifestEntry[] manifest = readSLOManifest(tee);
            validateManifest(manifest, blobStore, authToken);
            Pair<Long, String> sizeAndEtag = getManifestTotalSizeAndETag(Arrays.asList(manifest));
            metadata.put(STATIC_OBJECT_MANIFEST, sizeAndEtag.getFirst() + " " + sizeAndEtag.getSecond());
            copiedStream = new ByteArrayInputStream(buffer.toByteArray());
        } catch (IOException e) {
            throw propagate(e);
        }
    } else if (objectManifest != null) {
        metadata.put(DYNAMIC_OBJECT_MANIFEST, objectManifest);
    }

    if (!blobStore.containerExists(container)) {
        return notFound();
    }

    HashCode contentMD5 = null;
    if (eTag != null) {
        try {
            contentMD5 = HashCode.fromBytes(BaseEncoding.base16().lowerCase().decode(eTag));
        } catch (IllegalArgumentException iae) {
            throw new ClientErrorException(422, iae); // Unprocessable Entity
        }
        if (contentMD5.bits() != Hashing.md5().bits()) {
            // Unprocessable Entity
            throw new ClientErrorException(contentMD5.bits() + " != " + Hashing.md5().bits(), 422);
        }
    }

    try (InputStream is = copiedStream != null ? copiedStream : request.getInputStream()) {
        BlobBuilder.PayloadBlobBuilder builder = blobStore.blobBuilder(objectName).userMetadata(metadata)
                .payload(is);
        if (contentDisposition != null) {
            builder.contentDisposition(contentDisposition);
        }
        if (contentEncoding != null) {
            builder.contentEncoding(contentEncoding);
        }
        if (contentType != null) {
            builder.contentType(contentType.toString());
        }
        if (contentLengthParam != null) {
            builder.contentLength(contentLength);
        }
        if (contentMD5 != null) {
            builder.contentMD5(contentMD5);
        }
        try {
            String remoteETag;
            try {
                remoteETag = blobStore.putBlob(container, builder.build());
            } catch (HttpResponseException e) {
                HttpResponse response = e.getResponse();
                if (response == null) {
                    throw e;
                }
                int code = response.getStatusCode();

                if (code == 400 && !"openstack-swift".equals(blobStore.getContext().unwrap().getId())) {
                    // swift expects 422 for md5 mismatch
                    throw new ClientErrorException(response.getStatusLine(), 422, e.getCause());
                } else {
                    throw new ClientErrorException(response.getStatusLine(), code, e.getCause());
                }
            }
            BlobMetadata meta = blobStore.blobMetadata(container, objectName);
            return Response.status(Response.Status.CREATED).header(HttpHeaders.ETAG, remoteETag)
                    .header(HttpHeaders.LAST_MODIFIED, meta.getLastModified())
                    .header(HttpHeaders.CONTENT_LENGTH, 0).header(HttpHeaders.CONTENT_TYPE, contentType)
                    .header(HttpHeaders.DATE, new Date()).build();
        } catch (ContainerNotFoundException e) {
            return notFound();
        }
    } catch (IOException e) {
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
}