Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Test2/Test2.sln
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
105 changes: 105 additions & 0 deletions Test2/Test2/MD5.cs
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Надо комментарии

{
public static string CheckSum(string path)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Хеш в принципе не может быть строкой, это массив байт. А это совсем другое — в массиве байт могут быть нулевые байты, в строке нет

{
var dir = new DirectoryInfo(path);
var otherdir = dir.GetDirectories();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherDir

var files = dir.GetFiles();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

files в этом деле никак не участвуют, как я вижу

var md5 = new MD5CryptoServiceProvider();
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если у нас папка с файлами, но без подпапок, получится не только неправда, но ещё и непараллельно совсем :)


private static string MD5Single(DirectoryInfo[] dirs)
{
var md5 = new MD5CryptoServiceProvider();
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если в папке вообще нет файлов, этот код может оказаться в затруднительном положении

var threads = new Thread[size];
Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
Copy link
Collaborator

@yurii-litvinov yurii-litvinov Dec 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Гонки тут всякие напридумывали... Конкатенируем строку и надеемся на русский авось

}
});
}
Copy link
Collaborator

Choose a reason for hiding this comment

The 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]);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Параллельность?


return hash;
}
}
}
16 changes: 16 additions & 0 deletions Test2/Test2/Program.cs
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");
Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А ещё по условию надо было время работы сравнить

}
}
9 changes: 9 additions & 0 deletions Test2/Test2/Test2.csproj
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>