-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
297 lines (259 loc) · 10.9 KB
/
Program.cs
File metadata and controls
297 lines (259 loc) · 10.9 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
using System;
using System.Drawing;
using System.IO;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Diagnostics;
namespace HashChecker
{
public class SetupForm : Form
{
public SetupForm()
{
this.Text = "Context Menu Hash Checker - Setup";
this.Size = new Size(420, 240);
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.StartPosition = FormStartPosition.CenterScreen;
// --- TIKLANABİLİR HEADER ALANI ---
EventHandler openUrl = (s, e) => Process.Start(new ProcessStartInfo("https://www.osmanonurkoc.com") { UseShellExecute = true });
try
{
Icon appIcon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
this.Icon = appIcon;
PictureBox pbIcon = new PictureBox()
{
Image = appIcon.ToBitmap(),
Location = new Point(20, 15),
Size = new Size(48, 48),
SizeMode = PictureBoxSizeMode.Zoom,
Cursor = Cursors.Hand
};
pbIcon.Click += openUrl;
this.Controls.Add(pbIcon);
}
catch { }
Label lblTitle = new Label()
{
Text = "Context Menu Hash Checker",
Location = new Point(75, 20),
AutoSize = true,
Font = new Font(this.Font, FontStyle.Bold),
Cursor = Cursors.Hand
};
lblTitle.Click += openUrl;
Label lblSubtitle = new Label()
{
Text = "@osmanonurkoc",
Location = new Point(77, 40),
AutoSize = true,
ForeColor = Color.Gray,
Cursor = Cursors.Hand
};
lblSubtitle.Click += openUrl;
// --- İÇERİK ALANI ---
Label lblDesc = new Label()
{
Text = "This tool adds hash verification options to the right-click menu.\nYou can use the buttons below for installation.",
Location = new Point(20, 85),
AutoSize = true
};
Button btnInstall = new Button()
{
Text = "Install to Context Menu",
Location = new Point(20, 140),
Width = 175,
Height = 35
};
btnInstall.Click += BtnInstall_Click;
Button btnUninstall = new Button()
{
Text = "Remove from Context Menu",
Location = new Point(205, 140),
Width = 175,
Height = 35
};
btnUninstall.Click += BtnUninstall_Click;
this.Controls.Add(lblTitle);
this.Controls.Add(lblSubtitle);
this.Controls.Add(lblDesc);
this.Controls.Add(btnInstall);
this.Controls.Add(btnUninstall);
}
private void BtnInstall_Click(object sender, EventArgs e)
{
try
{
string exePath = Application.ExecutablePath;
string basePath = @"Software\Classes\*\shell\Checksum";
using (RegistryKey key = Registry.CurrentUser.CreateSubKey(basePath))
{
key.SetValue("MUIVerb", "Checksum");
key.SetValue("SubCommands", "");
key.SetValue("Icon", $"\"{exePath}\",0");
}
string[] algos = { "MD5", "SHA1", "SHA256", "SHA384", "SHA512" };
for (int i = 0; i < algos.Length; i++)
{
string algo = algos[i];
using (RegistryKey subKey = Registry.CurrentUser.CreateSubKey($@"{basePath}\shell\0{i + 1}{algo}"))
{
subKey.SetValue("MUIVerb", algo);
using (RegistryKey cmdKey = subKey.CreateSubKey("command"))
{
cmdKey.SetValue("", $"\"{exePath}\" \"%1\" {algo.ToLower()}");
}
}
}
MessageBox.Show("Successfully added to the context menu!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("An error occurred:\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void BtnUninstall_Click(object sender, EventArgs e)
{
try
{
Registry.CurrentUser.DeleteSubKeyTree(@"Software\Classes\*\shell\Checksum", false);
MessageBox.Show("Successfully removed from the context menu.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("An error occurred:\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
public class HashForm : Form
{
private TextBox txtCalculated;
private TextBox txtCompare;
private Label lblStatus;
private Button btnCopy;
private ProgressBar progressBar;
private string calculatedHash = "";
private string targetFile = "";
private string algorithm = "";
public HashForm(string file, string algo)
{
targetFile = file;
algorithm = algo.ToUpper();
this.Text = $"{algorithm} Checksum Verifier";
this.Size = new Size(500, 310);
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.StartPosition = FormStartPosition.CenterScreen;
this.Load += HashForm_Load;
try { this.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath); } catch { }
Label lblCalc = new Label() { Text = $"Calculated {algorithm}:", Location = new Point(15, 15), AutoSize = true, Font = new Font(this.Font, FontStyle.Bold) };
txtCalculated = new TextBox() { Text = "Calculating...", Location = new Point(15, 40), Width = 370, ReadOnly = true };
btnCopy = new Button() { Text = "Copy", Location = new Point(395, 38), Width = 75, Enabled = false };
btnCopy.Click += (s, e) => { if(!string.IsNullOrEmpty(calculatedHash)) Clipboard.SetText(calculatedHash); };
progressBar = new ProgressBar() { Location = new Point(15, 75), Width = 455, Height = 20, Style = ProgressBarStyle.Marquee };
Label lblComp = new Label() { Text = "Paste Target Hash Here:", Location = new Point(15, 110), AutoSize = true, Font = new Font(this.Font, FontStyle.Bold) };
txtCompare = new TextBox() { Location = new Point(15, 135), Width = 455 };
txtCompare.TextChanged += TxtCompare_TextChanged;
lblStatus = new Label() { Text = "Waiting for hash calculation...", Location = new Point(15, 175), Width = 455, TextAlign = ContentAlignment.MiddleCenter };
Button btnOk = new Button() { Text = "OK", Location = new Point(205, 215), Width = 80 };
btnOk.Click += (s, e) => { this.Close(); };
this.Controls.Add(lblCalc);
this.Controls.Add(txtCalculated);
this.Controls.Add(btnCopy);
this.Controls.Add(progressBar);
this.Controls.Add(lblComp);
this.Controls.Add(txtCompare);
this.Controls.Add(lblStatus);
this.Controls.Add(btnOk);
}
private async void HashForm_Load(object sender, EventArgs e)
{
try
{
calculatedHash = await Task.Run(() => CalculateHashOptimized(targetFile, algorithm));
txtCalculated.Text = calculatedHash;
btnCopy.Enabled = true;
progressBar.Style = ProgressBarStyle.Blocks;
progressBar.Value = 100;
if (string.IsNullOrEmpty(txtCompare.Text))
{
lblStatus.Text = "Waiting for comparison...";
}
else
{
TxtCompare_TextChanged(null, null);
}
}
catch (Exception ex)
{
txtCalculated.Text = "Error: " + ex.Message;
progressBar.Style = ProgressBarStyle.Blocks;
lblStatus.Text = "Calculation failed!";
lblStatus.ForeColor = Color.Red;
}
}
private void TxtCompare_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(calculatedHash)) return;
string target = txtCompare.Text.Trim().ToLower();
string calc = calculatedHash.ToLower();
if (string.IsNullOrEmpty(target))
{
lblStatus.Text = "Waiting for comparison...";
lblStatus.ForeColor = Color.Black;
}
else if (target == calc)
{
lblStatus.Text = "✓ Hash Matches!";
lblStatus.ForeColor = Color.Green;
}
else
{
lblStatus.Text = "✗ Hash Mismatch!";
lblStatus.ForeColor = Color.Red;
}
}
private string CalculateHashOptimized(string filename, string algo)
{
HashAlgorithm hashAlgo = null;
switch (algo)
{
case "MD5": hashAlgo = MD5.Create(); break;
case "SHA1": hashAlgo = SHA1.Create(); break;
case "SHA256": hashAlgo = SHA256.Create(); break;
case "SHA384": hashAlgo = SHA384.Create(); break;
case "SHA512": hashAlgo = SHA512.Create(); break;
default: throw new Exception("Unsupported Algorithm");
}
int bufferSize = 1024 * 1024;
using (FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize, FileOptions.SequentialScan))
{
byte[] hash = hashAlgo.ComputeHash(stream);
return BitConverter.ToString(hash).Replace("-", "").ToLower();
}
}
}
static class Program
{
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length == 0)
{
Application.Run(new SetupForm());
}
else if (args.Length >= 2)
{
Application.Run(new HashForm(args[0], args[1]));
}
else
{
MessageBox.Show("Missing parameters!\nDouble-click the program for normal use.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}