List of usage examples for java.security DigestInputStream getMessageDigest
public MessageDigest getMessageDigest()
From source file:edu.stanford.muse.datacache.BlobStore.java
/** * add a new piece of data with content copied from is. * if owner is not set on the data, we will set it to this data store's owner. * computes content hash and sets it.//from ww w . ja v a 2 s. co m * will close the stream when done. * Performance critical! * returns # of bytes in inputstream */ public long add(Blob blob, InputStream is) throws IOException { long nBytes = -1; /* we no longer assume that if 2 blobs have the same name and file size, their contents must be the same! synchronized (this) { if (this.contains(blob)) { log.info("Item is already present: " + blob); return nBytes; } add(blob); Util.ASSERT(this.contains(blob)); } */ // release the lock here because we dont want a long op like reading the object's stream // and storing the file to be serialized across threads. // copy the stream, if it throws IOException, then we cancel the item try { // this code is slightly tricky because blob equality (and therefore its membership in this blob store) depends on the hash , which we don't have yet. // so we first read the stream into a temp file. As we read the temp file, we get its checksum. We use the checksum to initialize the blob object completely // and check if it already exists in the blob store. if not, we assign it in the id_map etc. and then rename the temp file to the proper location in the blob store. DigestInputStream din = new DigestInputStream(is, MessageDigest.getInstance("SHA-1")); log.info(" adding file to blob store = " + blob.filename); Path tmpPath = Files.createTempFile(new File(System.getProperty("java.io.tmpdir")).toPath(), "epadd.", ".temp"); File tmpFile = tmpPath.toFile(); nBytes = Util.copy_stream_to_file(din, tmpFile.getAbsolutePath()); // now set the blob size and digest blob.size = nBytes; // overwrite this -- earlier we had the part size stored in blob size // be careful -- add the blob only after its size and SHA-1 has been updated byte byteArray[] = din.getMessageDigest().digest(); blob.setContentHash(byteArray); // ok, now blob is finally set up and can be compared // if the blob is already present, fetching the bytes was a waste, but there would no other way to know its checksum. // we'll delete the file that is in the tmp dir if (uniqueBlobs.contains(blob)) { tmpFile.delete(); return nBytes; } // blob doesn't already exist, add it, and move it from the temp dir to its actual place in the blobstore add(blob); // move it from the temp file to the blobs dir. don't do this before add(blob), because full_filename will not be set up correctly until the blob object can be lookedup String destination = dir + File.separatorChar + full_filename(blob); Files.move(tmpPath, new File(destination).toPath()); } catch (IOException ioe) { // we couldn't copy the stream to the data store, so undo everything Util.print_exception("IO Error copying blob to blobstore", ioe, log); remove(blob); Util.ASSERT(!this.contains(blob)); throw ioe; } catch (NoSuchAlgorithmException nsae) { // we couldn't copy the stream to the data store, so undo everything remove(blob); Util.ASSERT(!this.contains(blob)); throw new RuntimeException(nsae); } Util.ASSERT(this.contains(blob)); // packing needs to be done more efficiently (batch mode or incremental) if ((uniqueBlobs.size() % 100) == 0) pack(); return nBytes; }
From source file:org.opencastproject.workingfilerepository.impl.WorkingFileRepositoryImpl.java
/** * {@inheritDoc}// w w w. j a v a 2s. co m * * @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:org.opencastproject.workingfilerepository.impl.WorkingFileRepositoryImpl.java
/** * {@inheritDoc}//from w w w . j a va 2 s .c o m * * @see org.opencastproject.workingfilerepository.api.WorkingFileRepository#put(java.lang.String, java.lang.String, * java.lang.String, java.io.InputStream) */ public URI put(String mediaPackageID, String mediaPackageElementID, String filename, InputStream in) throws IOException { checkPathSafe(mediaPackageID); checkPathSafe(mediaPackageElementID); File f = null; File dir = getElementDirectory(mediaPackageID, mediaPackageElementID); if (dir.exists()) { // clear the directory File[] filesToDelete = dir.listFiles(); if (filesToDelete != null && filesToDelete.length > 0) { for (File fileToDelete : filesToDelete) { if (!fileToDelete.delete()) { throw new IllegalStateException("Unable to delete file: " + fileToDelete.getAbsolutePath()); } } } } else { logger.debug("Attempting to create a new directory at {}", dir.getAbsolutePath()); FileUtils.forceMkdir(dir); } f = new File(dir, PathSupport.toSafeName(filename)); logger.debug("Attempting to write a file to {}", f.getAbsolutePath()); FileOutputStream out = null; try { if (!f.exists()) { 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.deleteDirectory(dir); throw e; } finally { IOUtils.closeQuietly(out); IOUtils.closeQuietly(in); } return getURI(mediaPackageID, mediaPackageElementID, filename); }
From source file:org.dspace.services.impl.storage.DSpaceStorageService.java
@Override public Bitstream register(int assetstore, String path) throws StorageException { this.init();//from www . ja va 2 s . c o m // mark this bitstream as a registered bitstream String sInternalId = Bitstream.REGISTERED_FLAG + path; // Create a deleted bitstream row, using a separate DB connection Bitstream bitstream = new Bitstream(); bitstream.setDeleted(true); bitstream.setInternalId(sInternalId); bitstream.setStoreNumber(assetstore); bitstreamDao.save(bitstream); // get a reference to the file GeneralFile file = getFile(bitstream); // read through a DigestInputStream that will work out the MD5 // // DSpace refers to checksum, writes it in METS, and uses it as an // AIP filename (!), but never seems to validate with it. Furthermore, // DSpace appears to hardcode the algorithm to MD5 in some places--see // METSExport.java. // // To remain compatible with DSpace we calculate an MD5 checksum on // LOCAL registered files. But for REMOTE (e.g. SRB) files we // calculate an MD5 on just the fileNAME. The reasoning is that in the // case of a remote file, calculating an MD5 on the file itself will // generate network traffic to read the file's bytes. In this case it // would be better have a proxy process calculate MD5 and store it as // an SRB metadata attribute so it can be retrieved simply from SRB. // // TODO set this up as a proxy server process so no net activity // FIXME this is a first class HACK! for the reasons described above if (file instanceof LocalFile) { // get MD5 on the file for local file DigestInputStream dis = null; try { dis = new DigestInputStream(FileFactory.newFileInputStream(file), MessageDigest.getInstance("MD5")); } catch (NoSuchAlgorithmException e) { log.warn("Caught NoSuchAlgorithmException", e); throw new StorageException("Invalid checksum algorithm", e); } catch (IOException e) { log.error("File: " + file.getAbsolutePath() + " to be registered cannot be opened - is it " + "really there?"); throw new StorageException(e); } final int BUFFER_SIZE = 1024 * 4; final byte[] buffer = new byte[BUFFER_SIZE]; try { while (true) { final int count = dis.read(buffer, 0, BUFFER_SIZE); if (count == -1) { break; } } bitstream.setChecksum(Utils.toHex(dis.getMessageDigest().digest())); dis.close(); } catch (IOException e) { throw new StorageException(e); } } else if (file instanceof SRBFile) { if (!file.exists()) { log.error("File: " + file.getAbsolutePath() + " is not in SRB MCAT"); throw new StorageException("File is not in SRB MCAT"); } // get MD5 on just the filename (!) for SRB file int iLastSlash = path.lastIndexOf('/'); String sFilename = path.substring(iLastSlash + 1); MessageDigest md = null; try { md = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { log.error("Caught NoSuchAlgorithmException", e); throw new StorageException("Invalid checksum algorithm", e); } bitstream.setChecksum(Utils.toHex(md.digest(sFilename.getBytes()))); } else { throw new StorageException("Unrecognized file type - " + "not local, not SRB"); } bitstream.setChecksumAlgorithm("MD5"); bitstream.setSize(file.length()); bitstream.setDeleted(false); bitstreamDao.save(bitstream); if (log.isDebugEnabled()) { log.debug("Stored bitstream " + bitstream.getID() + " in file " + file.getAbsolutePath()); } return bitstream; }
From source file:uk.ac.ebi.jmzml.xml.io.MzMLUnmarshaller.java
/** * Calcultes the check sum./*from www .j a v a 2s . com*/ * * @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:API.amazon.mws.feeds.service.MarketplaceWebServiceClient.java
/** * Calculate content MD5 header values for feeds stored on disk. *///from ww w . j a v a 2 s .co m private String computeContentMD5HeaderValue(FileInputStream fis) throws IOException, NoSuchAlgorithmException { DigestInputStream dis = new DigestInputStream(fis, MessageDigest.getInstance("MD5")); byte[] buffer = new byte[8192]; while (dis.read(buffer) > 0) ; String md5Content = new String( org.apache.commons.codec.binary.Base64.encodeBase64(dis.getMessageDigest().digest())); // Effectively resets the stream to be beginning of the file via a FileChannel. fis.getChannel().position(0); return md5Content; }