C # obtain the MD5 and SHA1 files,

Source: Internet
Author: User

C # obtain the MD5 and SHA1 files,

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

This is the typical file MD5 verification, and a SHA1 is added by the way.

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

Its function is that we can perform an MD5 verification on the downloaded files after downloading the software to ensure that the files we obtain are the same as the files provided by the site.

So we need an MD5 verification tool, so we can find it on Baidu, but there is no need to mention the download site in China.

Therefore, as a programmer, write one by yourself.

 

The main requirement for analyzing this tool is

1. Find the file according to the path

2. Obtain MD5

3. Get SHA1

 

A simple tool,

The first method directly uses the FileInfo class constructor to input the path, because the console can drag and drop files directly, which is very convenient.

Note that an error is reported if there are spaces in the path.

In this method, s represents the input 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 access 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 gets the MD5 value. In this method, s represents the input file path.

MD5 treats the entire file as a large text, and generates this unique MD5 information digest through its irreversible String Conversion Algorithm.

 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("文件MD5:{0}",sc);
16             }
17             catch(Exception ex)
18             {
19                 Console.WriteLine(ex.Message);
20             }
21         }

The third method obtains the SHA1 value. In this method, s represents the input file path.

The SHA1 name is a secure hash algorithm. For messages with a length less than 2 ^ 64 bits, SHA1 generates a 160-bit message digest.

SHA1 has the following features: it cannot recover information from the message digest. Two different messages do not produce the same message digest, (However, there is a probability that the same message digest will appear at one of 1x10 ^ 48, which is generally ignored in use ).

 11 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("文件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


Related Article

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.