JAVA Message Digest Useful __java

Source: Internet
Author: User

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




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.