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

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

Introduction

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

Prototype

public static MessageDigest updateDigest(MessageDigest messageDigest, String valueToDigest) 

Source Link

Usage

From source file:de.fosd.jdime.artifact.Artifact.java

/**
 * Returns a hash of the tree rooted in this {@code Artifact}.
 *
 * @return the tree hash//from  w  ww  .jav a  2 s  .c o m
 */
public String getTreeHash() {

    if (hashValid) {
        return hash;
    }

    MessageDigest digest = DigestUtils.getSha256Digest();
    DigestUtils.updateDigest(digest, hashId());

    if (hasChildren()) {
        children.forEach(c -> DigestUtils.updateDigest(digest, c.getTreeHash()));
        hash = "1" + Hex.encodeHexString(digest.digest());
    } else {
        hash = "0" + Hex.encodeHexString(digest.digest());
    }

    hashValid = true;
    return hash;
}

From source file:org.apache.taverna.download.impl.DownloadManagerImpl.java

@Override
public void download(URI source, Path destination, String digestAlgorithm, URI digestSource)
        throws DownloadException {

    MessageDigest md = null;/* w w w  .j a va 2 s  .  co  m*/
    if (digestAlgorithm != null) {
        try {
            md = MessageDigest.getInstance(digestAlgorithm);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException("Unsupported digestAlgorithm: " + digestAlgorithm, e);
        }
    }

    // download the file
    Path tempFile;
    try {
        tempFile = Files.createTempFile(destination.getParent(), "." + destination.getFileName(), ".tmp");
    } catch (IOException e1) {
        // perhaps a permission problem?
        throw new DownloadException("Can't create temporary file in folder " + destination.getParent(), e1);
    }
    logger.info(String.format("Downloading %1$s to %2$s", source, tempFile));
    downloadToFile(source, tempFile);

    if (digestSource != null) {
        // download the digest file
        String expectedDigest;
        expectedDigest = downloadHash(digestSource).trim().toLowerCase(Locale.ROOT);
        // check if the digest matches
        try {
            try (InputStream s = Files.newInputStream(tempFile)) {
                DigestUtils.updateDigest(md, s);
                String actualDigest = Hex.encodeHexString(md.digest());
                if (!actualDigest.equals(expectedDigest)) {
                    throw new DownloadException(
                            String.format("Error downloading file: checksum mismatch (%1$s != %2$s)",
                                    actualDigest, expectedDigest));
                }
            }
        } catch (IOException e) {
            throw new DownloadException(String.format("Error checking digest for %1$s", destination), e);
        }
    }
    // All fine, move to destination
    try {
        logger.info(String.format("Copying %1$s to %2$s", tempFile, destination));
        Files.move(tempFile, destination, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
        throw new DownloadException(String.format("Error downloading %1$s to %2$s.", source, destination), e);
    }

}

From source file:org.nuxeo.ecm.platform.filemanager.core.listener.DigestComputer.java

private String computeDigest(Blob blob) throws IOException {

    MessageDigest md;//from  w  w  w.j a v  a  2  s.  c om
    try {
        md = MessageDigest.getInstance(digestAlgo);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    byte[] b = DigestUtils.updateDigest(md, blob.getStream()).digest();
    return Base64.encodeBytes(b);
}