List of usage examples for java.security DigestInputStream close
public void close() throws IOException
From source file:Main.java
private static String computeHash(InputStream dataStream) throws IOException, NoSuchAlgorithmException { MessageDigest messageDigest = null; DigestInputStream digestInputStream = null; try {//from w w w . ja va 2s . c om messageDigest = MessageDigest.getInstance("SHA-256"); digestInputStream = new DigestInputStream(dataStream, messageDigest); byte[] byteBuffer = new byte[1024 * 8]; while (digestInputStream.read(byteBuffer) != -1) ; } finally { try { if (digestInputStream != null) digestInputStream.close(); if (dataStream != null) dataStream.close(); } catch (IOException e) { e.printStackTrace(); } } byte[] hash = messageDigest.digest(); return String.format("%064x", new java.math.BigInteger(1, hash)); }
From source file:at.alladin.rmbt.client.v2.task.HttpProxyTask.java
/** * //from w w w .j a v a 2 s . c om * @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:de.uzk.hki.da.pkg.MetsConsistencyChecker.java
/** * Calculate hash./*from w w w . jav a 2s.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:com.clustercontrol.repository.factory.AgentLibDownloader.java
/** * MD5??//from w ww . j a va 2 s .c o m * @param filepath * @return * @throws HinemosUnknown */ private static String getMD5(String filepath) throws HinemosUnknown { MessageDigest md = null; DigestInputStream inStream = null; byte[] digest = null; try { md = MessageDigest.getInstance("MD5"); inStream = new DigestInputStream(new BufferedInputStream(new FileInputStream(filepath)), md); while (inStream.read() != -1) { } digest = md.digest(); } catch (Exception e) { m_log.warn("getMD5() : filepath=" + filepath + ", " + e.getClass(), e); } finally { if (inStream != null) { try { inStream.close(); } catch (Exception e) { m_log.warn("getMD5() : close " + e.getClass(), e); } } if (digest == null) throw new HinemosUnknown("MD5 digest is null"); } return hashByte2MD5(digest); }
From source file:fr.gouv.culture.vitam.digest.DigestCompute.java
/** * //from w ww. ja v a 2 s. co m * @param fis * @param algorithm * @return the Hashcode according to the algorithm * @throws Exception */ public final static String getHashCode(FileInputStream fis, String algorithm) throws Exception { MessageDigest md = MessageDigest.getInstance(algorithm); DigestInputStream dis = null; try { dis = new DigestInputStream(fis, md); byte[] buffer = new byte[8192]; while (dis.read(buffer) != -1) ; } finally { if (dis != null) { dis.close(); } } byte[] bDigest = md.digest(); return Base64.encode(bDigest, false); }
From source file:hudson.Util.java
/** * Computes MD5 digest of the given input stream. * * @param source//from w ww.jav a 2 s . co m * The stream will be closed by this method at the end of this method. * @return * 32-char wide string */ public static String getDigestOf(InputStream source) throws IOException { try { MessageDigest md5 = MessageDigest.getInstance("MD5"); DigestInputStream in = new DigestInputStream(source, md5); try { while (in.read(garbage) > 0) ; // simply discard the input } finally { in.close(); } return toHexString(md5.digest()); } catch (NoSuchAlgorithmException e) { throw new IOException2("MD5 not installed", e); // impossible } }
From source file:Main.java
public static String md5(InputStream in) { int bufferSize = 256 * 1024; DigestInputStream digestInputStream = null; try {/*from w w w . j a v a2 s . c o m*/ MessageDigest messageDigest = MessageDigest.getInstance("MD5"); digestInputStream = new DigestInputStream(in, messageDigest); byte[] buffer = new byte[bufferSize]; while (digestInputStream.read(buffer) > 0) ; messageDigest = digestInputStream.getMessageDigest(); byte[] resultByteArray = messageDigest.digest(); char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; char[] resultCharArray = new char[resultByteArray.length * 2]; int index = 0; for (byte b : resultByteArray) { resultCharArray[index++] = hexDigits[b >>> 4 & 0xf]; resultCharArray[index++] = hexDigits[b & 0xf]; } return new String(resultCharArray); } catch (NoSuchAlgorithmException e) { return null; } catch (IOException e) { e.printStackTrace(); } finally { try { if (digestInputStream != null) digestInputStream.close(); } catch (Exception e) { e.printStackTrace(); } } return null; }
From source file:Main.java
public static String encode(InputStream in) { int bufferSize = 256 * 1024; DigestInputStream digestInputStream = null; try {/*w w w. ja v a2 s. c o m*/ MessageDigest messageDigest = MessageDigest.getInstance("MD5"); digestInputStream = new DigestInputStream(in, messageDigest); byte[] buffer = new byte[bufferSize]; while (digestInputStream.read(buffer) > 0) ; messageDigest = digestInputStream.getMessageDigest(); byte[] resultByteArray = messageDigest.digest(); char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; char[] resultCharArray = new char[resultByteArray.length * 2]; int index = 0; for (byte b : resultByteArray) { resultCharArray[index++] = hexDigits[b >>> 4 & 0xf]; resultCharArray[index++] = hexDigits[b & 0xf]; } return new String(resultCharArray); } catch (NoSuchAlgorithmException e) { return null; } catch (IOException e) { e.printStackTrace(); } finally { try { if (digestInputStream != null) digestInputStream.close(); } catch (Exception e) { e.printStackTrace(); } } return null; }
From source file:hudson.Util.java
/** * Computes MD5 digest of the given input stream. * * @param source//from w ww . j a v a2 s.c o m * The stream will be closed by this method at the end of this method. * @return * 32-char wide string */ public static String getDigestOf(InputStream source) throws IOException { try { MessageDigest md5 = MessageDigest.getInstance("MD5"); byte[] buffer = new byte[1024]; DigestInputStream in = new DigestInputStream(source, md5); try { while (in.read(buffer) > 0) ; // simply discard the input } finally { in.close(); } return toHexString(md5.digest()); } catch (NoSuchAlgorithmException e) { throw new IOException2("MD5 not installed", e); // impossible } }
From source file:com.aurel.track.dbase.HandleHome.java
public static String computeHash(File file) { try {// w w w .ja v a 2s . c o m MessageDigest md = MessageDigest.getInstance("MD5"); InputStream is = new FileInputStream(file); byte[] buffer = new byte[4096]; // To hold file contents DigestInputStream dis = new DigestInputStream(is, md); while (dis.read(buffer) != -1) { } byte[] digest = md.digest(); dis.close(); String hash = DatatypeConverter.printHexBinary(digest); return hash; } catch (Exception e) { } return null; }