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 :)