SHA1 the implementation of the file Request Information Digest

Source: Internet
Author: User
Tags sha1

{Feature Description}
can be used to analyze whether the SHA1 value of the downloaded file is equal to the SHA1 value provided online.

{Code Implementation}

 PackageSup.orange.learn;ImportJava.io.FileInputStream;Importjava.io.IOException;Importjava.security.MessageDigest;Importjava.security.NoSuchAlgorithmException;/*** Created by Re-x on 11/4/14.*/ Public classHashfiletest { Public Static voidMain (string[] args)throwsIOException, nosuchalgorithmexception {Booleanresult = Verifychecksum ("/home/re-x/downloads/linux-1.0.tar.bz2", "4b3de322714d911c8bb46ceda2dbf541e6ab24f4"); System.out.println ("Does the file ' s checksum matches the expected one?" +result); }    /*** Verifies file ' s SHA1 checksum. * @paramfile, filepath and namd of a file, is verified. * @paramTestchecksum, the expected checksum. * @returntrue if the expeceted SHA1 checksum matches the file ' s SHA1 checksum; false otherwise *@throwsjava.security.NoSuchAlgorithmException *@throwsjava.io.IOException*/     Public Static BooleanVerifychecksum (string file, String testchecksum)throwsnosuchalgorithmexception, IOException {messagedigest messagedigest= Messagedigest.getinstance ("SHA1"); FileInputStream FIS=Newfileinputstream (file); byte[] data =New byte[1024]; intRead = 0;  while(read = Fis.read (data))! =-1) {messagedigest.update (data,0, read); }        byte[] Hashbytes =messagedigest.digest (); StringBuffer SB=NewStringBuffer ();  for(inti = 0; i < hashbytes.length; i++) {sb.append (integer.tostring (hashbytes[i)& 0xff) + 0x100, +). SUBSTRING (1)); } String Filehash=sb.tostring (); System.out.println (File+ "SHA1 is:" +Filehash); returnfilehash.equals (testchecksum); }}

SHA1 the implementation of the file Request Information Digest

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.