List of usage examples for java.security DigestInputStream available
public int available() throws IOException
From source file:com.github.nlloyd.hornofmongo.MongoScope.java
public static Object md5sumFile(Context cx, Scriptable thisObj, Object[] args, Function funObj) { assertSingleArgument(args);//from ww w .j ava 2s .c o m MessageDigest md = null; try { md = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { Context.throwAsScriptRuntimeEx(e); } File inFile; try { inFile = resolveFilePath((MongoScope) thisObj, Context.toString(args[0])).getCanonicalFile(); } catch (IOException e) { Context.throwAsScriptRuntimeEx(e); return null; } InputStream in = null; DigestInputStream dis = null; try { in = new BufferedInputStream(new FileInputStream(inFile)); dis = new DigestInputStream(in, md); while (dis.available() > 0) dis.read(); byte[] digest = md.digest(); String hexStr = Hex.encodeHexString(digest); return hexStr; } catch (FileNotFoundException e) { Context.throwAsScriptRuntimeEx(e); } catch (IOException e) { Context.throwAsScriptRuntimeEx(e); } finally { if (in != null) { try { in.close(); } catch (IOException e) { } } if (dis != null) { try { dis.close(); } catch (IOException e) { } } } return Undefined.instance; }
From source file:org.panbox.linux.desktop.identitymgmt.TestDownloadHashCompare.java
/** * Creates a Hash value for a given file * @param file/*from w w w . j a v a 2s . c o m*/ * @return */ private byte[] createFileHash(File file) { MessageDigest md = null; try (InputStream is = Files.newInputStream(Paths.get(file.getAbsolutePath()))) { md = MessageDigest.getInstance("SHA-256"); DigestInputStream dis = new DigestInputStream(is, md); /* Read stream to EOF as normal... */ while (dis.available() > 0) { dis.read(); } } catch (NoSuchAlgorithmException e) { fail(); } catch (IOException e1) { fail(); } byte[] digest = md.digest(); //String h = Hex.encodeHexString( digest ); return digest; }