.Familiarity with encryption algorithms
Currently, the most commonly used verification files are MD values and SHA values, but some use CRC. Some time ago, Microsoft released the official version of Visual Studio, win mirroring, and the verification methods given by Microsoft are all the verification files.
The implementation of C# MD encryption and SHA encryption is summarized here
.Encryption calculation of files
It is not enough to know how to encrypt the normal string. We want to verify the MD value or SHA value of the file. Next, we are familiar with how to obtain the MD value and SHA value of the file.
Get the MD value of the file
public static string GetFileMD(string filePath) { MDCryptoServiceProvider md = new MDCryptoServiceProvider(); FileStream fs = new FileStream(filePath, , , ); byte[] result = (fs); (); StringBuilder sb = new StringBuilder(); for (int i = ; i < ; i++) { (result[i].ToString("X")); } return (); }
Get the SHA value of the file
public static string GetFileSHA(string filePath) { SHA sha = new SHACryptoServiceProvider(); FileStream fs = new FileStream(filePath, , , ); byte[] result = (fs); (); StringBuilder sb = new StringBuilder(); for (int i = ; i < ; i++) { (result[i].ToString("X")); } return (); }
.File encryption data structure optimization
There are so many methods, and most of the code is still repeated. How can I make the code more streamlined? Refactoring to improve the reuse rate of the code, the initial idea was to build a base class. Various other specific implementations were inheriting it, but they also felt it was very troublesome. They had to build several new classes. Finally, they decided to build a class and use the simplest reconstruction and encapsulation method.
The final encryption help class implementation code is as follows:
public static class ValidHelper { public static string GetFileHash(string filePath, HashAlgorithm algorithm) { FileStream fs = new FileStream(filePath, , , ); byte[] result = (fs); (); StringBuilder sb = new StringBuilder(); for (int i = ; i < ; i++) { (result[i].ToString("X")); } return (); } 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 = (); return GetFileHash(filePath, sha); } public static string GetFileSHA(string filePath) { SHA sha = (); return GetFileHash(filePath, sha); } public static string GetFileSHA(string filePath) { SHA sha = (); return GetFileHash(filePath, sha); } }
. Test and analysis after completion
After the code is completed, the test is taken. Find a file verification tool online (the verification tool in the soft media cube used) and compare it to see if your verification tool is consistent with the verification result of the soft media cube's verification tool. After verification, there is no error. The small file I initially tested was downloaded. Later, I downloaded a win system and used my own verification tool to verify whether it was consistent with the SHA value given by Microsoft. The gadget was stuck directly and the result was released for a long time. This shows that this gadget still needs to be optimized, especially for processing large files.
Looking for information online, I saw that there is such an implementation idea: segment the file in memory, for example, divide it into segments, start calculating in threads at the same time, and finally process the calculated value to obtain the SHA value or MD value of the entire file. However, he was stupid and didn't know how to achieve it. I hope the great god can give some guidance after seeing it. I am very grateful.
The above introduction is the entire content of this article, I hope you like it.