Java implementation Calculation MD5

Source: Internet
Author: User
Tags bitwise

ImportJava.io.FileInputStream;ImportJava.security.DigestInputStream;Importjava.security.MessageDigest;Importjava.security.NoSuchAlgorithmException; Public classMd5class {//computes the MD5 of a string     Public Staticstring conVertTextToMD5 (string plaintext) {Try{messagedigest MD= Messagedigest.getinstance ("MD5");            Md.update (Plaintext.getbytes ()); byteB[] =md.digest (); inti; StringBuffer buf=NewStringBuffer ("");  for(intoffset = 0; Offset < b.length; offset++) {i=B[offset]; if(I < 0) I+ = 256; if(I < 16) Buf.append ("0");            Buf.append (integer.tohexstring (i)); }            //32-bit encryption            returnbuf.tostring (); //16-bit encryption//return buf.tostring (). substring (8);}Catch(nosuchalgorithmexception e) {e.printstacktrace (); return NULL; }    }
Calculate file MD5, support 4G file (file Pro test, large file not pro test) Public Staticstring conVertFileToMD5 (String inputfilepath) {intbuffersize = 256 * 1024; FileInputStream FileInputStream=NULL; Digestinputstream Digestinputstream=NULL; Try { //Get a MD5 converter (again, here you can switch to SHA1)messagedigest messagedigest= Messagedigest.getinstance ("MD5"); //using DigestinputstreamFileInputStream=NewFileInputStream (Inputfilepath); Digestinputstream=NewDigestinputstream (FileInputStream, messagedigest); //MD5 processing During the read process until the file is finished byte[] buffer =New byte[buffersize]; while(Digestinputstream.read (buffer) > 0) ; //get the final messagedigestMessageDigest=digestinputstream.getmessagedigest (); //Get the result, also a byte array, containing 16 elements byte[] Resultbytearray =messagedigest.digest (); //Similarly, converting a byte array to a string returnBytearraytohex (Resultbytearray); } Catch(Exception e) {return NULL; } finally { Try{digestinputstream.close (); } Catch(Exception e) {}Try{fileinputstream.close (); } Catch(Exception e) {}}} Private StaticString Bytearraytohex (byte[] byteArray) { //First initialize a character array to hold each 16-character Char[] hexdigits = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ' }; //new A character array, which is used to compose the result string (explanation: A byte is a eight-bit binary, which is the 2-bit hexadecimal character (2 of 8 is equal to 16 of the 2-second party)) Char[] Resultchararray =New Char[Bytearray.length * 2]; //iterates through byte arrays, converts characters into character arrays by bitwise operations (high efficiency of bitwise operations) intindex = 0; for(byteB:bytearray) {Resultchararray[index+ +] = hexdigits[b >>> 4 & 0xf]; Resultchararray[index+ +] = hexdigits[b & 0xf]; } //character array group composition string return return NewString (Resultchararray); } Public Static voidMain (string[] args) {//TestSystem.out.println (md5class.converttexttomd5 ("Hello")); System.out. println (ConVertFileToMD5 ("C:\\users\\administrator1\\downloads\\staruml-v2.8.0.msi")); }}

Java implementation Calculation MD5

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.