List of usage examples for java.security DigestInputStream DigestInputStream
public DigestInputStream(InputStream stream, MessageDigest digest)
From source file:com.mum.app.AutoSubmitPriceApp.java
private String computeContentMD5HeaderValue(InputStream fis) throws IOException, NoSuchAlgorithmException { DigestInputStream dis = new DigestInputStream(fis, MessageDigest.getInstance("MD5")); byte[] buffer = new byte[8192]; while (dis.read(buffer) > 0) ;//from w w w . ja va 2 s .c o m String md5Content = new String( org.apache.commons.codec.binary.Base64.encodeBase64(dis.getMessageDigest().digest())); return md5Content; }
From source file:org.dcm4chee.storage.tar.TarContainerProvider.java
@Override public void extractEntries(RetrieveContext ctx, String name, ExtractTask extractTask, InputStream in) throws IOException { TarArchiveInputStream tar = new TarArchiveInputStream(in); TarArchiveEntry entry = skipDirectoryEntries(tar); if (entry == null) throw new IOException("No entries in " + name); String entryName = entry.getName(); Map<String, byte[]> checksums = null; String checksumEntry = container.getChecksumEntry(); MessageDigest digest = null;/*from w w w.j av a 2 s.c o m*/ if (checksumEntry != null) { if (checksumEntry.equals(entryName)) { try { digest = MessageDigest .getInstance(ctx.getStorageSystem().getStorageSystemGroup().getDigestAlgorithm()); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } checksums = ContainerEntry.readChecksumsFrom(tar); } else LOG.warn("Misssing checksum entry in %s", name); entry = skipDirectoryEntries(tar); } for (; entry != null; entry = skipDirectoryEntries(tar)) { entryName = entry.getName(); InputStream in0 = tar; byte[] checksum = null; if (checksums != null && digest != null) { checksum = checksums.remove(entryName); if (checksum == null) throw new ChecksumException( "Missing checksum for container entry: " + entryName + " in " + name); digest.reset(); in0 = new DigestInputStream(tar, digest); } extractTask.copyStream(entryName, in0); if (checksums != null && digest != null) { if (!Arrays.equals(digest.digest(), checksum)) { throw new ChecksumException( "Checksums do not match for container entry: " + entry.getName() + " in " + name); } } extractTask.entryExtracted(entryName); } }
From source file:org.eclipse.smarthome.core.thing.internal.firmware.FirmwareImpl.java
@Override public synchronized byte @Nullable [] getBytes() { if (inputStream == null) { return null; }//from w w w . ja v a2 s .co m if (bytes == null) { try { MessageDigest md = MessageDigest.getInstance("MD5"); try (DigestInputStream dis = new DigestInputStream(inputStream, md)) { bytes = IOUtils.toByteArray(dis); } catch (IOException ioEx) { logger.error("Cannot read firmware {}.", this, ioEx); return null; } byte[] digest = md.digest(); if (md5Hash != null && digest != null) { StringBuilder digestString = new StringBuilder(); for (byte b : digest) { digestString.append(String.format("%02x", b)); } if (!md5Hash.equals(digestString.toString())) { bytes = null; throw new IllegalStateException(String .format("Invalid MD5 checksum. Expected %s, but was %s.", md5Hash, digestString)); } } } catch (NoSuchAlgorithmException e) { logger.error("Cannot calculate MD5 checksum.", e); bytes = null; return null; } } return bytes; }
From source file:org.fcrepo.apix.integration.StreamingIT.java
/** * Verify the binary can be retrieved from Fedora. The request should <em>not</em> be intercepted. * * @throws Exception if unexpected things go wrong *///from w w w . jav a2s .co m @Test public void testRetrieveLargeBinaryFromFedora() throws Exception { // Record 'true' if the intercepting route is triggered final AtomicBoolean intercepted = new AtomicBoolean(false); ctx.getRouteDefinition(INTERCEPT_ROUTE_ID).adviceWith((ModelCamelContext) ctx, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { weaveAddFirst().process((ex) -> intercepted.set(true)); } }); final long expectedSize = (2 * 1024 * 1024) + 1; final long actualSize; final String actualDigest; try (FcrepoResponse r = client.get(binaryResource).perform(); DigestInputStream body = new DigestInputStream(r.getBody(), sha1)) { actualSize = drain(body); actualDigest = asHex(body.getMessageDigest().digest()); } // The resource can be retrieved intact assertEquals(expectedSize, actualSize); assertEquals(binaryResourceSha, actualDigest); // And the request was not proxied by API-X assertFalse(String.format("Unexpected interception of a Fedora resource URI %s by route %s", binaryResource.toString(), INTERCEPT_ROUTE_ID), intercepted.get()); }
From source file:de.elomagic.carafile.client.CaraFileClient.java
/** * Uploads data via an {@link InputStream} (Single chunk upload). * <p/>/*www.j a va 2s.c om*/ * Single chunk upload means that the complete file will be upload in one step. * * @param in The input stream. It's not recommended to use a buffered stream. * @param filename Name of the file * @param contentLength Length of the content in bytes * @return Returns the {@link MetaData} of the uploaded stream * @throws IOException Thrown when unable to call REST services * @see CaraFileClient#uploadFile(java.net.URI, java.nio.file.Path, java.lang.String) */ public MetaData uploadFile(final InputStream in, final String filename, final long contentLength) throws IOException { if (registryURI == null) { throw new IllegalArgumentException("Parameter 'registryURI' must not be null!"); } if (in == null) { throw new IllegalArgumentException("Parameter 'in' must not be null!"); } URI peerURI = peerSelector.getURI(downloadPeerSet(), -1); if (peerURI == null) { throw new IOException("No peer for upload available"); } URI uri = CaraFileUtils.buildURI(peerURI, "peer", "seedFile", filename); MessageDigest messageDigest = DigestUtils.getSha1Digest(); try (BufferedInputStream bis = new BufferedInputStream(in); DigestInputStream dis = new DigestInputStream(bis, messageDigest)) { HttpResponse response = executeRequest( Request.Post(uri).bodyStream(dis, ContentType.APPLICATION_OCTET_STREAM)).returnResponse(); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { throw new HttpResponseException(statusCode, "Unable to upload file: " + response.getStatusLine().getReasonPhrase()); } MetaData md = getMetaDataFromResponse(response); if (!Hex.encodeHexString(messageDigest.digest()).equals(md.getId())) { throw new IOException("Peer response invalid SHA1 of file"); } return md; } }
From source file:org.dspace.pack.bagit.Bag.java
public void addData(String relPath, long size, InputStream is) throws IOException { if (filled) { throw new IllegalStateException("Cannot add data to filled bag"); }//from www . j ava 2 s . c o m // wrap stream in digest stream DigestInputStream dis = null; try { dis = new DigestInputStream(is, MessageDigest.getInstance(CS_ALGO)); FileOutputStream fos = new FileOutputStream(dataFile(relPath)); // attempt to optimize copy in various ways - TODO Utils.copy(dis, fos); fos.close(); dis.close(); is.close(); } catch (NoSuchAlgorithmException nsaE) { throw new IOException("no algorithm: " + CS_ALGO); } // record checksum String brPath = "data/" + relPath; manWriter.writeProperty(Utils.toHex(dis.getMessageDigest().digest()), brPath); }
From source file:org.sead.va.dataone.Object.java
@POST @Path("/{objectId}") @Consumes(MediaType.APPLICATION_XML)/* w w w .j a va2 s .com*/ @Produces(MediaType.APPLICATION_XML) public Response addObject(@Context HttpServletRequest request, @PathParam("objectId") String id, @QueryParam("creators") String creators, @QueryParam("deprecateFgdc") String deprecateFgdc, String fgdcString) throws UnsupportedEncodingException { Document metaInfo = new Document(); metaInfo.put(Constants.META_FORMAT, "http://www.fgdc.gov/schemas/metadata/fgdc-std-001-1998.xsd"); metaInfo.put(Constants.RO_ID, id); org.w3c.dom.Document doc = null; try { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); doc = dBuilder.parse(new ByteArrayInputStream(fgdcString.getBytes())); } catch (ParserConfigurationException e) { System.out.println(e.getMessage()); } catch (SAXException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } String creator = ""; if (creators != null && !creators.equals("")) { creator = URLEncoder.encode(creators.split("\\|")[0].replace(" ", "").replace(",", "")) + "-"; } String fgdcId = "seadva-" + creator + UUID.randomUUID().toString(); metaInfo.put(Constants.FGDC_ID, fgdcId); final byte[] utf8Bytes = fgdcString.getBytes("UTF-8"); metaInfo.put(Constants.SIZE, utf8Bytes.length); String strDate = simpleDateFormat.format(new Date()); metaInfo.put(Constants.META_UPDATE_DATE, strDate); metaInfo.put(Constants.DEPOSIT_DATE, strDate); try { DigestInputStream digestStream = new DigestInputStream(new ByteArrayInputStream(fgdcString.getBytes()), MessageDigest.getInstance("SHA-1")); if (digestStream.read() != -1) { byte[] buf = new byte[1024]; while (digestStream.read(buf) != -1) ; } byte[] digest = digestStream.getMessageDigest().digest(); metaInfo.put(Constants.FIXITY_FORMAT, "SHA-1"); metaInfo.put(Constants.FIXITY_VAL, new String(Hex.encodeHex(digest))); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } Document document = new Document(); document.put(Constants.META_INFO, metaInfo); document.put(Constants.METADATA, fgdcString); RO_STATUS updated = RO_STATUS.NOT_EXIST; updated = deprecateFGDC(id, document); if (deprecateFgdc != null && !deprecateFgdc.equals("") && updated == RO_STATUS.NOT_EXIST) { updated = deprecateFGDC(deprecateFgdc, document); } if (updated == RO_STATUS.NON_IDENTICAL || updated == RO_STATUS.NOT_EXIST) { fgdcCollection.insertOne(document); } return Response.ok().build(); }
From source file:dk.netarkivet.common.distribute.FTPRemoteFile.java
/** * Private constructor used by getInstance() static-method Tries to generate unique name on ftp-server. * * @param localFile File used to create new file on ftp-server. * @param useChecksums If true, checksums will be used to check transfers. * @param fileDeletable If true, this file will be deleted after upload to FTP. * @param multipleDownloads If true, the file will not be removed from FTP server automatically after first * download./* w w w . jav a2 s . c o m*/ * @param connectionParams If not null, contains connection parameters to the FTP-server desired by the user * @throws IOFailure if MD5 checksum fails, or ftp fails * @throws ArgumentNotValid if the local file cannot be read. */ private FTPRemoteFile(File localFile, boolean useChecksums, boolean fileDeletable, boolean multipleDownloads, RemoteFileSettings connectionParams) throws IOFailure { super(localFile, useChecksums, fileDeletable, multipleDownloads); if (connectionParams != null) { // use the connection parameters desired by the user. this.ftpServerName = connectionParams.getServerName(); this.ftpServerPort = connectionParams.getServerPort(); this.ftpUserName = connectionParams.getUserName(); this.ftpUserPassword = connectionParams.getUserPassword(); } else { // use the connection parameters specified by the settings. this.ftpServerName = Settings.get(CommonSettings.FTP_SERVER_NAME); this.ftpServerPort = Settings.getInt(CommonSettings.FTP_SERVER_PORT); this.ftpUserName = Settings.get(CommonSettings.FTP_USER_NAME); this.ftpUserPassword = Settings.get(CommonSettings.FTP_USER_PASSWORD); } this.cm = new FTPConnectionManager(ftpUserName, ftpUserPassword, ftpServerName, ftpServerPort, Settings.getInt(CommonSettings.FTP_RETRIES_SETTINGS), Settings.getInt(CommonSettings.FTP_DATATIMEOUT_SETTINGS)); if (filesize == 0) { if (useChecksums) { checksum = ChecksumCalculator.calculateMd5(file); } else { checksum = null; } ftpFileName = "-"; } else { // A large enough number to make it unlikely that two files are // created with the same FTP server name. Already the millisecond // datestamp reduces the likelihood, with this even if two // processes/threads try to upload the same file in the same // millisecond (very unlikely) they have only .01% chance of // clashing. final int aMagicNumber = 100000; ftpFileName = file.getName() + "-" + new Random().nextInt(aMagicNumber) + "-" + new Date().getTime(); InputStream in; try { in = new FileInputStream(localFile); } catch (FileNotFoundException e) { final String message = "Couldn't prepare file '" + localFile + "' for remote access. File not found."; log.debug(message, e); throw new IOFailure(message, e); } log.debug("Writing '{}' as '{}' on ftp-server {}", file.getName(), ftpFileName, cm.getFtpServer()); // Writing inlined in constructor to allow the checksum field to // be final (and thus must be set in constructor). try { cm.logOn(); if (useChecksums) { in = new DigestInputStream(in, ChecksumCalculator.getMessageDigest(ChecksumCalculator.MD5)); } boolean success = false; int tried = 0; String message = null; while (!success && tried < FTP_RETRIES) { tried++; try { success = cm.getFTPClient().storeFile(ftpFileName, in); if (!success) { log.debug("FTP store failed attempt '{}' of {}: {}", tried, FTP_RETRIES, cm.getFtpErrorMessage()); } } catch (IOException e) { message = "Write operation to '" + ftpFileName + "' failed on attempt " + tried + " of " + FTP_RETRIES; if (e instanceof CopyStreamException) { CopyStreamException realException = (CopyStreamException) e; message += "(real cause = " + realException.getIOException() + ")"; } log.debug(message, e); } } if (!success) { final String msg = "Failed to upload '" + localFile + "' after " + tried + " attempts. Reason for last failure: " + message; log.warn(msg); // Send an Notification because of this NotificationsFactory.getInstance().notify(msg, NotificationType.ERROR); throw new IOFailure(msg); } log.debug("Completed writing the file '{}'", ftpFileName); if (useChecksums) { checksum = ChecksumCalculator.toHex(((DigestInputStream) in).getMessageDigest().digest()); log.debug("Checksum of '{}' is:{}", ftpFileName, checksum); } else { checksum = null; } } finally { IOUtils.closeQuietly(in); cm.logOut(); log.debug("Ftp logout"); } } if (fileDeletable) { try { FileUtils.removeRecursively(localFile); } catch (IOFailure e) { // Not fatal log.warn("Couldn't remove tmp file {}", localFile, e); } } }
From source file:de.tudarmstadt.ukp.dkpro.core.api.datasets.DatasetLoader.java
private String getDigest(File aFile, String aDigest) throws IOException { MessageDigest digest;// w ww . j a v a 2 s . c om try { digest = MessageDigest.getInstance(aDigest); } catch (NoSuchAlgorithmException e) { throw new IOException(e); } try (InputStream is = new FileInputStream(aFile)) { DigestInputStream digestFilter = new DigestInputStream(is, digest); IOUtils.copy(digestFilter, new NullOutputStream()); return new String(Hex.encodeHex(digestFilter.getMessageDigest().digest())); } }
From source file:edu.mit.lib.bagit.Filler.java
/** * Adds the contents of the passed stream to the payload at the specified * relative path in the data directory tree. * //from ww w.j ava 2 s . co m * @param relPath * the relative path of the file * @param is * the input stream to read. * @return Filler this Filler * @throws IOException */ public Filler payload(String relPath, InputStream is) throws IOException { if (dataFile(relPath).exists()) { // TODO: overwrite? or merge? // throw new // IllegalStateException("Payload file already exists at: " // + relPath); } // wrap stream in digest stream try (DigestInputStream dis = new DigestInputStream(is, MessageDigest.getInstance(csAlg))) { payloadSize += Files.copy(dis, dataFile(relPath).toPath()); payloadCount++; // record checksum manWriter.writeLine(toHex(dis.getMessageDigest().digest()) + " " + DATA_PATH + relPath); } catch (NoSuchAlgorithmException nsaE) { throw new IOException("no algorithm: " + csAlg); } return this; }