-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
88 lines (85 loc) · 3.49 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.IO;
using System.Security.Cryptography;
using System.Threading;
namespace HashVarifier
{
class Program
{
static void Main()
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("<==================== Hash Varifier ====================>");
Console.WriteLine("***** Coded with <3 by Evilprince2009 *****");
while (true)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Select what you want to do.");
Console.WriteLine("1. Calculate file HASH");
Console.WriteLine("2. Match HASHes to verify files");
Console.ForegroundColor = ConsoleColor.White;
string decission = Console.ReadLine().Trim();
if (decission == "1")
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Enter full path with file name.");
string filepath = Console.ReadLine().Trim();
TimeLapse(500, 5);
Console.WriteLine("Calculated HASH");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(GenerateHash(filepath));
Console.ForegroundColor = ConsoleColor.Green;
}
else if (decission == "2")
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Enter full path with name for first file");
string first = Console.ReadLine().Trim();
Console.WriteLine("Enter full path with name for second file");
string second = Console.ReadLine().Trim();
if (String.Compare(GenerateHash(first), GenerateHash(second), StringComparison.Ordinal) == 0)
{
TimeLapse(500, 5);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("=> File HASHes match");
}
else
{
TimeLapse(500, 5);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("=> File HASHes do not match");
}
}
else
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("=> Choose a valid option . . .");
}
}
}
private static void TimeLapse(int millis, int times)
{
Console.WriteLine("Hang on just a moment");
for (int i = 0; i < times; i++)
{
Thread.Sleep(millis);
Console.Write(". ");
}
Console.WriteLine(Environment.NewLine);
}
private static string GenerateHash(string filename)
{
if (!File.Exists(filename))
{
throw new FileNotFoundException("Something unusual happened", filename);
}
else
{
var algorithm = SHA256.Create();
using FileStream stream = new(filename, FileMode.Open, FileAccess.ReadWrite);
byte[] hashes = algorithm.ComputeHash(stream);
return BitConverter.ToString(hashes).Replace("-", "").ToLowerInvariant();
}
}
}
}