C # and Java MD5 Simple validation (instance code) _c# Tutorial

Source: Internet
Author: User

C # end

Using System;
Using System.IO;

Using System.Security.Cryptography; Namespace the MD5 value of the computed file {class Md5_helper {///<summary>///file MD5 checksum///</summary>///< param name= "pathName" > File absolute path </param>///&LT;RETURNS&GT;MD5 checksum Code </returns> public string Getmd5hash (s
      Tring pathName) {string strresult = "";
      String strhashdata = "";
      Byte[] Arrbythashvalue;
      FileStream ofilestream = null;
      MD5CryptoServiceProvider omd5hasher = new MD5CryptoServiceProvider ();
        try {ofilestream = new FileStream (PathName, FileMode.Open, FileAccess.Read, fileshare.readwrite);
        Arrbythashvalue = Omd5hasher.computehash (Ofilestream);//Compute the hash value of the specified Stream object Ofilestream.close ();
        A string of hexadecimal pairs separated by hyphens in which each pair represents the corresponding element in value, such as "f-2c-4a" Strhashdata = bitconverter.tostring (Arrbythashvalue);
        Replacement-Strhashdata = Strhashdata.replace ("-", ""); strresult = StrhashData;

    The catch (System.Exception ex) {} return strresult;
    ///<summary>///byte array checksum///</summary>///<param name= "buffer" > to byte array </param> &LT;RETURNS&GT;MD5 Check Codes </returns> public string Getmd5hash (byte[] Buffer {string strresult = "
      ";
      String strhashdata = "";
      Byte[] Arrbythashvalue;
      MD5CryptoServiceProvider omd5hasher = new MD5CryptoServiceProvider (); try {arrbythashvalue = omd5hasher.computehash (buffer);//Compute the hash value//string of a specified stream object, consisting of a hexadecimal pair separated by hyphens,
        Each pair represents the corresponding element in value, such as "f-2c-4a" Strhashdata = bitconverter.tostring (Arrbythashvalue);
        Replacement-Strhashdata = Strhashdata.replace ("-", "");
      strresult = Strhashdata;

    The 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 16 characters, the correctness of the Apache download file is the default of this combination * * * protected static char He
	Xdigits[] = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '};
	protected static MessageDigest 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 checksum St
		Ring MD55 = getfilemd5string (buffer);
	System.out.println ("md55:" + md55);
		public static String getfilemd5string (file file) throws IOException {InputStream fis;
		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 static String getfilemd5string (byte[] buffer) throws IOException {messagedigest.update (buffer, 0, BUFFER.L
		Ength);
	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 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 stringbuffer) {char C0 = hexdigits[(BT & 0xf0) >> 4];//bytes High 4 digits conversion//to the logical right, move the symbol bit to the right, where there is no difference between the two symbols char C1 = HEXDIGITS[BT & 0xf];//byte low 4-bit number conversion Stringbuff
		Er.append (C0);
	Stringbuffer.append (C1); 


 }
}

The above is a small series for everyone to bring the C # and Java MD5 Simple verification (instance code) of the entire content, I hope to help you, a lot of support cloud Habitat Community ~

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.