C# Get Files MD5 and SHA1

Keywords: C# SHA1 Programming network less

When I first started learning programming, I always wanted to write some small software gadgets by myself.

And this is the classic file MD5 check, by the way, with a SHA1.

When downloading something on the network, the author will provide the MD5 value.

Its function is that after downloading the software, we can do an MD5 check on the downloaded files to ensure that the files we get are the same files as those provided by the site.

So you need a MD5 checking tool, then go to Baidu, but the domestic download stations need not say.......... bundled things can not be prevented.

So, as a programmer, write one by yourself.

 

The main requirement for analyzing this gadget is

1. Find the file according to the path

2. get MD5

3. get SHA1

 

A very simple tool.

The first method simply uses the FileInfo class constructor to pass in the path, because it's convenient to do the console and drag files directly.

It should be noted that there are spaces in the path that will cause errors.

In this method s represents the incoming file path

 1 static void GetFile(string s)
 2         {
 3             try
 4             {
 5                 FileInfo fi = new FileInfo(s);
 6                 Console.WriteLine("File path:{0}", s);
 7                 Console.WriteLine("File name:{0}", fi.Name.ToString());
 8                 Console.WriteLine("File type:{0}", fi.Extension.ToString());
 9                 Console.WriteLine("File size:{0} K", fi.Length / 1024);
10                 Console.WriteLine("File creation time:{0}", fi.CreationTime.ToString());
11                 Console.WriteLine("Last visit time:{0}", fi.LastAccessTime.ToString());
12                 Console.WriteLine("Last write time:{0}", fi.LastWriteTime.ToString());
13             }
14             catch (Exception ex) 
15             {
16                 Console.WriteLine(ex.Message);
17             }
18         }

The second method obtains the MD5 value, in which s represents the incoming file path

MD5 regards the whole file as a large text information. Through its irreversible string transformation algorithm, it generates this unique MD5 information summary.

 1 static void GetMD5(string s)
 2         {
 3             try
 4             {
 5                 FileStream file = new FileStream(s,FileMode.Open);
 6                 MD5 md5 = new MD5CryptoServiceProvider();
 7                 byte[] retval = md5.ComputeHash(file);
 8                 file.Close();
 9 
10                 StringBuilder sc = new StringBuilder();
11                 for (int i = 0 ; i<retval.Length ; i++ )
12                 {
13                     sc.Append(retval[i].ToString("x2"));
14                 }
15                 Console.WriteLine("file MD5: {0}",sc);
16             }
17             catch(Exception ex)
18             {
19                 Console.WriteLine(ex.Message);
20             }
21         }

The third method obtains the SHA1 value, in which s represents the incoming file path

SHA1 is called a secure hashing algorithm. For messages less than 2 ^ 64 bits in length, SHA1 produces a 160-bit message digest.

SHA1 has the following characteristics: it is not possible to recover information from message digests; two different messages do not produce the same message digest (but there is a 1 x 10 ^ 48 chance that the same message digest will occur, which is generally ignored in use).

 1 static void GetSHA1(string s)
 2         {
 3             try
 4             {
 5                 FileStream file = new FileStream(s, FileMode.Open);
 6                 SHA1 sha1 = new SHA1CryptoServiceProvider();
 7                 byte[] retval = sha1.ComputeHash(file);
 8                 file.Close();
 9 
10                 StringBuilder sc = new StringBuilder();
11                 for (int i = 0; i < retval.Length; i++)
12                 {
13                     sc.Append(retval[i].ToString("x2"));
14                 }
15                 Console.WriteLine("file SHA1: {0}", sc);
16             }
17             catch (Exception ex)
18             {
19                 Console.WriteLine(ex.Message);
20             }
21         }

 

Release my finished product with. NET Framework 4.0, which should be available on all computers.

(Password: XVi7MD)

https://share.weiyun.com/98d8c10869e693961fb0df10c9202624

 

Reprinted please contact

Posted by samusk on Sat, 30 Mar 2019 09:03:28 -0700