title: MD5 checksum from input stream
body: <p>I'm trying to get the md5 sum of an input stream but the string I get is encrypted incorrectly.</p>
  
  <p>The md5 string I'm getting is: <code>ä?E´]Õaá*TàŠöJ</code></p>
  
  <p>When it should be: <code>e48f0b45b45dd56102e12a54e08af64a</code></p>
  
  <p>Can you spot what I'm doing wrong? Here is a working program:</p>
  
  <pre><code>import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.security.MessageDigest;
  import java.security.NoSuchAlgorithmException;
  
  public class main {
  
  public static void main(String[] args) throws IOException {
  
      byte[] fileContents = new byte[15 * 10000000];
  
      FileOutputStream out = new FileOutputStream("C:\\testFile");
      out.write(fileContents);
      out.close();
  
      File file = new File("C:\\testFile");
      FileInputStream fs = new FileInputStream(file);
  
      System.out.println(new String(getBytesOfMd5(fs)));
  
  }
  
  public static byte[] getBytesOfMd5(InputStream is) throws IOException {
      byte[] buffer = new byte[1024];
      MessageDigest complete = null;
      try {
          complete = MessageDigest.getInstance("MD5");
      } catch (NoSuchAlgorithmException e) {
          return null;
      }
  
      int numRead;
      do {
          numRead = is.read(buffer);
          if (numRead &gt; 0) {
              complete.update(buffer, 0, numRead);
          }
      } while (numRead != -1);
  
      is.close();
      return complete.digest();
  }
  }
  </code></pre>