List of usage examples for java.security DigestInputStream read
public int read() throws IOException
From source file:de.escidoc.bwelabs.deposit.DepositServiceSpec.java
private String calculateCheckSum(Configuration conf, InputStream is) throws NoSuchAlgorithmException, IOException { MessageDigest instance = MessageDigest .getInstance(conf.getProperty(Configuration.PROPERTY_CHECKSUM_ALGORITHM)); DigestInputStream digest = new DigestInputStream(is, instance); int read = digest.read(); while (read != -1) { read = digest.read();/* w w w . ja v a 2 s . c o m*/ } return Utility.byteArraytoHexString(instance.digest()); }
From source file:com.diversityarrays.dalclient.DalUtil.java
/** * Computes the MD5 checksum of the bytes in the InputStream. * The input is close()d on exit./*from ww w. java 2 s .c o m*/ * @param input is the InputStream for which to compute the checksum * @return the MD5 checksum as a String of hexadecimal characters */ static public String computeMD5checksum(InputStream input) { DigestInputStream dis = null; Formatter formatter = null; try { MessageDigest md = MessageDigest.getInstance(DIGEST_MD5); dis = new DigestInputStream(input, md); while (-1 != dis.read()) ; byte[] digest = md.digest(); formatter = new Formatter(); for (byte b : digest) { formatter.format("%02x", b); //$NON-NLS-1$ } return formatter.toString(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } finally { if (dis != null) { try { dis.close(); } catch (IOException ignore) { } } if (formatter != null) { formatter.close(); } } }
From source file:com.zimbra.cs.store.triton.TritonBlobStoreManager.java
@Override public byte[] getHash(Blob blob) throws ServiceException, IOException { MessageDigest digest = newDigest(); DigestInputStream dis = null; InputStream bis = null;// www. ja va 2 s . c o m try { bis = blob.getInputStream(); dis = new DigestInputStream(bis, digest); while (dis.read() >= 0) { } return digest.digest(); } finally { ByteUtil.closeStream(bis); ByteUtil.closeStream(dis); } }
From source file:edu.ku.brc.specify.plugins.ipadexporter.iPadRepositoryHelper.java
/** * @param file/* w ww . j av 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; }
From source file:de.huxhorn.sulky.blobs.impl.BlobRepositoryImpl.java
private boolean valid(String id, File file) { if (!validating) { return true; }// ww w .jav a 2 s. co m MessageDigest digest = createMessageDigest(); FileInputStream input = null; try { input = new FileInputStream(file); DigestInputStream dis = new DigestInputStream(input, digest); for (;;) { if (dis.read() < 0) { break; } } byte[] hash = digest.digest(); Formatter formatter = new Formatter(); for (byte b : hash) { formatter.format("%02x", b); } return formatter.toString().equals(id); } catch (IOException e) { // ignore... } finally { IOUtils.closeQuietly(input); } return false; }
From source file:org.sead.va.dataone.Object.java
@POST @Path("/{objectId}") @Consumes(MediaType.APPLICATION_XML)/*from w w w .j a v a 2s.c om*/ @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:org.dataconservancy.dcs.ingest.client.impl.DualManagerDeposit.java
void uploadFile(DcsFile file, String path) { try {//from ww w .ja v a2 s . com File physicalFile = new File(path); DigestInputStream digestStream = new DigestInputStream(new FileInputStream(path), MessageDigest.getInstance(checksumAlgorithm)); String mime = MimeUtil.getMostSpecificMimeType(MimeUtil.getMimeTypes(new File(path))).toString(); /* * proceed to the end of the inputStream if we havent got there * apready. We need to visit every byte in order to have calculated * the digest. */ if (digestStream.read() != -1) { byte[] buf = new byte[1024]; while (digestStream.read(buf) != -1) ; } byte[] digest = digestStream.getMessageDigest().digest(); /* Set the file name */ file.setName(physicalFile.getName()); /* Set the calculated fixity */ DcsFixity fixity = new DcsFixity(); fixity.setAlgorithm(checksumAlgorithm); fixity.setValue(new String(Hex.encodeHex(digest))); file.addFixity(fixity); /* Set the format */ DcsFormat format = new DcsFormat(); format.setSchemeUri("http://www.iana.org/assignments/media-types/"); format.setFormat(mime); file.addFormat(format); long length = physicalFile.length(); file.setSource(doUpload(path, mime, digest, length)); file.setSizeBytes(length); file.setExtant(true); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:edu.ku.brc.util.WebStoreAttachmentMgr.java
/** * @param algorithm//w w w. j a va 2 s . co m * @param fileName * @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(); dis.close(); return byteArray2Hex(hash); } return null; }
From source file:com.flexoodb.common.FlexUtils.java
/** * computes the SHA1 hashcode for the passed byte array. * * @param ba byte array.// w w w. j a va 2 s . com * @return SHA1 string. */ public static String computeSHA1(byte[] ba) { String hash = null; try { MessageDigest sha1 = MessageDigest.getInstance("SHA1"); InputStream is = new ByteArrayInputStream(ba); BufferedInputStream bis = new BufferedInputStream(is); DigestInputStream dis = new DigestInputStream(bis, sha1); while (dis.read() != -1) { } ; byte[] h = sha1.digest(); Formatter formatter = new Formatter(); for (byte b : h) { formatter.format("%02x", b); } hash = formatter.toString(); } catch (Exception e) { e.printStackTrace(); } return hash; }