Java COMPUTE file MD5

Source: Internet
Author: User

public class Md5check {/** * default password string combination, used to convert bytes to 16 binary representation of the character, Apache verify the correctness of the downloaded file is the default combination of this */protected char hexdigits[     ] = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f '}; protected MessageDigest messagedigest = null;

    {        try {             messagedigest = messagedigest.getinstance ("MD5");         catch (NoSuchAlgorithmException e) {             E.printstacktrace ();        }    }     public String getfilemd5string ( File file) throws IOException {        InputStream fis;     & nbsp;   fis = new FileInputStream (file);         byte[] buffer = new byte[1024];         int numread = 0;         while ((Numread = fis.read (buffer)) > 0) {             messagedigest.update (buffer, 0, numread);        }         fis.close ();         return Buffertohex (Messagedigest.digest ());    }

    public String getfilemd5string (InputStream in) throws IOException {     & nbsp;  byte[] buffer = new byte[1024];         int numread = 0;         while ((Numread = in.read (buffer)) > 0) {             messagedigest.update (buffer, 0, numread);        }         in.close ();         return Buffertohex (Messagedigest.digest ());    }     Private String Buffertohex (byte bytes[]) {         return Buffertohex (bytes, 0, bytes.length);    }     Private String Buffertohex (byte bytes[], int m, int n) {    & nbsp;   StringBuffer stringbuffer = new StringBuffer (2 * n);         int k = m + N;         for (int l = m; l < K; l++) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NB sp;    Appendhexpair (Bytes[l], stringbuffer);        }         return Stringbuffer.tostring ();    }     private void Appendhexpair (Byte bt, StringBuffer StringBuffer) {  & nbsp;     char C0 = hexdigits[(BT & 0xf0) >> 4];//take byte high 4-bit digital conversion &NBSP;&NBSP;&NBSP;&NBSP;&N bsp;  //For logical right SHIFT, move the sign bit right together, there is no difference between the two symbols found here         char C1 = HEXDIGITS[BT & 0xf];//4-bit digital conversion         stringbuffer.append (C0) in bytes;         Stringbuffer.append (C1);    }}

Java COMPUTE file MD5

Related Article

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.