List of usage examples for java.security DigestInputStream DigestInputStream
public DigestInputStream(InputStream stream, MessageDigest digest)
From source file:edu.mit.lib.bagit.Loader.java
/** * Resolves a payload reference with passed stream content. * // ww w . ja va 2s . c o m * @param relPath * the bag-relative path to the payload file * @param is * the content input stream for payload * @throws IOException */ public void resolveRef(String relPath, InputStream is) throws IOException { // various checks - is ref known? if (!payloadRefMap.containsKey(relPath)) { throw new IOException("Unknown payload reference: " + relPath); } if (bagFile(relPath).exists()) { throw new IllegalStateException("Payload file already exists at: " + relPath); } // wrap stream in digest stream try (DigestInputStream dis = new DigestInputStream(is, MessageDigest.getInstance(csAlgorithm()))) { Files.copy(dis, bagFile(relPath).toPath()); // record checksum manifestWriter().writeLine(toHex(dis.getMessageDigest().digest()) + " " + relPath); // remove from map payloadRefMap.remove(relPath); } catch (NoSuchAlgorithmException nsaE) { throw new IOException("no algorithm: " + csAlg); } }
From source file:io.amient.yarn1.YarnClient.java
/** * Distribute all dependencies in a single jar both from Client to Master as well as Master to Container(s) *//*from w w w. j a va 2s . com*/ public static void distributeResources(Configuration yarnConf, Properties appConf, String appName) throws IOException { final FileSystem distFs = FileSystem.get(yarnConf); final FileSystem localFs = FileSystem.getLocal(yarnConf); try { //distribute configuration final Path dstConfig = new Path(distFs.getHomeDirectory(), appName + ".configuration"); final FSDataOutputStream fs = distFs.create(dstConfig); appConf.store(fs, "Yarn1 Application Config for " + appName); fs.close(); log.info("Updated resource " + dstConfig); //distribute main jar final String localPath = YarnClient.class.getProtectionDomain().getCodeSource().getLocation().getFile() .replace(".jar/", ".jar"); final Path src; final String jarName = appName + ".jar"; if (localPath.endsWith(".jar")) { log.info("Distributing local jar : " + localPath); src = new Path(localPath); } else { try { String localArchive = localPath + appName + ".jar"; localFs.delete(new Path(localArchive), false); log.info("Unpacking compile scope dependencies: " + localPath); executeShell("mvn -f " + localPath + "/../.. generate-resources"); log.info("Preparing application main jar " + localArchive); executeShell("jar cMf " + localArchive + " -C " + localPath + " ./"); src = new Path(localArchive); } catch (InterruptedException e) { throw new IOException(e); } } byte[] digest; final MessageDigest md = MessageDigest.getInstance("MD5"); try (InputStream is = new FileInputStream(src.toString())) { DigestInputStream dis = new DigestInputStream(is, md); byte[] buffer = new byte[8192]; int numOfBytesRead; while ((numOfBytesRead = dis.read(buffer)) > 0) { md.update(buffer, 0, numOfBytesRead); } digest = md.digest(); } log.info("Local check sum: " + Hex.encodeHexString(digest)); final Path dst = new Path(distFs.getHomeDirectory(), jarName); Path remoteChecksumFile = new Path(distFs.getHomeDirectory(), jarName + ".md5"); boolean checksumMatches = false; if (distFs.isFile(remoteChecksumFile)) { try (InputStream r = distFs.open(remoteChecksumFile)) { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int nRead; byte[] data = new byte[1024]; while ((nRead = r.read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); } buffer.flush(); byte[] remoteDigest = buffer.toByteArray(); log.info("Remote check sum: " + Hex.encodeHexString(remoteDigest)); checksumMatches = Arrays.equals(digest, remoteDigest); } } if (!checksumMatches) { log.info("Updating resource " + dst + " ..."); distFs.copyFromLocalFile(false, true, src, dst); try (FSDataOutputStream remoteChecksumStream = distFs.create(remoteChecksumFile)) { log.info("Updating checksum " + remoteChecksumFile + " ..."); remoteChecksumStream.write(digest); } FileStatus scFileStatus = distFs.getFileStatus(dst); log.info("Updated resource " + dst + " " + scFileStatus.getLen()); } } catch (NoSuchAlgorithmException e) { throw new IOException(e); } }
From source file:com.example.download.DownloadThread.java
/** * Fully execute a single download request - setup and send the request, handle the response, * and transfer the data to the destination file. *///w ww . j av a2 s .c o m private void executeDownload(State state, AndroidHttpClient client, HttpGet request) throws StopRequest, RetryDownload { InnerState innerState = new InnerState(); byte data[] = new byte[Constants.BUFFER_SIZE]; // set up the download file information setupDestinationFile(state, innerState); // add request headers for continue downloading addRequestHeaders(innerState, request); // check just before sending the request to avoid using an invalid connection at all checkConnectivity(state); HttpResponse response = sendRequest(state, client, request); handleExceptionalStatus(state, innerState, response); Utils.D("received response for " + mInfo.mUri); processResponseHeaders(state, innerState, response); InputStream entityStream = openResponseEntity(state, response); DigestInputStream responseStream = new DigestInputStream(entityStream, state.mDigester); transferData(state, innerState, data, responseStream); }
From source file:com.lan.nicehair.common.download.DownloadThread.java
/** * Fully execute a single download request - setup and send the request, handle the response, * and transfer the data to the destination file. */// www . ja v a 2 s . c o m private void executeDownload(State state, AndroidHttpClient client, HttpGet request) throws StopRequest, RetryDownload { InnerState innerState = new InnerState(); byte data[] = new byte[Constants.BUFFER_SIZE]; // set up the download file information setupDestinationFile(state, innerState); // add request headers for continue downloading addRequestHeaders(innerState, request); // check just before sending the request to avoid using an invalid connection at all checkConnectivity(state); HttpResponse response = sendRequest(state, client, request); handleExceptionalStatus(state, innerState, response); AppLog.d(TAG, "received response for " + mInfo.mUri); processResponseHeaders(state, innerState, response); InputStream entityStream = openResponseEntity(state, response); DigestInputStream responseStream = new DigestInputStream(entityStream, state.mDigester); transferData(state, innerState, data, responseStream); }
From source file:de.uzk.hki.da.pkg.MetsConsistencyChecker.java
/** * Calculate hash.//from ww w .ja v a 2 s . c o m * * @param algorithm the algorithm * @param file the file * @return the string * @throws Exception the exception */ private static String calculateHash(MessageDigest algorithm, File file) throws Exception { 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) ; // get the hash value as byte array byte[] hash = algorithm.digest(); fis.close(); bis.close(); dis.close(); return byteArray2Hex(hash); }
From source file:de.tudarmstadt.ukp.dkpro.core.api.datasets.DatasetFactory.java
/** * Verify/download/update artifact in cache. Execute post-download actions. *//*from ww w .jav a 2s. c om*/ private void materialize(DatasetDescription aDataset) throws IOException { Path root = resolve(aDataset); Collection<ArtifactDescription> artifacts = aDataset.getArtifacts().values(); // First validate if local copies are still up-to-date boolean reload = false; packageValidationLoop: for (ArtifactDescription artifact : artifacts) { Path cachedFile = resolve(aDataset, artifact); if (!Files.exists(cachedFile)) { continue; } if (artifact.getSha1() != null) { String actual = getDigest(cachedFile, "SHA1"); if (!artifact.getSha1().equals(actual)) { LOG.info("Local SHA1 hash mismatch on [" + cachedFile + "] - expected [" + artifact.getSha1() + "] - actual [" + actual + "]"); reload = true; break packageValidationLoop; } else { LOG.info("Local SHA1 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 [" + root + "]"); FileUtils.deleteQuietly(root.toFile()); } for (ArtifactDescription artifact : artifacts) { Path cachedFile = resolve(aDataset, artifact); if (Files.exists(cachedFile)) { continue; } if (artifact.getText() != null) { Files.createDirectories(cachedFile.getParent()); LOG.info("Creating [" + cachedFile + "]"); try (Writer out = Files.newBufferedWriter(cachedFile, StandardCharsets.UTF_8)) { out.write(artifact.getText()); } } if (artifact.getUrl() != null) { Files.createDirectories(cachedFile.getParent()); MessageDigest sha1; try { sha1 = MessageDigest.getInstance("SHA1"); } catch (NoSuchAlgorithmException e) { throw new IOException(e); } URL source = new URL(artifact.getUrl()); LOG.info("Fetching [" + cachedFile + "]"); URLConnection connection = source.openConnection(); connection.setRequestProperty("User-Agent", "Java"); try (InputStream is = connection.getInputStream()) { DigestInputStream sha1Filter = new DigestInputStream(is, sha1); Files.copy(sha1Filter, cachedFile); if (artifact.getSha1() != null) { String sha1Hex = new String(Hex.encodeHex(sha1Filter.getMessageDigest().digest())); if (!artifact.getSha1().equals(sha1Hex)) { String message = "SHA1 mismatch. Expected [" + artifact.getSha1() + "] but got [" + sha1Hex + "]."; LOG.error(message); throw new IOException(message); } } } } } // Perform a post-fetch action such as unpacking Path postActionCompleteMarker = resolve(aDataset).resolve(".postComplete"); if (!Files.exists(postActionCompleteMarker)) { for (ArtifactDescription artifact : artifacts) { Path cachedFile = resolve(aDataset, artifact); List<ActionDescription> actions = artifact.getActions(); if (actions != null && !actions.isEmpty()) { try { for (ActionDescription action : actions) { LOG.info("Post-download action [" + action.getAction() + "]"); Class<? extends Action_ImplBase> implClass = actionRegistry.get(action.getAction()); if (implClass == null) { throw new IllegalStateException( "Unknown or unsupported action [" + action.getAction() + "]"); } Action_ImplBase impl = implClass.newInstance(); impl.apply(action, aDataset, artifact, cachedFile); } } catch (IllegalStateException e) { throw e; } catch (IOException e) { throw e; } catch (Exception e) { throw new IllegalStateException(e); } } } Files.createFile(postActionCompleteMarker); } }
From source file:at.alladin.rmbt.client.v2.task.HttpProxyTask.java
/** * // w w w.j av a 2 s.c o m * @param file * @return * @throws NoSuchAlgorithmException * @throws IOException */ public static String generateChecksum(File file) throws NoSuchAlgorithmException, IOException { MessageDigest md = MessageDigest.getInstance("MD5"); DigestInputStream dis = new DigestInputStream(new FileInputStream(file), md); int ch; while ((ch = dis.read()) != -1) { //empty block } dis.close(); return generateChecksumFromDigest(md.digest()); }
From source file:pl.nask.hsn2.service.analysis.JSWekaAnalyzer.java
/** * Returns hex string representation of MD5 hash for given file. * /*from w ww . j a v a2s . c om*/ * @param fileName * @return * @throws IOException */ public final String md5hashFromFile(BufferedInputStream bufferedInputStream) throws IOException { bufferedInputStream.reset(); String result = null; MessageDigest md; try { md = MessageDigest.getInstance("MD5"); md.reset(); try (InputStream dis = new DigestInputStream(new WhiteListFileInputStream(bufferedInputStream), md)) { while (dis.read() != -1) { //NOPMD // Nothing to do. } char[] md5 = Hex.encodeHex(md.digest()); result = String.valueOf(md5); } } catch (NoSuchAlgorithmException e) { LOGGER.error("Could not create MD5 hash for whitelisting.\n{}", e); result = ""; } return result; }
From source file:edu.mit.lib.bagit.Filler.java
/** * Adds the contents of the passed stream to a tag (metadata) file at the * specified relative path in the bag directory tree. * /* w ww. jav a2 s.c o m*/ * @param relPath * the relative path of the file * @param is * the input stream to read. * @return Filler this Filler * @throws IOException */ public Filler tag(String relPath, InputStream is) throws IOException { // make sure tag files not written to payload directory if (relPath.startsWith(DATA_PATH)) { throw new IOException("Tag files not allowed in paylod directory"); } if (bagFile(relPath).exists()) { throw new IllegalStateException("Tag file already exists at: " + relPath); } // wrap stream in digest stream try (DigestInputStream dis = new DigestInputStream(is, MessageDigest.getInstance(csAlg))) { Files.copy(dis, tagFile(relPath).toPath()); // record checksum tagWriter.writeLine(toHex(dis.getMessageDigest().digest()) + " " + relPath); } catch (NoSuchAlgorithmException nsaE) { throw new IOException("no algorithm: " + csAlg); } return this; }
From source file:edu.ku.brc.specify.plugins.ipadexporter.iPadRepositoryHelper.java
/** * @param file//from w ww. j a v a 2 s.c o m * @return * @throws Exception */ private String calculateHash(final File file) throws Exception { if (sha1 != null) { FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); DigestInputStream dis = new DigestInputStream(bis, sha1); // read the file and update the hash calculation while (dis.read() != -1) ; // get the hash value as byte array byte[] hash = sha1.digest(); return byteArray2Hex(hash); } return null; }