C # Calculate the MD5 value of a file

Source: Internet
Author: User

MD5 is message digest algorithm 5 (Information AbstractAlgorithm(MD5) is a Hash technology that is widely used in encryption, decryption, Data signature, and data integrity verification. Any file, whether it is executableProgramImage files, temporary files, or other types of files, regardless of their size, can calculate an MD5 value. If the file has been modified, only one byte is changed, the MD5 value will also be completely different. Therefore, we can verify whether the file has been "Tampered" by comparing the MD5 value of the same file.

C # the MD5 value of the file can be conveniently calculated:

Calculate the MD5 value of a file
///   <Summary>
/// Calculate the MD5 value of a file
///   </Summary>
///   <Param name = "FILENAME"> Name and path of the file to calculate the MD5 Value </Param>
///   <Returns> MD5 value: A hexadecimal string </Returns>
Public   Static   String Md5file ( String Filename)
{
Return Hashfile (filename, " MD5 " );
}

///   <Summary>
/// Calculate the file hash value
///   </Summary>
///   <Param name = "FILENAME"> Name and path of the file to calculate the hash value </Param>
///   <Param name = "algname"> Algorithm: sha1, MD5 </Param>
///   <Returns> Hash Value: A hexadecimal string </Returns>
Public   Static   String Hashfile ( String Filename, String Algname)
{
If ( ! System. Io. file. exists (filename ))
Return   String . Empty;

Filestream FS= NewFilestream (filename, filemode. Open, fileaccess. Read );
Byte[] Hashbytes=Hashdata (FS, algname );
FS. Close ();
ReturnBytearraytohexstring (hashbytes );
}

///   <Summary>
/// Calculate the hash value
///   </Summary>
///   <Param name = "stream"> Stream to calculate the hash value </Param>
///   <Param name = "algname"> Algorithm: sha1, MD5 </Param>
///   <Returns> Hash Value byte array </Returns>
Public   Static   Byte [] Hashdata (Stream stream, String Algname)
{
Hashalgorithm algorithm;
If (Algname =   Null )
{
Throw   New Argumentnullexception ( " Algname cannot be null " );
}
If ( String . Compare (algname, " Sha1 " , True ) =   0 )
{
Algorithm = Sha1.create ();
}
Else
{
If ( String . Compare (algname, " MD5 " , True ) ! =   0 )
{
Throw   New Exception ( " Algname can only use sha1 or MD5 " );
}
Algorithm = Md5.create ();
}
Return Algorithm. computehash (Stream );
}

The computehash method returns a hash value byte array, while the MD5 value of a file is usually represented by a hexadecimal string. The byte array is converted to a hexadecimal string in system. web. configuration. the machinekeysection provides a method named bytearraytohexstring, but this method is nonpublic and cannot be called directly. Here we call the bytearraytohexstring method in machinekeysection through reflection.

Call the bytearraytohexstring method in machinekeysection through reflection:

Call the bytearraytohexstring method in machinekeysection through reflection
///   <Summary>
/// Byte array to a hexadecimal string
///   </Summary>
Public   Static   String Bytearraytohexstring ( Byte [] BUF)
{
Int Ilen =   0 ;

// obtain the bytearraytohexstring method in machinekeysection through reflection, this method is used to convert a byte array to a hex string.
type = typeof (system. web. configuration. machinekeysection);
methodinfo bytearraytohexstring = type. getmethod ( " bytearraytohexstring " , bindingflags. static | bindingflags. nonpublic);

//Byte array to a hexadecimal string
Return(String) Bytearraytohexstring. Invoke (Null,New Object[] {Buf, ilen });
}

Now the emergence of the "MD5 collision generator" makes MD5 also face challenges.

Md5checker is a free, fast, small, and easy-to-use file MD5 value calculation, verification, and management software. The md5checker official website is: http://cn.getmd5checker.com/

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.