Development and problems of file verification tools and development of verification tools

Source: Internet
Author: User

Development and problems of file verification tools and development of verification tools

Development and problems of file verification tools

1. Familiar with encryption algorithms

Currently, file verification uses the MD5 value and SHA1 value of the file, and CRC32 is used. Microsoft released the Visual Studio official version, win10 image, the verification method officially provided by Microsoft is to verify the SHA1 value of the file.

C # The implementation of MD5 encryption and SHA encryption is summarized here

 

2. Encrypted File computing

It is not enough to know how to encrypt common strings. We need to verify the MD5 value or SHA1 value of the file. Next we will be familiar with how to obtain the MD5 value and SHA1 value of the file.

 

  • Obtain the MD5 value of an object.

    

 1         public static string GetFileMD5(string filePath) 2         { 3             MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); 4             FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); 5             byte[] result = md5.ComputeHash(fs); 6             md5.Clear(); 7             StringBuilder sb = new StringBuilder(32); 8             for (int i = 0; i < result.Length; i++) 9             {10                 sb.Append(result[i].ToString("X2"));11             }12             return sb.ToString();13         }

 

 

 

  • Get the SHA1 value of the file

 

 1         public static string GetFileSHA1(string filePath) 2         { 3             SHA1 sha1 = new SHA1CryptoServiceProvider(); 4             FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); 5             byte[] result = sha1.ComputeHash(fs); 6             sha1.Clear(); 7             StringBuilder sb = new StringBuilder(32); 8             for (int i = 0; i < result.Length; i++) 9             {10                 sb.Append(result[i].ToString("X2"));11             }12             return sb.ToString();13         }

 

 

 

 

3. file encryption Data Structure Optimization

Many methods are repeated in most code. How can we make the code more streamlined? Refactoring to improve the code reuse rate. The first thought was to build a base class, and other specific implementations inherited it. However, it was difficult to create several classes and finally decided, create a class and use the simplest refactoring and encapsulation method.

The final encryption help class implementation code is as follows:

 

 

 1     public static class ValidHelper 2     { 3         public static string GetFileHash(string filePath, HashAlgorithm algorithm) 4         { 5             FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); 6             byte[] result = algorithm.ComputeHash(fs); 7             algorithm.Clear(); 8             StringBuilder sb = new StringBuilder(32); 9             for (int i = 0; i < result.Length; i++)10             {11                 sb.Append(result[i].ToString("X2"));12             }13             return sb.ToString();14         }15 16         public static string GetFileMD5(string filePath)17         {18             MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();19             return GetFileHash(filePath, md5);20         }21 22         public static string GetFileSHA1(string filePath)23         {24             SHA1 sha1 = new SHA1CryptoServiceProvider();25             return GetFileHash(filePath, sha1);26         }27 28         public static string GetFileSHA256(string filePath)29         {30             SHA256 sha256 = SHA256.Create();31             return GetFileHash(filePath, sha256);32         }33 34         public static string GetFileSHA384(string filePath)35         {36             SHA384 sha384 = SHA384.Create();37             return GetFileHash(filePath, sha384);38         }39 40         public static string GetFileSHA512(string filePath)41         {42             SHA512 sha512 = SHA512.Create();43             return GetFileHash(filePath, sha512);44         }45     }

 

4. Test and Analysis after completion

After the code is complete, it is a test. Find a file verification tool on the Internet (the verification tool in the soft media cube) for comparison, check whether your verification tool is consistent with the verification result of the soft media cube verification tool. After verification, there is no error. The small file that was initially tested was downloaded from a win10 system, when you use your own verification tool to verify whether the SHA1 value is consistent with the SHA1 value officially given by Microsoft, the tool will be stuck and the result will be returned after a long time. This shows that this tool is still to be optimized, especially for processing large files.

Search for information on the Internet and see this implementation idea. segment the file location in the memory. For example, divide the file into five segments and start computing at the same time in five threads, finally, the five calculated values are processed to obtain the SHA1 value or MD5 value of the entire file. However, I am dull and don't know how to implement it. I hope you can give me some guidance after seeing it. Thank you very much.

 

Download file verification tool

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.