Today we write the tool class, what is the use of this class?
That is, no matter what project you do, this tool class can be used, the reason is universal, is applicable to all projects.
This section I mainly talk about how to generate the file MD5 code.
Then this MD5 is what a ghost thing, the reader can own Baidu, in fact, the brief is to verify whether the file has been tampered with.
What do you mean, like when you go to the website to download the game client software, when some hackers like to do something bad, in your client add some malicious code and so on.
When the hacker changed the client file, the MD5 of the file also changed. So we just need to verify that the user downloads the client's MD5 and the MD5 code provided by the website is consistent, to determine whether to download.
So this MD5 is very useful, and C # also comes with the MD5 library.
public static string BuildFileMd5 (string filename) { string filemd5 = null; Try { using (var fileStream = file.openread (filename)) { var md5 = MD5. Create (); var filemd5bytes = MD5. ComputeHash (FileStream);//Calculates the hash value of the specified Stream object filemd5 = FormatMD5 (filemd5bytes);} } catch (System.Exception ex) { loggerhelper.except (ex); } return filemd5; }
public static string FormatMD5 (byte[] data) { return System.BitConverter.ToString (data). Replace ("-", ""). ToLower ();//replace byte[] with a string }
Write this code into the Utils class, and later, if you want to use the MD5 code to calculate the direct utils.buildfilemd5 (path), you can take the MD5 of this file.
is not very simple, OK, then I will continue to update some of the common tool methods used.
Unity3d the MD5 of the generated files of the generic tool class