In the network computing environment, message digest computing is often performed on files to prevent file tampering and check whether the file has been modified. JDK provides the message digest algorithm, which is quite convenient to use, mainly used in Security summary computing
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 "; // This file is the file for Digest Calculation. commessdigest (file, "SHA-1"); commessdigest (file, "MD5");}/***** @ Param name Name * @ Param MD Digest algorithm * @ throws exception */public static 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 in specified bytes. update (byte) CH);} In. close (); // calculate byte [] hash = digest. digest (); printresult (hash);}/*** print Summary Result * @ Param scoure */public static void printresult (byte [] scoure) {stringbuilder builder = new stringbuilder (); For (byte C: scoure) {builder. append (INT) C);} system. out. println (builder. tostring ());}}
The result is as follows:
682289988-127418543-1277977120-372-125-36-5364105-96-19-641210641-72-77-6282-8-67-611221005