code: public static byte[] getHash( String digestName,
                                    InputStream stream ) throws NoSuchAlgorithmException, IOException {
          CheckArg.isNotNull(stream, "stream");
          MessageDigest digest = MessageDigest.getInstance(digestName);
          assert digest != null;
          int bufSize = 1024;
          byte[] buffer = new byte[bufSize];
          int n = stream.read(buffer, 0, bufSize);
          while (n != -1) {
              digest.update(buffer, 0, n);
              n = stream.read(buffer, 0, bufSize);
          }
          return digest.digest();
      }
docs: Get the hash of the supplied content, using the digest identified by the supplied name. Note that this method never closes
  the supplied stream.
  
  @param digestName the name of the hashing function (or {@link MessageDigest message digest}) that should be used
  @param stream the stream containing the content to be hashed; may not be null
  @return the hash of the contents as a byte array
  @throws NoSuchAlgorithmException if the supplied algorithm could not be found
  @throws IOException if there is an error reading the stream