Skip to content

Initial commit#26

Open
Palezehvat wants to merge 1 commit intomasterfrom
Md5
Open

Initial commit#26
Palezehvat wants to merge 1 commit intomasterfrom
Md5

Conversation

@Palezehvat
Copy link
Owner

No description provided.

using System.Text;
using System.Security.Cryptography;

namespace Md5;

Choose a reason for hiding this comment

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

Лучше неймспейс в самом начале файла (после шапки с лицензией)


namespace Md5;

public class MultiThreadMD5

Choose a reason for hiding this comment

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

  • Самому классу тоже нужен комментарий
  • Все методы static, значит и сам класс по логике вещей должен быть static

{
throw new FileNotFoundException();
}
var fstream = File.OpenRead(path);

Choose a reason for hiding this comment

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

Suggested change
var fstream = File.OpenRead(path);
using var fstream = File.OpenRead(path);

}
var fstream = File.OpenRead(path);
using var md5 = MD5.Create();
byte[] buffer = new byte[fstream.Length];

Choose a reason for hiding this comment

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

Suggested change
byte[] buffer = new byte[fstream.Length];
var buffer = new byte[fstream.Length];

var fstream = File.OpenRead(path);
using var md5 = MD5.Create();
byte[] buffer = new byte[fstream.Length];
await fstream.ReadAsync(buffer, 0, buffer.Length);

Choose a reason for hiding this comment

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

Зачитывать весь файл в массив может быть хорошей идеей, если мы знаем, что у нас ОЧЕНЬ маленькие файлы и ОЧЕНЬ много оперативки. Поэтому у ComputeHash есть перегрузка, принимающая Stream (она сама вычитывает файл порциями и считает хеш поблочно).

{
Console.WriteLine("Далее будет выполнено сравнение Md5 hash с использованием многопоточности и без неё");
Console.WriteLine("Введите путь до дирректории");
var pathToDirrectory = Console.ReadLine();

Choose a reason for hiding this comment

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

Suggested change
var pathToDirrectory = Console.ReadLine();
var pathToDirectory = Console.ReadLine();

var pathToDirrectory = Console.ReadLine();
if (pathToDirrectory == null )
{
throw new NullReferenceException();

Choose a reason for hiding this comment

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

Никогда не кидайте NullReferenceException, оно всегда должно указывать на ошибку в коде (и кидаться .NET-машиной).

{
throw new NullReferenceException();
}
Stopwatch stopwatchForDirrectory = new Stopwatch();

Choose a reason for hiding this comment

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

Suggested change
Stopwatch stopwatchForDirrectory = new Stopwatch();
Stopwatch stopwatchForDirectory = new();

}
var fstream = File.OpenRead(path);
using var md5 = MD5.Create();
return md5.ComputeHash(fstream);

Choose a reason for hiding this comment

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

Кажется, это можно было бы переиспользовать и в многопоточном варианте.

public void IsThrowExceptionWhenIncorrectPathFileInMultiThread()
{
Assert.ThrowsAsync<FileNotFoundException>(() => MultiThreadMD5.CalculateFile(pathDirectory + "/ttrt.txt"));
}

Choose a reason for hiding this comment

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

Можно было бы использовать TestCaseSource, чтобы избежать копипаста.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments