This paper introduces the development and problems of C document verification tool in detail

  • 2021-07-18 08:51:27
  • OfStack

Familiarity with encryption algorithms
At present, MD value and SHA value are most used in verification files, except that some use CRC. Some time ago, Microsoft released the official version of VisualStudio and win image, and the verification methods given by Microsoft are all SHA value of verification files.

The implementation of C # for MD encryption and SHA encryption is summarized here
Encryption calculation of files
It is not enough to know how to encrypt ordinary strings. We want to check the MD value or SHA value of the file. Next, we are familiar with how to get the MD value and SHA value of the file
Gets the MD value of the file


public static string GetFileMD(string filePath)
     {
       MDCryptoServiceProvider md = new MDCryptoServiceProvider();
       FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
       byte[] result = md.ComputeHash(fs);
       md.Clear();
       StringBuilder sb = new StringBuilder();
       for (int i = ; i < result.Length; i++)
       {
         sb.Append(result[i].ToString("X"));
       }
       return sb.ToString();
     }


Gets the SHA value of the file


 public static string GetFileSHA(string filePath)
     {
       SHA sha = new SHACryptoServiceProvider();
       FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
       byte[] result = sha.ComputeHash(fs);
       sha.Clear();
       StringBuilder sb = new StringBuilder();
       for (int i = ; i < result.Length; i++)
       {
         sb.Append(result[i].ToString("X"));
       }
       return sb.ToString();
     }

File encryption data structure optimization
So many methods, there are many methods, most of the code is repetitive, how to make the code more concise? Refactoring to improve the reuse rate of code, the initial idea is to build a base class, and other concrete implementations are inheriting it, but they feel very troublesome. They need to build several new classes, and finally decide to build a class with the simplest refactoring and encapsulation methods.
The final encryption helper class implementation code is as follows:

public static class ValidHelper
   {
     public static string GetFileHash(string filePath, HashAlgorithm algorithm)
     {
       FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
       byte[] result = algorithm.ComputeHash(fs);
       algorithm.Clear();
       StringBuilder sb = new StringBuilder();
       for (int i = ; i < result.Length; i++)
       {
         sb.Append(result[i].ToString("X"));
       }
       return sb.ToString();
     }
 
     public static string GetFileMD(string filePath)
     {
       MDCryptoServiceProvider md = new MDCryptoServiceProvider();
       return GetFileHash(filePath, md);
     }
 
     public static string GetFileSHA(string filePath)
     {
       SHA sha = new SHACryptoServiceProvider();
       return GetFileHash(filePath, sha);
     }
 
     public static string GetFileSHA(string filePath)
     {
       SHA sha = SHA.Create();
       return GetFileHash(filePath, sha);
     }
 
     public static string GetFileSHA(string filePath)
     {
       SHA sha = SHA.Create();
       return GetFileHash(filePath, sha);
     }
 
     public static string GetFileSHA(string filePath)
     {
       SHA sha = SHA.Create();
       return GetFileHash(filePath, sha);
     }
   }

Test and analysis after completion
After the code is finished, it is tested. Find a file verification tool on the Internet (the verification tool in the soft media Rubik's Cube), Make a comparison, See if the verification result of your own verification tool and the verification tool of Soft Media Rubik's Cube is 1, After verification, there is no error. After the initial test of the small file, I downloaded an win system. When I used my own verification tool to check whether it was equal to the SHA value given by Microsoft, the small tool was directly stuck, and it took a long time to come out, which shows that this small tool needs to be optimized, especially for dealing with large files.
Looking for information on the Internet, I saw such a realization idea, segmenting the position of the file in memory, for example, dividing into segments, starting calculation at the same time by a thread, and finally processing the calculated value to get the SHA value or MD value of the whole file. However, I am dull and don't know how to realize it. I hope God can give some guidance after seeing it. Thank you very much.

The above introduction is the whole content of this article, and I hope you like it.


Related articles: