title: computing checksum for an input stream
body: <p>I need to compute checksum for an inputstream(or a file) to check if the file contents are changed. I have this below code that generates a different value for each execution though I'm using the same stream. Can someone help me to do this right?</p>
  
  <pre><code>public class CreateChecksum {
      public static void main(String args[]) {
          String test = "Hello world";
          ByteArrayInputStream bis = new ByteArrayInputStream(test.getBytes());
          System.out.println("MD5 checksum for file using Java : "    + checkSum(bis));
          System.out.println("MD5 checksum for file using Java : "    + checkSum(bis));
      }
      public static String checkSum(InputStream fis){
          String checksum = null;
          try {
              MessageDigest md = MessageDigest.getInstance("MD5");
              //Using MessageDigest update() method to provide input
              byte[] buffer = new byte[8192];
              int numOfBytesRead;
              while( (numOfBytesRead = fis.read(buffer)) &gt; 0){
                  md.update(buffer, 0, numOfBytesRead);
              }
              byte[] hash = md.digest();
              checksum = new BigInteger(1, hash).toString(16); //don't use this, truncates leading zero
          } catch (Exception ex) {                  
          }
         return checksum;
      }
  }
  </code></pre>