-
Notifications
You must be signed in to change notification settings - Fork 0
test #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
test #6
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Хеш в принципе не может быть строкой, это массив байт. А это совсем другое — в массиве байт могут быть нулевые байты, в строке нет |
||
| { | ||
| var dir = new DirectoryInfo(path); | ||
| var otherdir = dir.GetDirectories(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. otherDir |
||
| var files = dir.GetFiles(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. files в этом деле никак не участвуют, как я вижу |
||
| var md5 = new MD5CryptoServiceProvider(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Derived cryptographic types are obsolete. Use the Create method on the base type instead". Интересно, что в условии был написан класс, которым надо было пользоваться, Вы нашли другой :) |
||
| string hash = BitConverter.ToString(md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(dir.Name))); | ||
| hash += MD5Single(otherdir); | ||
| return hash; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. И просили MD5-хеш от вот этого, то есть ответ-то неправильный |
||
| } | ||
|
|
||
| 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; | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если у нас папка с файлами, но без подпапок, получится не только неправда, но ещё и непараллельно совсем :) |
||
|
|
||
| private static string MD5Single(DirectoryInfo[] dirs) | ||
| { | ||
| var md5 = new MD5CryptoServiceProvider(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Он IDisposable, так что правильнее было using var |
||
| 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; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если в папке вообще нет файлов, этот код может оказаться в затруднительном положении |
||
| var threads = new Thread[size]; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "As soon as you type new Thread() , it’s over; your project already has legacy code." (с) Concurrency in C# Cookbook |
||
| 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); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Гонки тут всякие напридумывали... Конкатенируем строку и надеемся на русский авось |
||
| } | ||
| }); | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это самодельный плохой Parallel.ForEach :) |
||
| 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]); | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Параллельность? |
||
|
|
||
| return hash; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. У меня такого нет, не работает :) |
||
| Console.WriteLine(hash); | ||
| Console.WriteLine("\n"); | ||
| hash = MD5.CheckSumMulti("C:\\Users\\89803\\Files\\MatMech\\Algebra"); | ||
| Console.WriteLine(hash); | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А ещё по условию надо было время работы сравнить |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net5.0</TargetFramework> | ||
| <DockerDefaultTargetOS>Windows</DockerDefaultTargetOS> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Надо комментарии