20230204
This commit is contained in:
parent
63b5cfeed8
commit
6fd3f41e31
@ -9,48 +9,6 @@ namespace anbs_cp.Classes;
|
||||
/// </summary>
|
||||
public static class FileExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// Получение md5-хэша файла.
|
||||
/// Взято с https://stackoverflow.com/a/24031467/16469671
|
||||
/// </summary>
|
||||
/// <param name="fileName">Имя файла</param>
|
||||
/// <returns>Массив хэша</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение md5-хэша загружаемого файла.
|
||||
/// Взято с https://stackoverflow.com/a/67081012/16469671
|
||||
/// </summary>
|
||||
/// <param name="file">Загружаемый файл</param>
|
||||
/// <returns>Массив хэша</returns>
|
||||
public static byte[] Hash (IFormFile file)
|
||||
{
|
||||
using MD5 md5 = MD5.Create();
|
||||
using StreamReader streamReader = new(file.OpenReadStream());
|
||||
return md5.ComputeHash(streamReader.BaseStream);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение md5-хэша файла и вывод в строке.
|
||||
/// </summary>
|
||||
/// <param name="fileName">Имя файла</param>
|
||||
/// <returns>Срока с хэшем файла</returns>
|
||||
public static string HashString (string fileName) => Convert.ToHexString(Hash(fileName));
|
||||
|
||||
/// <summary>
|
||||
/// Получение md5-хэша файла и вывод в строке.
|
||||
/// </summary>
|
||||
/// <param name="file">Загружаемый файл</param>
|
||||
/// <returns>Срока с хэшем файла</returns>
|
||||
public static string HashString (IFormFile file) => Convert.ToHexString(Hash(file));
|
||||
|
||||
/// <summary>
|
||||
/// Получает MIME-тип файла
|
||||
/// </summary>
|
||||
|
48
anbs_cp/Classes/FileHash.cs
Normal file
48
anbs_cp/Classes/FileHash.cs
Normal file
@ -0,0 +1,48 @@
|
||||
using System.Security.Cryptography;
|
||||
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace anbs_cp.Classes;
|
||||
|
||||
/// <summary>
|
||||
/// Класс для работы с хэшем файла
|
||||
/// </summary>
|
||||
public class FileHash
|
||||
{
|
||||
/// <summary>
|
||||
/// Получение md5-хэша файла.
|
||||
/// Взято с https://stackoverflow.com/a/24031467/16469671
|
||||
/// </summary>
|
||||
/// <param name="fileName">Имя файла</param>
|
||||
/// <returns>Массив хэша</returns>
|
||||
public FileHash (string fileName)
|
||||
{
|
||||
using MD5 md5 = MD5.Create();
|
||||
using FileStream stream = File.OpenRead(fileName);
|
||||
Hash = md5.ComputeHash(stream);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение md5-хэша загружаемого файла.
|
||||
/// Взято с https://stackoverflow.com/a/67081012/16469671
|
||||
/// </summary>
|
||||
/// <param name="file">Загружаемый файл</param>
|
||||
/// <returns>Массив хэша</returns>
|
||||
public FileHash (IFormFile file)
|
||||
{
|
||||
using MD5 md5 = MD5.Create();
|
||||
using StreamReader streamReader = new(file.OpenReadStream());
|
||||
Hash = md5.ComputeHash(streamReader.BaseStream);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Хэш файла
|
||||
/// </summary>
|
||||
public byte[] Hash { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Вывод в строку
|
||||
/// </summary>
|
||||
/// <returns>Строка хэша файла</returns>
|
||||
public override string ToString () => BitConverter.ToString(Hash).Replace("-", "").ToLowerInvariant();
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<Version>2023.201.0</Version>
|
||||
<Version>2023.204.0</Version>
|
||||
<Authors>Alexander Babaev</Authors>
|
||||
<Product>ANB Software Components Pack</Product>
|
||||
<Description>Library of some useful functions in C# language.</Description>
|
||||
|
@ -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 =
|
||||
|
Loading…
x
Reference in New Issue
Block a user