diff --git a/anbs_cp/Classes/FileExtensions.cs b/anbs_cp/Classes/FileExtensions.cs index a0b278a..478586f 100644 --- a/anbs_cp/Classes/FileExtensions.cs +++ b/anbs_cp/Classes/FileExtensions.cs @@ -9,48 +9,6 @@ namespace anbs_cp.Classes; /// public static class FileExtension { - /// - /// Получение md5-хэша файла. - /// Взято с https://stackoverflow.com/a/24031467/16469671 - /// - /// Имя файла - /// Массив хэша - public static byte[] Hash (string fileName) - { - using MD5 md5 = MD5.Create(); - byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(fileName); - byte[] result = md5.ComputeHash(inputBytes); - - return result; - } - - /// - /// Получение md5-хэша загружаемого файла. - /// Взято с https://stackoverflow.com/a/67081012/16469671 - /// - /// Загружаемый файл - /// Массив хэша - public static byte[] Hash (IFormFile file) - { - using MD5 md5 = MD5.Create(); - using StreamReader streamReader = new(file.OpenReadStream()); - return md5.ComputeHash(streamReader.BaseStream); - } - - /// - /// Получение md5-хэша файла и вывод в строке. - /// - /// Имя файла - /// Срока с хэшем файла - public static string HashString (string fileName) => Convert.ToHexString(Hash(fileName)); - - /// - /// Получение md5-хэша файла и вывод в строке. - /// - /// Загружаемый файл - /// Срока с хэшем файла - public static string HashString (IFormFile file) => Convert.ToHexString(Hash(file)); - /// /// Получает MIME-тип файла /// diff --git a/anbs_cp/Classes/FileHash.cs b/anbs_cp/Classes/FileHash.cs new file mode 100644 index 0000000..b1453be --- /dev/null +++ b/anbs_cp/Classes/FileHash.cs @@ -0,0 +1,48 @@ +using System.Security.Cryptography; + +using Microsoft.AspNetCore.Http; + +namespace anbs_cp.Classes; + +/// +/// Класс для работы с хэшем файла +/// +public class FileHash +{ + /// + /// Получение md5-хэша файла. + /// Взято с https://stackoverflow.com/a/24031467/16469671 + /// + /// Имя файла + /// Массив хэша + public FileHash (string fileName) + { + using MD5 md5 = MD5.Create(); + using FileStream stream = File.OpenRead(fileName); + Hash = md5.ComputeHash(stream); + } + + /// + /// Получение md5-хэша загружаемого файла. + /// Взято с https://stackoverflow.com/a/67081012/16469671 + /// + /// Загружаемый файл + /// Массив хэша + public FileHash (IFormFile file) + { + using MD5 md5 = MD5.Create(); + using StreamReader streamReader = new(file.OpenReadStream()); + Hash = md5.ComputeHash(streamReader.BaseStream); + } + + /// + /// Хэш файла + /// + public byte[] Hash { get; } + + /// + /// Вывод в строку + /// + /// Строка хэша файла + public override string ToString () => BitConverter.ToString(Hash).Replace("-", "").ToLowerInvariant(); +} \ No newline at end of file diff --git a/anbs_cp/anbs_cp.csproj b/anbs_cp/anbs_cp.csproj index b3185b3..bdbb49c 100644 --- a/anbs_cp/anbs_cp.csproj +++ b/anbs_cp/anbs_cp.csproj @@ -2,7 +2,7 @@ net7.0 - 2023.201.0 + 2023.204.0 Alexander Babaev ANB Software Components Pack Library of some useful functions in C# language. diff --git a/demo/FileHashAndMimeTypeTest.cs b/demo/FileHashAndMimeTypeTest.cs index 0cc25ad..69f7cf4 100644 --- a/demo/FileHashAndMimeTypeTest.cs +++ b/demo/FileHashAndMimeTypeTest.cs @@ -23,7 +23,7 @@ private void GetFileHashAndMimeType() return; } - string fileHash = FileExtension.HashString(fileNameEdt.Text); + string fileHash = new FileHash(fileNameEdt.Text).ToString(); string fileType = FileExtension.MIMEType(fileNameEdt.Text); ResultLabel.Text =