Message Digest Demo : MessageDigest « Security « Java Tutorial






import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;

public class MainClass {
  private static int BUFFER_SIZE = 32 * 1024;

  public static void main(String[] args) throws Exception {
    System.out.println("test.txt: " + md("text.txt"));
  }

  public static String md(String f) throws Exception {
    BufferedInputStream file = new BufferedInputStream(new FileInputStream(f));
    MessageDigest md = MessageDigest.getInstance("MD5");
    DigestInputStream in = new DigestInputStream(file, md);
    int i;
    byte[] buffer = new byte[BUFFER_SIZE];
    do {
      i = in.read(buffer, 0, BUFFER_SIZE);
    } while (i == BUFFER_SIZE);
    md = in.getMessageDigest();
    in.close();

    return new String(md.digest());
  }
}








36.27.MessageDigest
36.27.1.Message Digest Demo
36.27.2.Digest Stream
36.27.3.Make SHA Message
36.27.4.Make SHA Message with update methods
36.27.5.Masher a file
36.27.6.extends MessageDigest
36.27.7.MD5 MessageDigest
36.27.8.Digest Input
36.27.9.Create an encrypted string for password
36.27.10.Cryptography Streams: URLDigest