/* (C) 2012 Pragmatic Software
   This Source Code Form is subject to the terms of the Mozilla Public
   License, v. 2.0. If a copy of the MPL was not distributed with this
   file, You can obtain one at http://mozilla.org/MPL/2.0/
 */

package com.googlecode.networklog;

import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.math.BigInteger;

public class MD5Sum {
  public static String digestFile(File file) {
    MessageDigest md;
    InputStream is = null;

    try {
      md = MessageDigest.getInstance("MD5");
      is = new DigestInputStream(new FileInputStream(file), md);
      byte[] bytes = new byte[8192];
      while(is.read(bytes) > 0) {}
    } catch (java.security.NoSuchAlgorithmException nsa) {
      Log.e("NetworkLog", "MD5 algorithm not supported", nsa);
      return "";
    } catch (java.io.FileNotFoundException nfe) {
      Log.e("NetworkLog", "File not found", nfe);
      return "";
    } catch (java.io.IOException ioe) {
      Log.e("NetworkLog", "IO exception", ioe);
      return "";
    }  finally {
      try {
        is.close();
      } catch (java.io.IOException e) {
        e.printStackTrace();
      }
    }

    byte[] digest = md.digest();
    return new BigInteger(1, digest).toString(16);
  }
}

@@ -517,16 +517,532 @@
  static 
+MessageDigest md;%0A%0A  public static String digestString(String string) %7B%0A    if(string == null) %7B%0A      return %22%22;%0A    %7D%0A%0A    try %7B%0A      if(md == null) %7B%0A        md = MessageDigest.getInstance(%22MD5%22);%0A      %7D else %7B%0A        md.reset();%0A      %7D%0A      byte%5B%5D digest = md.digest(string.getBytes(%22UTF-8%22));%0A      return new BigInteger(1, digest).toString(16);%0A    %7D catch (Exception e) %7B%0A      Log.e(%22NetworkLog%22, %22Exception getting md5sum for string %5C%22%22 + string + %22%5C%22%22, e);%0A      return %22%22;%0A    %7D%0A  %7D%0A%0A  public static 
 String d
@@ -1067,30 +1067,8 @@
 e) %7B
-%0A    MessageDigest md;
 %0A   
@@ -1098,24 +1098,49 @@
 %0A%0A    try %7B%0A
+      if(md == null) %7B%0A  
       md = M
@@ -1168,24 +1168,67 @@
 nce(%22MD5%22);%0A
+      %7D else %7B%0A        md.reset();%0A      %7D%0A
       is = n

Add method to md5sum a string; reuse MessageDigest object