Network computing environment in a lot of time need to file a message digest calculation, to prevent file tampering, verify whether the file has been modified, the JDK itself provides a message digest algorithm, very convenient to use, mainly used in Security digest calculation class
Java.security.MessageDigest, as follows:
Package com.test.b;
Import Java.io.BufferedInputStream;
Import Java.io.FileInputStream;
Import Java.io.InputStream;
Import Java.security.MessageDigest; public class Messagedigesttest {/** * @param args * @throws Exception/public static void main (string[] args) throws Exception {String file = "Mm.txt";
The file is commessdigest (file, "SHA-1") for the required summary calculation;
Commessdigest (file, "MD5"); /** * * @param name filename * @param MD Digest algorithm * @throws Exception/public Stati c void Commessdigest (String name,string md) throws exception{MessageDigest digest = messagedigest.getinstance (MD)
;
InputStream in = new Bufferedinputstream (new FileInputStream (name));
int ch;
while ((ch = in.read ())!=-1) {//update digest with the specified byte Digest.update ((byte) ch);
} in.close ();
Complete hash summary calculation byte[] hash = digest.digest ();
Printresult (hash); /** * Print Summary results * @param scoure */public static void Printresult (byte[) scoure) {St
Ringbuilder builder = new StringBuilder ();
for (byte c:scoure) {builder.append (int) c);
} System.out.println (Builder.tostring ());
}
}
The results are as follows:
682289988-127418543-1277977120-372-125-36-5364105
-96-19-641210641-72-77-6282-8-67-611221005