} finally { if (digestInputStream != null) { digestInputStream.close();
dis.close(); fis.close(); fis = null;
/** * This method will read all the bytes in the stream and digest them. * It will attempt to close the stream as well. */ public static Sha256Hash createFrom(InputStream is) throws IOException { try { MessageDigest digest = getMessageDigest(); DigestInputStream digestInputStream = new DigestInputStream(is, digest); ByteStreams.copy(digestInputStream, ByteStreams.nullOutputStream()); digestInputStream.close(); return createFrom(digest); } finally { try { is.close(); } catch (IOException e) {/* don't throw from finally*/} } }
private String shaDigestForFile(String filename) throws IOException { DigestInputStream str = digestInputStream(inputStreamForFile(filename)); byte[] dummy = new byte[4096]; for (int i = 1; i > 0; i = str.read(dummy)); str.close(); return HexBin.bytesToString(str.getMessageDigest().digest()); }
protected static void hashStream(InputStream is, MessageDigest md) throws Exception { DigestInputStream dis = new DigestInputStream(is, md); byte[] buf = new byte[8192]; while(dis.read(buf)!=-1); dis.close(); }
public static void updateDigest(final InputStream input, final MessageDigest digest) throws IOException { final byte[] buffer = new byte[65536]; final DigestInputStream digestStream = new DigestInputStream(input, digest); while (digestStream.read(buffer) >= 0); /* do nothing */ digestStream.close(); }
public void close() throws IOException { int r = 0; do { r = read(ourDirtyBuffer); } while(r >= 0); super.close(); }
public void close() throws IOException { int r = 0; do { r = read(ourDirtyBuffer); } while(r >= 0); super.close(); }
public void close() throws IOException { int r = 0; do { r = read(ourDirtyBuffer); } while(r >= 0); super.close(); }
public static String digest(X509Certificate k) throws Exception { MessageDigest md5 = MessageDigest.getInstance("SHA1"); DigestInputStream in = new DigestInputStream(new ByteArrayInputStream(k.getEncoded()), md5); try { while (in.read(new byte[128]) > 0) { } } finally { in.close(); } StringBuilder buf = new StringBuilder(); char[] hex = Hex.encodeHex(md5.digest()); for (int i = 0; i < hex.length; i += 2) { if (buf.length() > 0) buf.append(':'); buf.append(hex, i, 2); } return buf.toString(); } }
private static String createSignature(InputStream is) throws Exception { MessageDigest md = MessageDigest.getInstance("SHA-256"); DigestInputStream dis = new DigestInputStream(is, md); byte[] buffer = new byte[1024]; while (dis.read(buffer) != -1) {} dis.close(); byte[] digest = md.digest(); return "sha256:" + bytesToHex(digest); }
private static String calcHash(InputStream input, String algorithm) throws IOException { try { final MessageDigest md = MessageDigest.getInstance(algorithm); final DigestInputStream dis = new DigestInputStream(input, md); try { byte[] buff = new byte[4096]; // just read to get the Digest filled... while (dis.read(buff) > 0) { // nop } return new BigInteger(1, md.digest()).toString(16); } finally { dis.close(); } } catch (NoSuchAlgorithmException e) { // this should not happen return ""; } }
public static String getMD5(File file) throws Exception { DigestInputStream stream = null; try { stream = new DigestInputStream(new FileInputStream(file), MessageDigest.getInstance("MD5")); byte[] buffer = new byte[65536]; int read = stream.read(buffer); while (read >= 1) read = stream.read(buffer); } catch (Exception ignored) { return null; } finally { stream.close(); } return String.format("%1$032x", new BigInteger(1, stream.getMessageDigest().digest())); } }
/** * This method will read all the bytes in the stream and digest them. * It will attempt to close the stream as well. */ public static Sha256Hash createFrom(InputStream is) throws IOException { try { MessageDigest digest = getMessageDigest(); DigestInputStream digestInputStream = new DigestInputStream(is, digest); ByteStreams.copy(digestInputStream, ByteStreams.nullOutputStream()); digestInputStream.close(); return createFrom(digest); } finally { try { is.close(); } catch (IOException e) {/* don't throw from finally*/} } }
static String computeSha1(File file) { InputStream is = null; try { is = new FileInputStream(file); MessageDigest md = MessageDigest.getInstance("SHA-1"); DigestInputStream dis = new DigestInputStream(is, md); IOUtils.copyLarge(dis, new NullOutputStream()); dis.close(); byte[] bytes = dis.getMessageDigest().digest(); return hex(bytes); } catch (NoSuchAlgorithmException e) { throw new UnhandledException(e); } catch (IOException e) { throw new UnhandledException(e); } finally { IOUtils.closeQuietly(is); } }
private String getDigest(File file) throws Exception { DigestInputStream dis = new DigestInputStream(new FileInputStream(file), MessageDigest.getInstance("MD5")); while (dis.read() != -1) { } dis.close(); return new String(dis.getMessageDigest().digest()); } }
private String shaDigestForFile(String filename) throws IOException { DigestInputStream str = digestInputStream(inputStreamForFile(filename)); byte[] dummy = new byte[4096]; for (int i = 1; i > 0; i = str.read(dummy)); str.close(); return HexBin.bytesToString(str.getMessageDigest().digest()); }
private String shaDigestForFile(String filename) throws IOException { DigestInputStream str = digestInputStream(inputStreamForFile(filename)); byte[] dummy = new byte[4096]; for (int i = 1; i > 0; i = str.read(dummy)); str.close(); return HexBin.bytesToString(str.getMessageDigest().digest()); }
private String getDigest(InputStream is) throws Exception { DigestInputStream dis = new DigestInputStream(is, MessageDigest.getInstance("MD5")); while (dis.read() != -1) { } dis.close(); return new String(dis.getMessageDigest().digest()); } }