Create a checksum : Digest Stream « Security « Java Tutorial






import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;

public class Main {
  public static void main(String args[]) throws Exception {
    InputStream fis = new FileInputStream("a.exe");

    byte[] buffer = new byte[1024];
    MessageDigest complete = MessageDigest.getInstance("MD5");
    int numRead;
    do {
      numRead = fis.read(buffer);
      if (numRead > 0) {
        complete.update(buffer, 0, numRead);
      }
    } while (numRead != -1);
    fis.close();
    
    byte[] b = complete.digest();
    String result = "";
    for (int i = 0; i < b.length; i++) {
      result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
    }
    System.out.println(result);

  }
}








36.15.Digest Stream
36.15.1.Create a checksum
36.15.2.Cryptography Streams: True Mirror
36.15.3.Using DigestInputStream