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:fi.mikuz.boarder.util.FileProcessor.java

public static String calculateHash(File file) throws Exception {

    MessageDigest algorithm = MessageDigest.getInstance("SHA1");
    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis);
    DigestInputStream dis = new DigestInputStream(bis, algorithm);

    // read the file and update the hash calculation
    while (dis.read() != -1)
        ;/*from w  ww.ja v  a2 s . c  om*/

    // get the hash value as byte array
    byte[] hash = algorithm.digest();

    return byteArray2Hex(hash);
}

From source file:com.opentable.db.postgres.embedded.EmbeddedPostgres.java

private static File prepareBinaries(PgBinaryResolver pgBinaryResolver) {
    PREPARE_BINARIES_LOCK.lock();//from  w  w w.j av  a 2 s.  c om
    try {
        if (BINARY_DIR.get() != null) {
            return BINARY_DIR.get();
        }

        final String system = getOS();
        final String machineHardware = getArchitecture();

        LOG.info("Detected a {} {} system", system, machineHardware);
        File pgDir;
        File pgTbz;
        final InputStream pgBinary;
        try {
            pgTbz = File.createTempFile("pgpg", "pgpg");
            pgBinary = pgBinaryResolver.getPgBinary(system, machineHardware);
        } catch (final IOException e) {
            throw new ExceptionInInitializerError(e);
        }

        if (pgBinary == null) {
            throw new IllegalStateException("No Postgres binary found for " + system + " / " + machineHardware);
        }

        try (final DigestInputStream pgArchiveData = new DigestInputStream(pgBinary,
                MessageDigest.getInstance("MD5")); final FileOutputStream os = new FileOutputStream(pgTbz)) {
            IOUtils.copy(pgArchiveData, os);
            pgArchiveData.close();
            os.close();

            String pgDigest = Hex.encodeHexString(pgArchiveData.getMessageDigest().digest());

            pgDir = new File(TMP_DIR, String.format("PG-%s", pgDigest));

            mkdirs(pgDir);
            final File unpackLockFile = new File(pgDir, LOCK_FILE_NAME);
            final File pgDirExists = new File(pgDir, ".exists");

            if (!pgDirExists.exists()) {
                try (final FileOutputStream lockStream = new FileOutputStream(unpackLockFile);
                        final FileLock unpackLock = lockStream.getChannel().tryLock()) {
                    if (unpackLock != null) {
                        try {
                            Preconditions.checkState(!pgDirExists.exists(),
                                    "unpack lock acquired but .exists file is present.");
                            LOG.info("Extracting Postgres...");
                            extractTxz(pgTbz.getPath(), pgDir.getPath());
                            Verify.verify(pgDirExists.createNewFile(), "couldn't make .exists file");
                        } catch (Exception e) {
                            LOG.error("while unpacking Postgres", e);
                        }
                    } else {
                        // the other guy is unpacking for us.
                        int maxAttempts = 60;
                        while (!pgDirExists.exists() && --maxAttempts > 0) {
                            Thread.sleep(1000L);
                        }
                        Verify.verify(pgDirExists.exists(),
                                "Waited 60 seconds for postgres to be unpacked but it never finished!");
                    }
                } finally {
                    if (unpackLockFile.exists()) {
                        Verify.verify(unpackLockFile.delete(), "could not remove lock file %s",
                                unpackLockFile.getAbsolutePath());
                    }
                }
            }
        } catch (final IOException | NoSuchAlgorithmException e) {
            throw new ExceptionInInitializerError(e);
        } catch (final InterruptedException ie) {
            Thread.currentThread().interrupt();
            throw new ExceptionInInitializerError(ie);
        } finally {
            Verify.verify(pgTbz.delete(), "could not delete %s", pgTbz);
        }
        BINARY_DIR.set(pgDir);
        LOG.info("Postgres binaries at {}", pgDir);
        return pgDir;
    } finally {
        PREPARE_BINARIES_LOCK.unlock();
    }
}

From source file:org.deventropy.shared.utils.DirectoryArchiverUtilTest.java

private byte[] getMd5Digest(final InputStream inputStream, final boolean closeStream) throws IOException {
    try {//from  ww  w.  j  a va  2s  .  c  om
        final MessageDigest md = MessageDigest.getInstance("MD5");
        final DigestInputStream dis = new DigestInputStream(inputStream, md);

        final byte[] buf = new byte[MAX_FILE_SIZE];
        int len = 0;
        while (len != -1) {
            len = dis.read(buf);
        }
        return md.digest();
    } catch (NoSuchAlgorithmException e) {
        throw new IOException(e);
    } finally {
        if (closeStream && null != inputStream) {
            inputStream.close();
        }
    }
}

From source file:org.opencastproject.workingfilerepository.impl.WorkingFileRepositoryImpl.java

/**
 * {@inheritDoc}//  w w w  .j a  va 2  s  .c  om
 * 
 * @throws IOException
 *           if the hash can't be created
 * 
 * @see org.opencastproject.workingfilerepository.api.WorkingFileRepository#putInCollection(java.lang.String,
 *      java.lang.String, java.io.InputStream)
 */
@Override
public URI putInCollection(String collectionId, String fileName, InputStream in) throws IOException {
    checkPathSafe(collectionId);
    checkPathSafe(fileName);
    File f = new File(PathSupport.concat(new String[] { rootDirectory, COLLECTION_PATH_PREFIX, collectionId,
            PathSupport.toSafeName(fileName) }));
    logger.debug("Attempting to write a file to {}", f.getAbsolutePath());
    FileOutputStream out = null;
    try {
        if (!f.exists()) {
            logger.debug("Attempting to create a new file at {}", f.getAbsolutePath());
            File collectionDirectory = getCollectionDirectory(collectionId, true);
            if (!collectionDirectory.exists()) {
                logger.debug("Attempting to create a new directory at {}",
                        collectionDirectory.getAbsolutePath());
                FileUtils.forceMkdir(collectionDirectory);
            }
            f.createNewFile();
        } else {
            logger.debug("Attempting to overwrite the file at {}", f.getAbsolutePath());
        }
        out = new FileOutputStream(f);

        // Wrap the input stream and copy the input stream to the file
        MessageDigest messageDigest = null;
        DigestInputStream dis = null;
        try {
            messageDigest = MessageDigest.getInstance("MD5");
            dis = new DigestInputStream(in, messageDigest);
            IOUtils.copy(dis, out);
        } catch (NoSuchAlgorithmException e1) {
            logger.error("Unable to create md5 message digest");
        }

        // Store the hash
        String md5 = Checksum.convertToHex(dis.getMessageDigest().digest());
        File md5File = null;
        try {
            md5File = getMd5File(f);
            FileUtils.writeStringToFile(md5File, md5);
        } catch (IOException e) {
            FileUtils.deleteQuietly(md5File);
            throw e;
        } finally {
            IOUtils.closeQuietly(dis);
        }

    } catch (IOException e) {
        FileUtils.deleteQuietly(f);
        throw e;
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(in);
    }
    return getCollectionURI(collectionId, fileName);
}

From source file:ch.cyberduck.core.s3.S3Path.java

/**
 * @param throttle Bandwidth throttle//  w w w.j  a va 2  s.  co m
 * @param listener Callback for bytes sent
 * @param status   Transfer status
 * @param object   File location
 * @throws IOException      I/O error
 * @throws ServiceException Service error
 */
private void uploadSingle(final BandwidthThrottle throttle, final StreamListener listener,
        final TransferStatus status, final StorageObject object) throws IOException, ServiceException {

    InputStream in = null;
    ResponseOutputStream<StorageObject> out = null;
    MessageDigest digest = null;
    if (!Preferences.instance().getBoolean("s3.upload.metadata.md5")) {
        // Content-MD5 not set. Need to verify ourselves instad of S3
        try {
            digest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            log.error(e.getMessage());
        }
    }
    try {
        if (null == digest) {
            log.warn("MD5 calculation disabled");
            in = this.getLocal().getInputStream();
        } else {
            in = new DigestInputStream(this.getLocal().getInputStream(), digest);
        }
        out = this.write(object, status.getLength() - status.getCurrent(),
                Collections.<String, String>emptyMap());
        this.upload(out, in, throttle, listener, status);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
    if (null != digest) {
        final StorageObject part = out.getResponse();
        this.getSession().message(MessageFormat
                .format(Locale.localizedString("Compute MD5 hash of {0}", "Status"), this.getName()));
        // Obtain locally-calculated MD5 hash.
        String hexMD5 = ServiceUtils.toHex(digest.digest());
        this.getSession().getClient().verifyExpectedAndActualETagValues(hexMD5, part);
    }
}

From source file:uk.ac.ebi.jmzml.xml.io.MzMLUnmarshaller.java

/**
 * Calcultes the check sum.// w  ww.  j  a v a2s  .co m
 *
 * @return the check sum as hexidecimal
 */
private String calculateChecksum() {
    // we have to create the checksum for the mzML file (from its beginning to the
    // end of the fileChecksum start tag).
    // Since this stop location is very near the end of the file, we skip everything
    // until we come within a certain limit of the end of the file
    long limit = mzMLFile.length() - 200L;
    logger.debug("Looking for fileChecksum tag between byte " + limit + " and byte " + mzMLFile.length()
            + " (the end) of the mzML file.");

    // initialize the hash algorithm
    MessageDigest hash;
    try {
        hash = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("SHA-1 not recognized as Secure Hash Algorithm.", e);
    }

    // create the input stream that will calculate the checksum
    FileInputStream fis;
    try {
        fis = new FileInputStream(mzMLFile);
    } catch (FileNotFoundException e) {
        throw new IllegalStateException("File " + mzMLFile.getAbsoluteFile() + " could not be found!", e);
    }
    BufferedInputStream bis = new BufferedInputStream(fis);
    DigestInputStream dis = new DigestInputStream(bis, hash);

    // prepare for input stream processing
    // we read through the file until we reach a specified limit before the end of the file
    // from there we populate a buffer with the read bytes (characters) and check if we have
    // already reached the position up to where we have to calculate the hash.
    CircularFifoBuffer bBuf = new CircularFifoBuffer(15);
    long cnt = 0; // counter to keep track of our position
    byte[] b = new byte[1]; // we only read one byte at a time
    try {
        while (dis.read(b) >= 0) {
            bBuf.add(b[0]);
            cnt++;
            // check if we have already reached the last bit of the file, where we have
            // to find the right position to stop (after the 'fileChecksum' start tag)
            if (cnt > limit) {
                // we should have reached the start of the <fileChecksum> tag,
                // now we have to find the end
                String readBuffer = convert2String(bBuf);
                if (readBuffer.endsWith("<fileChecksum>")) {
                    // we have found the end of the fileChecksum start tag, we have to stop the hash
                    if (b[0] != '>') { // check that we are really at the last character of the tag
                        throw new IllegalStateException("We are not at the end of <fileChecksum> tag!");
                    }
                    break;
                }
            } // else if not yet near the end of the file, just keep on going
        }
        dis.close();
    } catch (IOException e) {
        throw new IllegalStateException(
                "Could not read from file '" + mzMLFile.getAbsolutePath() + "' while trying ot calculate hash.",
                e);
    }
    logger.debug("Read over " + cnt + " bytes while calculating the file hash.");

    byte[] bytesDigest = dis.getMessageDigest().digest();

    return asHex(bytesDigest);
}

From source file:org.mule.module.s3.automation.testcases.CreateObjectTestCases.java

@Ignore
@Category({ RegressionTests.class })
@Test/*from w  ww .j  a  v  a 2 s. co m*/
public void testCreateInputStreamObjectOptionalAttributes() {

    InputStream inputStream = null;

    testObjects.putAll((HashMap<String, Object>) context.getBean("createInputStreamObjectTestData"));

    String host = testObjects.get("host").toString();
    String path = testObjects.get("path").toString();
    String urlString = String.format("http://%s/%s", host, path);

    try {

        URL url = new URL(urlString);
        URLConnection connection = url.openConnection();
        inputStream = connection.getInputStream();

        testObjects.put("contentRef", inputStream);

        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        DigestInputStream digestInputStream = new DigestInputStream(inputStream, messageDigest);

        byte[] encodedByteData = Base64.encodeBase64(digestInputStream.getMessageDigest().digest());

        testObjects.put("contentMd5", new String(encodedByteData, "UTF-8"));
        testObjects.put("contentLength", Long.valueOf(IOUtils.toByteArray(inputStream).length));

        MessageProcessor createObjectFlow = lookupMessageProcessor("create-object-optional-attributes");
        MuleEvent response = createObjectFlow.process(getTestEvent(testObjects));

        assertEquals("{NullPayload}", response.getMessage().getPayload().toString());

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        fail();
    } finally {
        if (inputStream != null)
            try {
                inputStream.close();
            } catch (IOException logOrIgnore) {
            }
    }

}

From source file:org.zanata.client.commands.push.RawPushCommand.java

private String calculateFileHash(File srcFile) {
    try {//from  w  w  w .  j a va2 s.  c o  m
        MessageDigest md = MessageDigest.getInstance("MD5");
        InputStream fileStream = new FileInputStream(srcFile);
        try {
            fileStream = new DigestInputStream(fileStream, md);
            byte[] buffer = new byte[256];
            //noinspection StatementWithEmptyBody
            while (fileStream.read(buffer) > 0) {
                // just keep digesting the input
            }
        } finally {
            fileStream.close();
        }
        //noinspection UnnecessaryLocalVariable
        String md5hash = new String(Hex.encodeHex(md.digest()));
        return md5hash;
    } catch (NoSuchAlgorithmException | IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.emc.ecs.smart.SmartUploader.java

/**
 * Does a standard PUT upload using HttpURLConnection.
 *//* ww  w .  ja va  2  s  .  co m*/
private void doSimpleUpload() {
    try {
        fileSize = Files.size(fileToUpload);
        HttpURLConnection conn = null;
        long start = System.currentTimeMillis();
        try (DigestInputStream is = new DigestInputStream(Files.newInputStream(fileToUpload),
                MessageDigest.getInstance("MD5"))) {
            conn = (HttpURLConnection) uploadUrl.openConnection();
            conn.setFixedLengthStreamingMode(fileSize);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setInstanceFollowRedirects(false);
            conn.setUseCaches(false);
            conn.setRequestMethod(HttpMethod.PUT);
            conn.setRequestProperty(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM);
            OutputStream os = conn.getOutputStream();
            byte[] buf = new byte[CHUNK_SIZE];
            int len;

            while ((len = is.read(buf)) != -1) {
                os.write(buf, 0, len);
                bytesUploaded += len;
                printPercent();
            }
            os.close();

            if (conn.getResponseCode() != ClientResponse.Status.OK.getStatusCode()) {
                throw new RuntimeException("Unable to upload object content: status=" + conn.getResponseCode());
            } else {
                List<String> eTags = conn.getHeaderFields().get(HttpHeaders.ETAG);
                if (eTags == null || eTags.size() < 1) {
                    throw new RuntimeException("Unable to get ETag for uploaded data");
                } else {
                    byte[] sentMD5 = is.getMessageDigest().digest();
                    String eTag = eTags.get(0);
                    byte[] gotMD5 = DatatypeConverter.parseHexBinary(eTag.substring(1, eTag.length() - 1));
                    if (!Arrays.equals(gotMD5, sentMD5)) {
                        throw new RuntimeException("ETag doesn't match streamed data's MD5.");
                    }
                }
            }
        } catch (IOException e) {
            throw new Exception("IOException while uploading object content after writing " + bytesUploaded
                    + " of " + fileSize + " bytes: " + e.getMessage());
        } finally {
            if (conn != null)
                conn.disconnect();
        }

        long elapsed = System.currentTimeMillis() - start;
        printRate(fileSize, elapsed);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.github.nlloyd.hornofmongo.MongoScope.java

public static Object md5sumFile(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
    assertSingleArgument(args);/*  w ww  .j a v  a2 s  .  c  o m*/
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        Context.throwAsScriptRuntimeEx(e);
    }
    File inFile;
    try {
        inFile = resolveFilePath((MongoScope) thisObj, Context.toString(args[0])).getCanonicalFile();
    } catch (IOException e) {
        Context.throwAsScriptRuntimeEx(e);
        return null;
    }
    InputStream in = null;
    DigestInputStream dis = null;
    try {
        in = new BufferedInputStream(new FileInputStream(inFile));
        dis = new DigestInputStream(in, md);
        while (dis.available() > 0)
            dis.read();
        byte[] digest = md.digest();
        String hexStr = Hex.encodeHexString(digest);
        return hexStr;
    } catch (FileNotFoundException e) {
        Context.throwAsScriptRuntimeEx(e);
    } catch (IOException e) {
        Context.throwAsScriptRuntimeEx(e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
        if (dis != null) {
            try {
                dis.close();
            } catch (IOException e) {
            }
        }
    }
    return Undefined.instance;
}