Create a checksum : Message Digest « Security « Java






Create a checksum

     
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);

  }
}

   
    
    
    
    
  








Related examples in the same category

1.MessageDigest with SHA-1
2.Operations to simplifiy common java.security.MessageDigest tasks.
3.Digest string
4.MD4 Digest
5.MD5 Digest
6.Implements the MD4 message digest algorithm in Java
7.This program computes the message digest of a file or the contents of a text area.This program computes the message digest of a file or the contents of a text area.
8.Returns the type MessageDigest of the input text with the input salt.
9.Message Digest Util
10.A directive is a parameter of the digest authentication process