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:com.complexible.pinto.RDFMapper.java

/**
 * Get or generate an rdf:ID for the given object
 * @param theT  the object/*w  ww  .j a  va2s . com*/
 * @return      the rdf:ID
 */
private <T> Resource id(final T theT) {
    if (theT instanceof Identifiable) {
        Identifiable aIdentifiable = (Identifiable) theT;

        if (aIdentifiable.id() != null) {
            return aIdentifiable.id();
        }
    }

    final Iterable<String> aProps = () -> StreamSupport
            .stream(Beans.getDeclaredMethods(theT.getClass()).spliterator(), false)
            .filter(Methods.annotated(RdfId.class)).map(Methods.property()).iterator();

    // Sort the properties so they're always iterated over in the same order.  since the hash is sensitive
    // to iteration order, the same inputs but in a different order yields a different hashed value, and thus
    // a different ID, even though it's the *same* resource.
    final List<String> aSorted = Ordering.natural().sortedCopy(aProps);

    Resource aId = null;
    if (!Iterables.isEmpty(aSorted)) {
        Hasher aFunc = Hashing.md5().newHasher();
        for (String aProp : aSorted) {
            try {
                final Object aValue = PropertyUtils.getProperty(theT, aProp);

                if (aValue == null) {
                    continue;
                }

                aFunc.putString(aValue.toString(), Charsets.UTF_8);
            } catch (Exception e) {
                Throwables.propagateIfInstanceOf(e, RDFMappingException.class);
                throw new RDFMappingException(e);
            }
        }

        aId = mValueFactory.createIRI(mDefaultNamespace + aFunc.hash().toString());
    }

    for (Map.Entry<Class<?>, Function<Object, Resource>> aEntry : mIdFunctions.entrySet()) {
        if (aEntry.getKey().isAssignableFrom(theT.getClass())) {
            aId = aEntry.getValue().apply(theT);
            break;
        }
    }

    if (aId == null && mMappingOptions.is(MappingOptions.REQUIRE_IDS)) {
        throw new UnidentifiableObjectException(
                String.format(
                        "No identifier was found for %s!  The instance should "
                                + "implement Identifiable, have one or more properties "
                                + "annotated with @RdfId, or have an id function provided " + "to the mapper.",
                        theT));
    } else {
        if (aId == null) {
            aId = mValueFactory.createIRI(mDefaultNamespace
                    + Hashing.md5().newHasher().putString(theT.toString(), Charsets.UTF_8).hash().toString());
        }

        if (theT instanceof Identifiable) {
            ((Identifiable) theT).id(aId);
        }

        return aId;
    }
}

From source file:org.eclipse.andmore.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 .  jav a  2s.  c  o m
    }

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

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

From source file:org.eclipse.che.api.vfs.impl.file.LocalVirtualFileSystem.java

List<Pair<String, String>> countMd5Sums(LocalVirtualFile virtualFile) throws ServerException {
    if (virtualFile.isFile()) {
        return emptyList();
    }//from w w w.  jav a 2 s .  co m
    return new HashSumsCounter(virtualFile, Hashing.md5()).countHashSums();
}

From source file:org.jetbrains.android.run.AndroidRunningState.java

private static void trackInstallation(@NotNull IDevice device) {
    if (!UsageTracker.getInstance().canTrack()) {
        return;//  w  w w  .ja v  a 2 s.  c o m
    }

    UsageTracker.getInstance().trackEvent(UsageTracker.CATEGORY_DEPLOYMENT, UsageTracker.ACTION_APK_DEPLOYED,
            null, null);

    UsageTracker.getInstance().trackEvent(UsageTracker.CATEGORY_DEVICEINFO,
            UsageTracker.INFO_DEVICE_SERIAL_HASH,
            Hashing.md5().hashString(device.getSerialNumber(), Charsets.UTF_8).toString(), null);
    UsageTracker.getInstance().trackEvent(UsageTracker.CATEGORY_DEVICEINFO, UsageTracker.INFO_DEVICE_BUILD_TAGS,
            device.getProperty(IDevice.PROP_BUILD_TAGS), null);
    UsageTracker.getInstance().trackEvent(UsageTracker.CATEGORY_DEVICEINFO, UsageTracker.INFO_DEVICE_BUILD_TYPE,
            device.getProperty(IDevice.PROP_BUILD_TYPE), null);
    UsageTracker.getInstance().trackEvent(UsageTracker.CATEGORY_DEVICEINFO,
            UsageTracker.INFO_DEVICE_BUILD_VERSION_RELEASE, device.getProperty(IDevice.PROP_BUILD_VERSION),
            null);
    UsageTracker.getInstance().trackEvent(UsageTracker.CATEGORY_DEVICEINFO,
            UsageTracker.INFO_DEVICE_BUILD_API_LEVEL, device.getProperty(IDevice.PROP_BUILD_API_LEVEL), null);
    UsageTracker.getInstance().trackEvent(UsageTracker.CATEGORY_DEVICEINFO,
            UsageTracker.INFO_DEVICE_MANUFACTURER, device.getProperty(IDevice.PROP_DEVICE_MANUFACTURER), null);
    UsageTracker.getInstance().trackEvent(UsageTracker.CATEGORY_DEVICEINFO, UsageTracker.INFO_DEVICE_MODEL,
            device.getProperty(IDevice.PROP_DEVICE_MODEL), null);
    UsageTracker.getInstance().trackEvent(UsageTracker.CATEGORY_DEVICEINFO, UsageTracker.INFO_DEVICE_CPU_ABI,
            device.getProperty(IDevice.PROP_DEVICE_CPU_ABI), null);
}

From source file:org.gaul.s3proxy.S3ProxyHandler.java

private void handlePutBlob(HttpServletRequest request, HttpServletResponse response, BlobStore blobStore,
        String containerName, String blobName) throws IOException, S3Exception {
    // Flag headers present since HttpServletResponse.getHeader returns
    // null for empty headers values.
    String contentLengthString = null;
    String contentMD5String = null;
    for (String headerName : Collections.list(request.getHeaderNames())) {
        String headerValue = Strings.nullToEmpty(request.getHeader(headerName));
        if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH)) {
            contentLengthString = headerValue;
        } else if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_MD5)) {
            contentMD5String = headerValue;
        }/*ww  w.j  ava  2s .  com*/
    }

    HashCode contentMD5 = null;
    if (contentMD5String != null) {
        try {
            contentMD5 = HashCode.fromBytes(BaseEncoding.base64().decode(contentMD5String));
        } catch (IllegalArgumentException iae) {
            throw new S3Exception(S3ErrorCode.INVALID_DIGEST, iae);
        }
        if (contentMD5.bits() != Hashing.md5().bits()) {
            throw new S3Exception(S3ErrorCode.INVALID_DIGEST);
        }
    }

    if (contentLengthString == null) {
        throw new S3Exception(S3ErrorCode.MISSING_CONTENT_LENGTH);
    }
    long contentLength;
    try {
        contentLength = Long.parseLong(contentLengthString);
    } catch (NumberFormatException nfe) {
        throw new S3Exception(S3ErrorCode.INVALID_ARGUMENT, nfe);
    }
    if (contentLength < 0) {
        throw new S3Exception(S3ErrorCode.INVALID_ARGUMENT);
    }

    try (InputStream is = request.getInputStream()) {
        BlobBuilder.PayloadBlobBuilder builder = blobStore.blobBuilder(blobName).payload(is)
                .contentLength(request.getContentLength());
        addContentMetdataFromHttpRequest(builder, request);
        if (contentMD5 != null) {
            builder = builder.contentMD5(contentMD5);
        }

        PutOptions options = new PutOptions();
        String blobStoreType = getBlobStoreType(blobStore);
        if (blobStoreType.equals("azureblob") && contentLength > 64 * 1024 * 1024) {
            options.multipart(true);
        }
        String eTag;
        try {
            eTag = blobStore.putBlob(containerName, builder.build(), options);
        } catch (HttpResponseException hre) {
            HttpResponse hr = hre.getResponse();
            if (hr == null) {
                return;
            }
            int status = hr.getStatusCode();
            switch (status) {
            case HttpServletResponse.SC_BAD_REQUEST:
            case 422: // Swift returns 422 Unprocessable Entity
                throw new S3Exception(S3ErrorCode.BAD_DIGEST);
            default:
                // TODO: emit hre.getContent() ?
                response.sendError(status);
                break;
            }
            return;
        } catch (RuntimeException re) {
            if (Throwables2.getFirstThrowableOfType(re, TimeoutException.class) != null) {
                throw new S3Exception(S3ErrorCode.REQUEST_TIMEOUT, re);
            } else {
                throw re;
            }
        }

        // S3 quotes ETag while Swift does not
        if (!eTag.startsWith("\"") && !eTag.endsWith("\"")) {
            eTag = '"' + eTag + '"';
        }
        response.addHeader(HttpHeaders.ETAG, eTag);
    }

    // TODO: jclouds should include this in PutOptions
    String cannedAcl = request.getHeader("x-amz-acl");
    if (cannedAcl != null && !cannedAcl.equalsIgnoreCase("private")) {
        handleSetBlobAcl(request, response, blobStore, containerName, blobName);
    }
}

From source file:org.dllearner.algorithms.qtl.experiments.QTLEvaluation.java

private void solutionsFromCache(String sparqlQuery, int possibleNrOfExamples, double noise) {
    HashFunction hf = Hashing.md5();
    String hash = hf.newHasher().putString(sparqlQuery, Charsets.UTF_8).putInt(possibleNrOfExamples)
            .putDouble(noise).hash().toString();
    File file = new File(cacheDirectory, hash + "-data.ttl");
    if (file.exists()) {

    }/*from  ww  w.  j  a  v a2  s .c  o m*/
}

From source file:org.gaul.s3proxy.S3ProxyHandler.java

private void handleUploadPart(HttpServletRequest request, HttpServletResponse response, BlobStore blobStore,
        String containerName, String blobName, String uploadId) throws IOException, S3Exception {
    // TODO: duplicated from handlePutBlob
    String contentLengthString = null;
    String contentMD5String = null;
    for (String headerName : Collections.list(request.getHeaderNames())) {
        String headerValue = Strings.nullToEmpty(request.getHeader(headerName));
        if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH)) {
            contentLengthString = headerValue;
        } else if (headerName.equalsIgnoreCase(HttpHeaders.CONTENT_MD5)) {
            contentMD5String = headerValue;
        }//from w w  w.j a  v a2 s  .  co  m
    }

    HashCode contentMD5 = null;
    if (contentMD5String != null) {
        try {
            contentMD5 = HashCode.fromBytes(BaseEncoding.base64().decode(contentMD5String));
        } catch (IllegalArgumentException iae) {
            throw new S3Exception(S3ErrorCode.INVALID_DIGEST, iae);
        }
        if (contentMD5.bits() != Hashing.md5().bits()) {
            throw new S3Exception(S3ErrorCode.INVALID_DIGEST);
        }
    }

    if (contentLengthString == null) {
        throw new S3Exception(S3ErrorCode.MISSING_CONTENT_LENGTH);
    }
    long contentLength;
    try {
        contentLength = Long.parseLong(contentLengthString);
    } catch (NumberFormatException nfe) {
        throw new S3Exception(S3ErrorCode.INVALID_ARGUMENT, nfe);
    }
    if (contentLength < 0) {
        throw new S3Exception(S3ErrorCode.INVALID_ARGUMENT);
    }

    String partNumberString = request.getParameter("partNumber");
    if (partNumberString == null) {
        throw new S3Exception(S3ErrorCode.INVALID_ARGUMENT);
    }
    int partNumber;
    try {
        partNumber = Integer.parseInt(partNumberString);
    } catch (NumberFormatException nfe) {
        throw new S3Exception(S3ErrorCode.INVALID_ARGUMENT,
                "Part number must be an integer between 1 and 10000" + ", inclusive", nfe,
                ImmutableMap.of("ArgumentName", "partNumber", "ArgumentValue", partNumberString));
    }
    if (partNumber < 1 || partNumber > 10_000) {
        throw new S3Exception(S3ErrorCode.INVALID_ARGUMENT,
                "Part number must be an integer between 1 and 10000" + ", inclusive", (Throwable) null,
                ImmutableMap.of("ArgumentName", "partNumber", "ArgumentValue", partNumberString));
    }

    // TODO: how to reconstruct original mpu?
    MultipartUpload mpu = MultipartUpload.create(containerName, blobName, uploadId,
            createFakeBlobMetadata(blobStore));

    try (InputStream is = request.getInputStream()) {
        Payload payload = Payloads.newInputStreamPayload(is);
        payload.getContentMetadata().setContentLength(contentLength);
        if (contentMD5 != null) {
            payload.getContentMetadata().setContentMD5(contentMD5);
        }

        MultipartPart part = blobStore.uploadMultipartPart(mpu, partNumber, payload);
        response.addHeader(HttpHeaders.ETAG, "\"" + part.partETag() + "\"");
    }
}

From source file:org.dllearner.algorithms.qtl.experiments.QTLEvaluation.java

private String hash(String query) {
    return Hashing.md5().newHasher().putString(query, Charsets.UTF_8).hash().toString();
}

From source file:org.eclipse.che.vfs.impl.fs.FSMountPoint.java

LazyIterator<Pair<String, String>> countMd5Sums(VirtualFileImpl virtualFile) throws ServerException {
    if (!virtualFile.isFolder()) {
        return LazyIterator.emptyIterator();
    }/*from   w  w  w . ja v  a  2  s .c  o  m*/
    final List<Pair<String, String>> hashes = new ArrayList<>();
    final int trimPathLength = virtualFile.getPath().length() + 1;
    final HashFunction hashFunction = Hashing.md5();
    final ValueHolder<ServerException> errorHolder = new ValueHolder<>();
    virtualFile.accept(new VirtualFileVisitor() {
        @Override
        public void visit(final VirtualFile virtualFile) {
            try {
                if (virtualFile.isFile()) {
                    hashes.add(Pair.of(countHashSum(virtualFile, hashFunction),
                            virtualFile.getPath().substring(trimPathLength)));
                } else {
                    final LazyIterator<VirtualFile> children = virtualFile.getChildren(VirtualFileFilter.ALL);
                    while (children.hasNext()) {
                        children.next().accept(this);
                    }
                }
            } catch (ServerException e) {
                errorHolder.set(e);
            }
        }
    });
    return LazyIterator.fromList(hashes);
}

From source file:com.cloudera.director.aws.ec2.EC2Provider.java

/**
 * Combines all the virtual instance IDs together in a single token than
 * can be used to make sure we can safely retry any runInstances() call.
 *
 * @see <a href="http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Run_Instance_Idempotency.html">Ensuring Idempotency</a>
 *///from ww  w. j  a v  a 2  s  .  c om
private String getHashOfVirtualInstanceIdsForClientToken(Collection<String> virtualInstanceIds,
        Optional<Long> discriminator) {
    // Using MD5 because clientToken should be less than 64 characters long
    Hasher hasher = Hashing.md5().newHasher(virtualInstanceIds.size());

    // We are sorting the input list because we want our hash to be order independent
    for (String id : Sets.newTreeSet(virtualInstanceIds)) {
        hasher.putString(id, Charsets.UTF_8);
    }
    if (discriminator.isPresent()) {
        hasher.putLong(discriminator.get());
    }
    return hasher.hash().toString();
}