diff --git a/Test2/Test2.sln b/Test2/Test2.sln new file mode 100644 index 0000000..a2d1c6d --- /dev/null +++ b/Test2/Test2.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test2", "Test2\Test2.csproj", "{C4E4D325-036A-4937-A6B9-4B54D4A6B5B0}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C4E4D325-036A-4937-A6B9-4B54D4A6B5B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C4E4D325-036A-4937-A6B9-4B54D4A6B5B0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C4E4D325-036A-4937-A6B9-4B54D4A6B5B0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C4E4D325-036A-4937-A6B9-4B54D4A6B5B0}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Test2/Test2/MD5.cs b/Test2/Test2/MD5.cs new file mode 100644 index 0000000..137776c --- /dev/null +++ b/Test2/Test2/MD5.cs @@ -0,0 +1,105 @@ +using System; +using System.IO; +using System.Security.Cryptography; +using System.Text; +using System.Threading; + +namespace Test2 +{ + public class MD5 + { + public static string CheckSum(string path) + { + var dir = new DirectoryInfo(path); + var otherdir = dir.GetDirectories(); + var files = dir.GetFiles(); + var md5 = new MD5CryptoServiceProvider(); + string hash = BitConverter.ToString(md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(dir.Name))); + hash += MD5Single(otherdir); + return hash; + } + + public static string CheckSumMulti(string path) + { + var dir = new DirectoryInfo(path); + var otherdir = dir.GetDirectories(); + var files = dir.GetFiles(); + var md5 = new MD5CryptoServiceProvider(); + string hash = BitConverter.ToString(md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(dir.Name))); + hash += MD5Multi(otherdir); + return hash; + } + + private static string MD5Single(DirectoryInfo[] dirs) + { + var md5 = new MD5CryptoServiceProvider(); + string hash = null; + foreach (var d in dirs) + { + var byteDirHash = md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(d.Name)); + hash += BitConverter.ToString(byteDirHash); + var files = d.GetFiles(); + foreach (var f in files) + { + hash += FileHash(f, md5); + } + MD5Single(d.GetDirectories()); + } + return hash; + } + + private static string FileHash(FileInfo file, MD5CryptoServiceProvider md5) + { + var fileStream = File.OpenRead(file.FullName); + byte[] hashByte = md5.ComputeHash(fileStream); + return BitConverter.ToString(hashByte); + } + + private static string FindHash(DirectoryInfo dir) + { + var size = dir.GetFiles().Length < Environment.ProcessorCount + ? dir.GetFiles().Length + : Environment.ProcessorCount; + var threads = new Thread[size]; + var chunkSize = dir.GetFiles().Length / threads.Length + 1; + var md5 = new MD5CryptoServiceProvider(); + var bytedir = md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(dir.Name)); + var hash = BitConverter.ToString(bytedir); + var files = dir.GetFiles(); + for (int i = 0; i < dir.GetFiles().Length; i++) + { + var localI = i; + threads[i] = new Thread(() => + { + for (var l = localI * chunkSize; l < (localI + 1) * chunkSize && l < files.Length; ++l) + { + hash += FileHash(files[l], md5); + } + }); + } + foreach (var thread in threads) + { + thread.Start(); + } + + foreach (var thread in threads) + { + thread.Join(); + } + + return hash; + } + + + private static string MD5Multi(DirectoryInfo[] dirs) + { + string hash = null; + for (int i = 0; i < dirs.Length; ++i) + { + hash += FindHash(dirs[i]); + } + + return hash; + } + } +} \ No newline at end of file diff --git a/Test2/Test2/Program.cs b/Test2/Test2/Program.cs new file mode 100644 index 0000000..17e0062 --- /dev/null +++ b/Test2/Test2/Program.cs @@ -0,0 +1,16 @@ +using System; + +namespace Test2 +{ + class Program + { + static void Main(string[] args) + { + var hash = MD5.CheckSum("C:\\Users\\89803\\Files\\MatMech\\Algebra"); + Console.WriteLine(hash); + Console.WriteLine("\n"); + hash = MD5.CheckSumMulti("C:\\Users\\89803\\Files\\MatMech\\Algebra"); + Console.WriteLine(hash); + } + } +} \ No newline at end of file diff --git a/Test2/Test2/Test2.csproj b/Test2/Test2/Test2.csproj new file mode 100644 index 0000000..a184b89 --- /dev/null +++ b/Test2/Test2/Test2.csproj @@ -0,0 +1,9 @@ + + + + Exe + net5.0 + Windows + + +