Example usage for org.apache.commons.codec.digest DigestUtils md5

List of usage examples for org.apache.commons.codec.digest DigestUtils md5

Introduction

In this page you can find the example usage for org.apache.commons.codec.digest DigestUtils md5.

Prototype

public static byte[] md5(String data) 

Source Link

Usage

From source file:jobhunter.persistence.Persistence.java

private Boolean wasModified(final File file) {
    if (this.lastModification != file.lastModified())
        return true;

    try (InputStream in = new FileInputStream(file)) {
        return Arrays.equals(this.md5sum, DigestUtils.md5(in));
    } catch (IOException e) {
        l.error("Failed to read MD5 checksum from {}", file.toString(), e);
    }/*from   ww  w .  j  a  va  2 s . co m*/

    return false;
}

From source file:com.netflix.spinnaker.front50.model.S3StorageService.java

@Override
public <T extends Timestamped> void storeObject(ObjectType objectType, String objectKey, T item) {
    if (readOnlyMode) {
        throw new ReadOnlyModeException();
    }/*from w  ww  .  jav  a 2s.c o  m*/
    try {
        item.setLastModifiedBy(AuthenticatedRequest.getSpinnakerUser().orElse("anonymous"));
        byte[] bytes = objectMapper.writeValueAsBytes(item);

        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(bytes.length);
        objectMetadata.setContentMD5(
                new String(org.apache.commons.codec.binary.Base64.encodeBase64(DigestUtils.md5(bytes))));

        amazonS3.putObject(bucket, buildS3Key(objectType.group, objectKey, objectType.defaultMetadataFilename),
                new ByteArrayInputStream(bytes), objectMetadata);
        writeLastModified(objectType.group);
    } catch (JsonProcessingException e) {
        throw new IllegalStateException(e);
    }
}

From source file:gribbit.server.siteresources.CacheExtension.java

/**
 * Create a hash URI (which allows the browser to cache this resource indefinitely) if the last modified
 * timestamp has increased, or if there is no hash URI yet for this resource. For a new hash URI to be created,
 * the passed object is scheduled to be hashed by a background thread.
 *
 * This method can be called by any route handler that stores or returns database objects. It should be called
 * both when storing objects and when returning them, since the hash URI cache is held in RAM and is empty when
 * the server starts, so it needs to be built as requests start to come in. IMPORTANT NOTE: If the database can
 * be written to by other database clients, then this method must also be called when those changes are
 * detected, otherwise web clients connected to this web server will continue to serve old linked resources.
 * /*from  w ww  .  j ava 2  s .  co m*/
 * This method should only be used when the total keyspace of URIs that map to database objects easily fits in
 * RAM, and when the objects that need to be hashed are not large (i.e. tens of MB is OK, hundreds of MB is
 * probably not, since there are several background worker threads and they all can be hashing objects in
 * parallel).
 */
public static void updateHashURI(String origURI, ByteBuf content, long lastModifiedEpochSeconds) {
    // Can only hash absolute but local (i.e. domain-less) URIs that have not already been hashed
    if (origURI.startsWith("/") && !origURI.startsWith("//") && !origURI.startsWith("/_/")) {
        // Check to see if there is already a mapping to hash URI for this original URI
        HashInfo hashInfo = origURIToHashInfo.get(origURI);
        if (hashInfo == null || hashInfo.lastModifiedEpochSeconds < lastModifiedEpochSeconds) {
            // There is no hash URI yet for origURI, or there is already a hash URI corresponding to origURI,
            // but the modification time has increased since the cached version, so need to re-hash.
            // Check if another thread has already enqueued the URI for hashing.
            Object alreadyInQueue = scheduledURIsToHash.put(origURI, new Object());
            if (alreadyInQueue == null) {
                content.retain();
                // This URI is not currently queued for hashing by background workers, add it to the queue
                scheduleHasher(origURI, lastModifiedEpochSeconds, new Hasher() {
                    @Override
                    public String computeHashKey() {
                        // Compute MD5 hash of the ByteBuf contents, then base64-encode the results
                        try {
                            String hash = Base64Safe
                                    .base64Encode(DigestUtils.md5(new ByteBufInputStream(content)));
                            content.release(); // TODO: does ByteBufInputStream call release()?
                            return hash;
                        } catch (IOException e) {
                            return null;
                        }
                    }
                });
            }
        }
    }
}

From source file:com.crosstreelabs.cognitio.service.mongo.MongoCatalogueService.java

protected DBObject toMongoObject(final CatalogueEntry entry) {
    if (entry == null) {
        throw new IllegalArgumentException("Entry cannot be null");
    }/*  w  ww.  j  av  a 2 s  . com*/
    if (entry.host == null || StringUtils.isBlank(entry.host.id)) {
        throw new IllegalArgumentException("Cannot persist catalogue entry without host ref");
    }
    BasicDBObject obj = new BasicDBObject(/*"_id", entry.id*/).append("status", entry.status.toString())
            .append("status_reason", StringUtils.defaultIfBlank(entry.status_reason, ""))
            .append("location", entry.location).append("location_hash", DigestUtils.md5(entry.location))
            .append("host", new ObjectId(entry.host.id))
            .append("path", StringUtils.defaultIfBlank(entry.path, ""))
            .append("relocated", entry.relocated.toString())
            .append("new_location", StringUtils.defaultIfBlank(entry.newLocation, ""))
            .append("initial_parent", StringUtils.defaultIfBlank(entry.initialParent, ""))
            .append("initial_depth", entry.initialDepth)
            .append("initial_parent_title", StringUtils.defaultIfBlank(entry.initialParentTitle, ""));
    if (entry.firstSeen != null) {
        obj.append("first_seen", entry.firstSeen.toDate());
    }
    if (entry.lastVisit != null) {
        obj.append("last_visit", entry.lastVisit.toDate());
    }
    return obj;
}

From source file:de.ks.flatadocdb.index.GlobalIndex.java

protected byte[] readMd5(Path path) {
    byte[] md5 = new byte[0];
    try (FileInputStream stream = new FileInputStream(path.toFile())) {
        try (BufferedInputStream buffered = new BufferedInputStream(stream)) {
            md5 = DigestUtils.md5(buffered);

        }/*  w  w w . j  av a2s  .  c o m*/
    } catch (IOException e) {
        log.error("Could not get md5 of {}", path, e);
    }
    return md5;
}

From source file:de.rub.nds.burp.utilities.protocols.SSOProtocol.java

/**
 * Convert SSOProtocol to a String.//w  w  w. j  a va2 s  .co  m
 * @return Token + Protocol + md5(Request)
 */
@Override
public String toString() {
    return token + " " + protocol + " md5(Request)=" + DigestUtils.md5(message.getRequest());
}

From source file:com.archivas.clienttools.arcutils.utils.net.SSLCertChain.java

public String getMD5FingerprintString() {
    X509Certificate cert = getCertificateList().get(0);
    String s = null;/* w w  w .ja va  2s.  c  o  m*/
    try {
        s = byteArrayToColonSeparatedHexString(DigestUtils.md5(cert.getTBSCertificate()), ":");
    } catch (CertificateEncodingException e) {
        LOG.log(Level.WARNING, "Error generating MD5 Fingerprint for SSL Certificate : " + toString());
    }
    return s;
}

From source file:com.netflix.spinnaker.front50.model.S3Support.java

private void writeLastModified() {
    try {//  w  w w . ja  v  a  2s.  c o m
        byte[] bytes = objectMapper
                .writeValueAsBytes(Collections.singletonMap("lastModified", System.currentTimeMillis()));

        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(bytes.length);
        objectMetadata.setContentMD5(
                new String(org.apache.commons.codec.binary.Base64.encodeBase64(DigestUtils.md5(bytes))));

        amazonS3.putObject(bucket, rootFolder + "last-modified.json", new ByteArrayInputStream(bytes),
                objectMetadata);
    } catch (JsonProcessingException e) {
        throw new IllegalStateException(e);
    }
}

From source file:io.stallion.dataAccess.file.FilePersisterBase.java

/**
 * Derives a Long id by hashing the file path and then taking the first 8 bytes
 * of the path.// www. java2  s  . co m
 *
 * This is used if the model object doesn't have a defined id field.
 *
 * @param path
 * @return
 */
public Long makeIdFromFilePath(String path) {
    path = path.toLowerCase();
    path = path.replace(getBucketFolderPath().toLowerCase(), "");
    path = StringUtils.stripStart(path, "/");
    path = getBucket() + "-----" + path;
    // Derive a long id by hashing the file path
    byte[] bs = Arrays.copyOfRange(DigestUtils.md5(path), 0, 6);
    bs = ArrayUtils.addAll(new byte[] { 0, 0 }, bs);
    ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
    buffer.put(bs);
    buffer.flip();//need flip
    Long l = buffer.getLong();
    if (l < 0) {
        l = -l;
    }
    Log.finest("calculated id is {0}", l);
    return l;
}

From source file:com.docd.purefm.test.CommandLineFileTest.java

private String md5sum(final File file) {
    try {/*  www. ja  va  2 s. c om*/
        final InputStream fis = new AutoCloseInputStream(new FileInputStream(file));
        return new String(Hex.encodeHex(DigestUtils.md5(fis)));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}