C # simple MD5 verification with Java

Source: Internet
Author: User

C # End

Using system; using system. io; using system. security. cryptography; the MD5 value of the namespace calculation file {class md5_helper {// <summary> // file MD5 verification /// </Summary> /// <Param name = "pathname"> absolute file path </param> /// <returns> MD5 Verification Code </returns> Public String getmd5hash (string pathname) {string strresult = ""; string strhashdata = ""; byte [] arrbythashvalue; filestream ofilestream = NULL; md5cipher serviceprovider omd5hasher = new md5cryptoserviceprovider (); try {ofilestream = new filestream (pathname, filemode. open, fileaccess. read, fileshare. readwrite); arrbythashvalue = omd5hasher. computehash (ofilestream); // calculate the hash value of the specified stream object ofilestream. close (); // a string consisting of a hex pair separated by a hyphen, where each pair represents the corresponding element in the value; for example, "F-2C-4A" strhashdata = bitconverter. tostring (arrbythashvalue); // replace-strhashdata = strhashdata. replace ("-", ""); strresult = strhashdata;} catch (system. exception ex) {} return strresult ;} /// <summary> /// check the byte array /// </Summary> /// <Param name = "buffer"> waiting for byte array </param> /// <returns> MD5 Verification Code </returns> Public String getmd5hash (byte [] buffer) {string strresult = ""; string strhashdata = ""; byte [] arrbythashvalue; md5cryptoserviceprovider omd5hasher = new md5cryptoserviceprovider (); try {arrbythashvalue = omd5hasher. computehash (buffer); // calculate the hash value of a specified stream object // a string consisting of a hexadecimal pair separated by a hyphen, where each pair represents the corresponding element of the value; for example, "F-2C-4A" strhashdata = bitconverter. tostring (arrbythashvalue); // replace-strhashdata = strhashdata. replace ("-", ""); strresult = strhashdata;} catch (system. exception ex) {} return strresult ;}}}

Java end

Package com; import Java. io. file; import Java. io. fileinputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. security. messagedigest; import Java. security. nosuchalgorithmexception; public class md5util {/*** default password string combination, used to convert bytes into hexadecimal characters, apache verifies the correctness of downloaded files using the default combination */protected static char hexdigits [] = {'0', '1', '2', '3 ', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D ', 'E', 'F'}; protected static messagedigest = NULL; static {try {messagedigest = messagedigest. getinstance ("MD5");} catch (nosuchalgorithmexception e) {e. printstacktrace () ;}} public static void main (string [] ARGs) throws ioexception {byte [] buffer = "helloworld ". getbytes (); // byte array validation string md55 = getfilemd5string (buffer); system. out. println ("md55:" + md55);} public static string getfilemd5string (File file) throws ioexception {inputstream FS; FS = new fileinputstream (File ); byte [] buffer = new byte [1024]; int numread = 0; while (numread = Fi. read (buffer)> 0) {messagedigest. update (buffer, 0, numread);} FCM. close (); Return buffertohex (messagedigest. digest ();} public static string getfilemd5string (byte [] buffer) throws ioexception {messagedigest. update (buffer, 0, buffer. length); Return buffertohex (messagedigest. digest ();} Private Static string buffertohex (byte bytes []) {return buffertohex (bytes, 0, bytes. length);} Private Static string buffertohex (byte bytes [], int M, int N) {stringbuffer = new stringbuffer (2 * n); int K = m + N; for (int l = m; L <K; l ++) {appendhexpair (Bytes [L], stringbuffer);} return stringbuffer. tostring ();} Private Static void appendhexpair (byte BT, stringbuffer) {char C0 = hexdigits [(BT & 0xf0)> 4]; // convert the numbers with four or more characters in a byte to the right of the logic. Shift the symbols right together. No difference is found between the two symbols: Char C1 = hexdigits [BT & 0xf]; // convert the numbers in the middle or lower byte to stringbuffer. append (C0); stringbuffer. append (C1 );}}

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.