Evaluate the file MD5 value in Java

Source: Internet
Author: User
Import Java. io. fileinputstream; import Java. io. ioexception; import Java. security. digestinputstream; import Java. security. messagedigest; import Java. security. nosuchalgorithmexception; public class filemd5 {public static void main (string [] ARGs) {long starttime = system. currenttimemillis (); try {system. out. println (filemd5 ("F: \ tddownload \ 123.rar"); // bdeae194a93c186b7b42a7a6425b6310} catch (ioexception e) {e. printstacktrace ();} Long endtime = system. currenttimemillis (); system. out. println (endtime-starttime)/1000);} public static string filemd5 (string inputfile) throws ioexception {// buffer size (this can extract a parameter) int buffersize = 256*1024; fileinputstream = NULL; digestinputstream = NULL; try {// get an MD5 converter (likewise, you can change it to sha1) messagedigest = messagedigest. getinstance ("MD5"); // use digestinputstreamfileinputstream = new fileinputstream (inputfile); digestinputstream = new digestinputstream (fileinputstream, messagedigest); // perform MD5 processing during read, until the byte [] buffer = new byte [buffersize]; while (digestinputstream. read (buffer)> 0); // obtain the final messagedigestmessagedigest = digestinputstream. getmessagedigest (); // get the result, which is also a byte array containing 16 elements, byte [] resultbytearray = messagedigest. digest (); // Similarly, convert the byte array to the string return bytearraytohex (resultbytearray);} catch (nosuchalgorithmexception e) {return NULL;} finally {try {digestinputstream. close ();} catch (exception e) {e. printstacktrace ();} Try {fileinputstream. close ();} catch (exception e) {e. printstacktrace () ;}}// the following function is used to convert the byte array into a hexadecimal string public static string bytearraytohex (byte [] bytearray) {// Initialize an array of characters to store each hexadecimal char [] hexdigits = {'0', '1', '2', '3 ', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D ', 'E', 'F'}; // a new character array, which is used to form the result string (to explain: A byte is an eight-bit binary, that is, two hexadecimal characters (the power of 2 is equal to the power of 16) Char [] resultchararray = new char [bytearray. length * 2]; // traverses the byte array. bitwise operations are performed (bit operation efficiency is high) to convert it into characters and place them in the character array. Int Index = 0; For (byte B: bytearray) {resultchararray [index ++] = hexdigits [B >>> 4 & 0xf]; resultchararray [index ++] = hexdigits [B & 0xf];} // returns return new string (resultchararray) when combining character arrays into strings );}}

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.