MD5 hash calculation tool

Source: Internet
Author: User
Tags md5 hash

MD5 is equivalent to a file fingerprint. It is useful to compare whether a file is modified. This technology is used for common file upgrades and virus detection.

RealityCodeAs follows:

 Using System; Using System. Collections. Generic; Using System. text; Using System. Security. cryptography; Namespace Codelib. Security { /// <Summary> ///  MD5 hash calculation tool class, which can calculate hash values from strings and files.  ///  Supports MD5 and sha1AlgorithmSpecifies the case sensitivity.  /// </Summary> Public sealed class  Md5utils { Public String Getmd5fromstring ( String Value ){ Return Getmd5fromstring (value, False );} Public String Getmd5fromstring ( String Value, Bool Returnlowercase ){ MD5 MD5 = MD5 . Create ();Byte [] DATA = md5.computehash (system. Text. Encoding . Default. getbytes (value )); Stringbuilder Sbresult = New  Stringbuilder (); For ( Int I = 0; I <data. length; I ++ ){ If (Returnlowercase) {sbresult. appendformat ( "{0: X2 }" , Data [I]);} Else {Sbresult. appendformat ( "{0: X2 }" , Data [I]) ;}} Return Sbresult. tostring ();} Public String Getmd5fromfile ( String Filename ){ Return Getmd5fromfile (filename, False );} Public String Getmd5fromfile ( String Filename, Bool Returnlowercase ){ Using (System. Io. Filestream Filestream = New System. Io. Filestream (Filename, system. Io. Filemode . Open, system. Io. Fileaccess . Read )){ MD5 MD5 = MD5 . Create (); Byte [] DATA = md5.computehash (filestream ); Stringbuilder Sbresult = New  Stringbuilder (); For ( Int I = 0; I <data. length; I ++ ){ If (Returnlowercase) {sbresult. appendformat ( "{0: X2 }" , Data [I]);} Else {Sbresult. appendformat ( "{0: X2 }" , Data [I]) ;}} Return Sbresult. tostring ();}} Public String Getsha1fromstring (String Value ){ Return Getsha1fromstring (value, False );} Public String Getsha1fromstring ( String Value, Bool Returnlowercase ){ Sha1 MD5 = Sha1 . Create (); Byte [] DATA = md5.computehash (system. Text. Encoding . Default. getbytes (value )); Stringbuilder Sbresult = New  Stringbuilder (); For ( Int I = 0; I <data. length; I ++ ){ If (Returnlowercase) {sbresult. appendformat ( "{0: X2 }" , Data [I]);} Else {Sbresult. appendformat ( "{0: X2 }" , Data [I]) ;}} Return Sbresult. tostring ();}Public String Getsha1fromfile ( String Filename ){ Return Getsha1fromfile (filename, False );} Public String Getsha1fromfile ( String Filename, Bool Returnlowercase ){ Using (System. Io. Filestream Filestream = New System. Io. Filestream (Filename, system. Io.Filemode . Open, system. Io. Fileaccess . Read )){ Sha1 MD5 = Sha1 . Create (); Byte [] DATA = md5.computehash (filestream ); Stringbuilder Sbresult = New  Stringbuilder (); For ( Int I = 0; I <data. length; I ++ ){ If (Returnlowercase) {sbresult. appendformat ( "{0: X2 }" , Data [I]);} Else {Sbresult. appendformat ( "{0: X2 }" , Data [I]) ;}} Return Sbresult. tostring ();}}}}

 

Test:

  ///  ///   A test for getmd5fromfile   //   [ testmethod  ()]  Public void  getmd5fromfiletest () { md5utils  Target =  New   md5utils  ();  string  filename =  @" C: \ WINDOWS \ notepad. EXE ";  string  expected =  string . empty;  string  actual; actual = target. getmd5fromfile (filename);  console . writeline (actual) ;}

 

Output:Ca94f7297b444ab655cd4b7793c02fd3

If this value is not output, it is estimated that it is a virus :)

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.