Example usage for java.security DigestInputStream DigestInputStream

List of usage examples for java.security DigestInputStream DigestInputStream

Introduction

In this page you can find the example usage for java.security DigestInputStream DigestInputStream.

Prototype

public DigestInputStream(InputStream stream, MessageDigest digest) 

Source Link

Document

Creates a digest input stream, using the specified input stream and message digest.

Usage

From source file:stargate.drivers.recipe.sha1fixed.SHA1FixedChunkRecipeGeneratorDriver.java

@Override
public synchronized Recipe getRecipe(DataObjectMetadata metadata, InputStream is) throws IOException {
    if (metadata == null || metadata.isEmpty()) {
        throw new IllegalArgumentException("metadata is null or empty");
    }/*  ww w .ja v a 2  s  .c om*/

    if (is == null) {
        throw new IllegalArgumentException("is is null");
    }

    List<RecipeChunk> chunk = new ArrayList<RecipeChunk>();

    int bufferSize = Math.min(this.chunkSize, BUFFER_SIZE);
    byte[] buffer = new byte[bufferSize];

    try {
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
        DigestInputStream dis = new DigestInputStream(is, messageDigest);

        long chunkOffset = 0;

        while (chunkOffset < metadata.getObjectSize()) {
            int chunkLength = 0;
            int nread = 0;
            int toread = this.chunkSize;

            while ((nread = dis.read(buffer, 0, Math.min(toread, bufferSize))) > 0) {
                chunkLength += nread;
                toread -= nread;
                if (toread == 0) {
                    break;
                }
            }

            byte[] digest = messageDigest.digest();
            chunk.add(new RecipeChunk(chunkOffset, chunkLength, digest));
            messageDigest.reset();

            if (nread <= 0) {
                //EOF
                break;
            }

            chunkOffset += chunkLength;
        }

        dis.close();
    } catch (NoSuchAlgorithmException ex) {
        throw new IOException(ex);
    }

    return new Recipe(metadata, HASH_ALGORITHM, this.chunkSize, chunk);
}

From source file:com.qut.middleware.metadata.source.impl.MetadataSourceBase.java

/**
 * Reads the provided input stream, calculates and updates an internal hash.
 * If the internal hash has changed, the byte array obtained from reading the
 * InputStream is passed to the processMetadata method, along with the
 * provided MetadataProcessor object./*from  w w  w. ja va2s  . c o m*/
 * @param input Input stream to send 
 * @param processor
 * @throws IOException
 */
protected void readMetadata(InputStream input, MetadataProcessor processor) throws IOException {
    byte[] buf = new byte[BUFFER_LENGTH];
    long startTime = System.currentTimeMillis();

    // Pipe everything through a digest stream so we get a hash value at the end
    DigestInputStream digestInput = new DigestInputStream(input, this.getMessageDigestInstance());
    BufferedInputStream bufferedInput = new BufferedInputStream(digestInput);

    ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();

    this.logger.debug("Metadata source {} - going to read input stream", this.getLocation());
    int bytes = 0;
    while ((bytes = bufferedInput.read(buf)) != -1) {
        byteOutput.write(buf, 0, bytes);
    }

    bufferedInput.close();
    digestInput.close();
    byteOutput.close();

    long endTime = System.currentTimeMillis();

    byte[] document = byteOutput.toByteArray();
    byte[] hash = digestInput.getMessageDigest().digest();

    this.logger.debug("Metadata source {} - read {} bytes of metadata in {} ms",
            new Object[] { this.getLocation(), document.length, (endTime - startTime) });

    // If the document has changed, the hash will be updated, and then we go to process the new document
    if (this.updateDigest(hash)) {
        startTime = System.currentTimeMillis();
        this.logger.debug("Metadata source {} - updated. Going to process.", this.getLocation());
        this.processMetadata(document, processor);
        endTime = System.currentTimeMillis();
        this.logger.info("Metadata source {} - processed document and updated cache in {} ms",
                this.getLocation(), (endTime - startTime));
    } else {
        this.logger.info("Metadata source {} - has not been updated.", this.getLocation());
    }
}

From source file:hudson.plugins.ec2.EC2PrivateKey.java

static String digestOpt(Key k, String dg) throws IOException {
    try {/*from w  ww .  j a va2s.co  m*/
        MessageDigest md5 = MessageDigest.getInstance(dg);

        DigestInputStream in = new DigestInputStream(new ByteArrayInputStream(k.getEncoded()), md5);
        try {
            while (in.read(new byte[128]) > 0)
                ; // simply discard the input
        } finally {
            in.close();
        }
        StringBuilder buf = new StringBuilder();
        char[] hex = Hex.encodeHex(md5.digest());
        for (int i = 0; i < hex.length; i += 2) {
            if (buf.length() > 0)
                buf.append(':');
            buf.append(hex, i, 2);
        }
        return buf.toString();
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError(e);
    }
}

From source file:com.emc.ecs.sync.model.object.AbstractSyncObject.java

@Override
public final synchronized InputStream getInputStream() {
    try {/*from   ww  w  .jav  a2s  .  co  m*/
        if (cin == null) {
            InputStream in = din = new DigestInputStream(createSourceInputStream(),
                    MessageDigest.getInstance("MD5"));
            if (parentPlugin != null && parentPlugin.isMonitorPerformance())
                in = new ProgressInputStream(din,
                        new TransferProgressListener(parentPlugin.getReadPerformanceCounter()));
            cin = new CountingInputStream(in);
        }
        return cin;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("No MD5 digest?!");
    }
}

From source file:org.unitils.dbmaintainer.script.impl.DefaultScriptSourceTest.java

private String getCheckSum(String fileName) throws NoSuchAlgorithmException, IOException {
    MessageDigest digest = MessageDigest.getInstance("MD5");
    InputStream is = new DigestInputStream(new FileInputStream(scriptsDirName + "/test_scripts/" + fileName),
            digest);// w w  w. ja va 2 s  .  c  o m

    while (is.read() != -1)
        ;
    return getHexPresentation(digest.digest());
}

From source file:org.dita.dost.ant.PluginInstallTask.java

private String getFileHash(final File file) {
    try (DigestInputStream digestInputStream = new DigestInputStream(
            new BufferedInputStream(new FileInputStream(file)), MessageDigest.getInstance("SHA-256"))) {
        IOUtils.copy(digestInputStream, new NullOutputStream());
        final MessageDigest digest = digestInputStream.getMessageDigest();
        final byte[] sha256 = digest.digest();
        return printHexBinary(sha256);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalArgumentException(e);
    } catch (IOException e) {
        throw new BuildException("Failed to calculate file checksum: " + e.getMessage(), e);
    }//from w ww  . java2s . c  om
}

From source file:com.microsoftopentechnologies.windowsazurestorage.service.UploadToBlobService.java

/**
 * @param blob/*from  ww  w . ja  v  a  2 s.  c  om*/
 * @param src
 * @throws StorageException
 * @throws IOException
 * @throws InterruptedException
 * @returns Md5 hash of the uploaded file in hexadecimal encoding
 */
private String uploadBlob(CloudBlockBlob blob, FilePath src)
        throws StorageException, IOException, InterruptedException {
    final MessageDigest md = DigestUtils.getMd5Digest();
    long startTime = System.currentTimeMillis();
    try (InputStream inputStream = src.read();
            DigestInputStream digestInputStream = new DigestInputStream(inputStream, md)) {
        blob.upload(digestInputStream, src.length(), null, getBlobRequestOptions(), Utils.updateUserAgent());
    }
    long endTime = System.currentTimeMillis();

    println("Uploaded to file storage with uri " + blob.getUri() + " in " + getTime(endTime - startTime));
    return DatatypeConverter.printHexBinary(md.digest());
}

From source file:de.tudarmstadt.ukp.dkpro.core.api.datasets.DatasetLoader.java

private void fetch(File aTarget, DataPackage... aPackages) throws IOException {
    // First validate if local copies are still up-to-date
    boolean reload = false;
    packageValidationLoop: for (DataPackage pack : aPackages) {
        File cachedFile = new File(aTarget, pack.getTarget());
        if (!cachedFile.exists()) {
            continue;
        }//w  w w  .j  av a 2 s .co  m

        if (pack.getSha1() != null) {
            String actual = getDigest(cachedFile, "SHA1");
            if (!pack.getSha1().equals(actual)) {
                LOG.info("Local SHA1 hash mismatch on [" + cachedFile + "] - expected [" + pack.getSha1()
                        + "] - actual [" + actual + "]");
                reload = true;
                break packageValidationLoop;
            } else {
                LOG.info("Local SHA1 hash verified on [" + cachedFile + "] - [" + actual + "]");
            }
        }

        if (pack.getMd5() != null) {
            String actual = getDigest(cachedFile, "MD5");
            if (!pack.getMd5().equals(actual)) {
                LOG.info("Local MD5 hash mismatch on [" + cachedFile + "] - expected [" + pack.getMd5()
                        + "] - actual [" + actual + "]");
                reload = true;
                break packageValidationLoop;
            } else {
                LOG.info("Local MD5 hash verified on [" + cachedFile + "] - [" + actual + "]");
            }
        }

    }

    // If any of the packages are outdated, clear the cache and download again
    if (reload) {
        LOG.info("Clearing local cache for [" + aTarget + "]");
        FileUtils.deleteQuietly(aTarget);
    }

    for (DataPackage pack : aPackages) {
        File cachedFile = new File(aTarget, pack.getTarget());

        if (cachedFile.exists()) {
            continue;
        }

        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new IOException(e);
        }

        MessageDigest sha1;
        try {
            sha1 = MessageDigest.getInstance("SHA1");
        } catch (NoSuchAlgorithmException e) {
            throw new IOException(e);
        }

        cachedFile.getParentFile().mkdirs();
        URL source = new URL(pack.getUrl());

        LOG.info("Fetching [" + cachedFile + "]");

        URLConnection connection = source.openConnection();
        connection.setRequestProperty("User-Agent", "Java");

        try (InputStream is = connection.getInputStream()) {
            DigestInputStream md5Filter = new DigestInputStream(is, md5);
            DigestInputStream sha1Filter = new DigestInputStream(md5Filter, sha1);
            FileUtils.copyInputStreamToFile(sha1Filter, cachedFile);

            if (pack.getMd5() != null) {
                String md5Hex = new String(Hex.encodeHex(md5Filter.getMessageDigest().digest()));
                if (!pack.getMd5().equals(md5Hex)) {
                    String message = "MD5 mismatch. Expected [" + pack.getMd5() + "] but got [" + md5Hex + "].";
                    LOG.error(message);
                    throw new IOException(message);
                }
            }

            if (pack.getSha1() != null) {
                String sha1Hex = new String(Hex.encodeHex(sha1Filter.getMessageDigest().digest()));
                if (!pack.getSha1().equals(sha1Hex)) {
                    String message = "SHA1 mismatch. Expected [" + pack.getSha1() + "] but got [" + sha1Hex
                            + "].";
                    LOG.error(message);
                    throw new IOException(message);
                }
            }
        }
    }

    // Perform a post-fetch action such as unpacking
    for (DataPackage pack : aPackages) {
        File cachedFile = new File(aTarget, pack.getTarget());
        File postActionCompleteMarker = new File(cachedFile.getPath() + ".postComplete");
        if (pack.getPostAction() != null && !postActionCompleteMarker.exists()) {
            try {
                pack.getPostAction().run(pack);
                FileUtils.touch(postActionCompleteMarker);
            } catch (IOException e) {
                throw e;
            } catch (Exception e) {
                throw new IllegalStateException(e);
            }
        }
    }
}

From source file:com.docdoku.cli.helpers.FileHelper.java

public String uploadFile(File pLocalFile, String pURL)
        throws IOException, LoginException, NoSuchAlgorithmException {
    InputStream in = null;// w w  w .j  a  v  a  2 s . c  o  m
    OutputStream out = null;
    HttpURLConnection conn = null;
    try {
        //Hack for NTLM proxy
        //perform a head method to negociate the NTLM proxy authentication
        URL url = new URL(pURL);
        System.out.println("Uploading file: " + pLocalFile.getName() + " to " + url.getHost());
        performHeadHTTPMethod(url);

        conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setAllowUserInteraction(true);
        conn.setRequestProperty("Connection", "Keep-Alive");
        byte[] encoded = Base64.encodeBase64((login + ":" + password).getBytes("ISO-8859-1"));
        conn.setRequestProperty("Authorization", "Basic " + new String(encoded, "US-ASCII"));

        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "--------------------" + Long.toString(System.currentTimeMillis(), 16);
        byte[] header = (twoHyphens + boundary + lineEnd + "Content-Disposition: form-data; name=\"upload\";"
                + " filename=\"" + pLocalFile + "\"" + lineEnd + lineEnd).getBytes("ISO-8859-1");
        byte[] footer = (lineEnd + twoHyphens + boundary + twoHyphens + lineEnd).getBytes("ISO-8859-1");

        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
        //conn.setRequestProperty("Content-Length",len + "");
        long len = header.length + pLocalFile.length() + footer.length;
        conn.setFixedLengthStreamingMode((int) len);
        out = new BufferedOutputStream(conn.getOutputStream(), BUFFER_CAPACITY);
        out.write(header);

        byte[] data = new byte[CHUNK_SIZE];
        int length;
        MessageDigest md = MessageDigest.getInstance("MD5");
        in = new ConsoleProgressMonitorInputStream(pLocalFile.length(), new DigestInputStream(
                new BufferedInputStream(new FileInputStream(pLocalFile), BUFFER_CAPACITY), md));
        while ((length = in.read(data)) != -1) {
            out.write(data, 0, length);
        }

        out.write(footer);
        out.flush();

        manageHTTPCode(conn);

        byte[] digest = md.digest();
        return Base64.encodeBase64String(digest);
    } finally {
        if (out != null)
            out.close();
        if (in != null)
            in.close();
        if (conn != null)
            conn.disconnect();
    }
}

From source file:com.clustercontrol.repository.factory.AgentLibDownloader.java

/**
 * MD5??//from ww w .j a  v a 2 s . com
 * @param filepath
 * @return
 * @throws HinemosUnknown 
 */
private static String getMD5(String filepath) throws HinemosUnknown {
    MessageDigest md = null;
    DigestInputStream inStream = null;
    byte[] digest = null;
    try {
        md = MessageDigest.getInstance("MD5");
        inStream = new DigestInputStream(new BufferedInputStream(new FileInputStream(filepath)), md);
        while (inStream.read() != -1) {
        }
        digest = md.digest();
    } catch (Exception e) {
        m_log.warn("getMD5() : filepath=" + filepath + ", " + e.getClass(), e);
    } finally {
        if (inStream != null) {
            try {
                inStream.close();
            } catch (Exception e) {
                m_log.warn("getMD5() : close " + e.getClass(), e);
            }
        }
        if (digest == null)
            throw new HinemosUnknown("MD5 digest is null");
    }
    return hashByte2MD5(digest);
}