Java Message Digest usage

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.