Initial commit

Signed-off-by: Lev Rusanov <30170278+JDM170@users.noreply.github.com>
This commit is contained in:
2024-11-08 23:01:42 +07:00
commit 9381bb8804
7 changed files with 287 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
.vs
bin/
obj/
packages/
*.csproj.user

6
App.config Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>

74
Program.cs Normal file
View File

@@ -0,0 +1,74 @@
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
namespace file_hash_checker
{
internal class Program
{
private static string CalculateMD5(string data)
{
using (var md5 = MD5.Create())
{
//using (var progress = new ProgressBar())
//{
// progress.Report((double)(stream.Position / stream.Length));
//}
Console.Write("Расчет MD5... ");
using (var stream = File.OpenRead(data))
md5.ComputeHash(stream);
Console.WriteLine("Готово.");
StringBuilder sb = new StringBuilder();
byte[] hash = md5.Hash;
for (int i = 0; i < hash.Length; i++)
sb.Append(hash[i].ToString("x2"));
return sb.ToString();
}
}
private static string InputData(string message)
{
Console.WriteLine(message);
Console.Write("> ");
string data = Console.ReadLine().Trim();
if (data.Length == 0 || data == string.Empty)
InputData(message);
return data;
}
static void Main()
{
string filename = InputData("\nВведите путь до файла:");
string remote = InputData("\nВведите путь до файла для сравнения (или путь до файла с хэшем или сам хэш):");
Console.WriteLine();
string hash = CalculateMD5(filename);
bool result = false;
if (Regex.Match(remote, @"[a-zA-Z0-9]{32,}").Success)
result = hash == remote.ToLower();
else if (File.Exists(remote))
result = hash == CalculateMD5(remote);
else if (remote.EndsWith(".md5"))
{
if (!File.Exists(remote))
{
Console.WriteLine("Файл не найден!");
return;
}
var fileData = File.ReadAllText(remote);
Match match = Regex.Match(fileData, @"[a-zA-Z0-9]{32,}");
if (match.Success)
result = hash == match.Value;
}
Console.WriteLine("\nСовпадает: " + (result ? "Да" : "Нет"));
Console.WriteLine("\nНажмите любую клавишу...");
Console.ReadKey();
}
}
}

90
ProgressBar.cs Normal file
View File

@@ -0,0 +1,90 @@
using System;
using System.Text;
using System.Threading;
/// <summary>
/// An ASCII progress bar
/// </summary>
public class ProgressBar : IDisposable, IProgress<double> {
private const int blockCount = 10;
private readonly TimeSpan animationInterval = TimeSpan.FromSeconds(1.0 / 8);
private const string animation = @"|/-\";
private readonly Timer timer;
private double currentProgress = 0;
private string currentText = string.Empty;
private bool disposed = false;
private int animationIndex = 0;
public ProgressBar() {
timer = new Timer(TimerHandler);
// A progress bar is only for temporary display in a console window.
// If the console output is redirected to a file, draw nothing.
// Otherwise, we'll end up with a lot of garbage in the target file.
if (!Console.IsOutputRedirected) {
ResetTimer();
}
}
public void Report(double value) {
// Make sure value is in [0..1] range
value = Math.Max(0, Math.Min(1, value));
Interlocked.Exchange(ref currentProgress, value);
}
private void TimerHandler(object state) {
lock (timer) {
if (disposed) return;
int progressBlockCount = (int) (currentProgress * blockCount);
int percent = (int) (currentProgress * 100);
string text = string.Format("[{0}{1}] {2,3}% {3}",
new string('#', progressBlockCount), new string('-', blockCount - progressBlockCount),
percent,
animation[animationIndex++ % animation.Length]);
UpdateText(text);
ResetTimer();
}
}
private void UpdateText(string text) {
// Get length of common portion
int commonPrefixLength = 0;
int commonLength = Math.Min(currentText.Length, text.Length);
while (commonPrefixLength < commonLength && text[commonPrefixLength] == currentText[commonPrefixLength]) {
commonPrefixLength++;
}
// Backtrack to the first differing character
StringBuilder outputBuilder = new StringBuilder();
outputBuilder.Append('\b', currentText.Length - commonPrefixLength);
// Output new suffix
outputBuilder.Append(text.Substring(commonPrefixLength));
// If the new text is shorter than the old one: delete overlapping characters
int overlapCount = currentText.Length - text.Length;
if (overlapCount > 0) {
outputBuilder.Append(' ', overlapCount);
outputBuilder.Append('\b', overlapCount);
}
Console.Write(outputBuilder);
currentText = text;
}
private void ResetTimer() {
timer.Change(animationInterval, TimeSpan.FromMilliseconds(-1));
}
public void Dispose() {
lock (timer) {
disposed = true;
UpdateText(string.Empty);
}
}
}

View File

@@ -0,0 +1,33 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// Общие сведения об этой сборке предоставляются следующим набором
// набора атрибутов. Измените значения этих атрибутов для изменения сведений,
// связанные с этой сборкой.
[assembly: AssemblyTitle("Утилита для сверки контрольных сумм файлов")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("file_hash_checker")]
[assembly: AssemblyCopyright("Lev Rusanov © 2024")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Установка значения False для параметра ComVisible делает типы в этой сборке невидимыми
// для компонентов COM. Если необходимо обратиться к типу в этой сборке через
// из модели COM задайте для атрибута ComVisible этого типа значение true.
[assembly: ComVisible(false)]
// Следующий GUID представляет идентификатор typelib, если этот проект доступен из модели COM
[assembly: Guid("4fdab78e-f93e-4ddc-a787-b8ce6f14542f")]
// Сведения о версии сборки состоят из указанных ниже четырех значений:
//
// Основной номер версии
// Дополнительный номер версии
// Номер сборки
// Номер редакции
//
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

54
file_hash_checker.csproj Normal file
View File

@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{4FDAB78E-F93E-4DDC-A787-B8CE6F14542F}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>file_hash_checker</RootNamespace>
<AssemblyName>file_hash_checker</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<None Include="ProgressBar.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

25
file_hash_checker.sln Normal file
View File

@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.35327.3
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "file_hash_checker", "file_hash_checker.csproj", "{4FDAB78E-F93E-4DDC-A787-B8CE6F14542F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4FDAB78E-F93E-4DDC-A787-B8CE6F14542F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4FDAB78E-F93E-4DDC-A787-B8CE6F14542F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4FDAB78E-F93E-4DDC-A787-B8CE6F14542F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4FDAB78E-F93E-4DDC-A787-B8CE6F14542F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B85EDCB5-364F-4391-AF7B-0491EC8C2DCD}
EndGlobalSection
EndGlobal