-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
372 lines (325 loc) · 14.3 KB
/
Copy pathProgram.cs
File metadata and controls
372 lines (325 loc) · 14.3 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
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
{
private readonly string installDir;
private readonly string installExePath;
public SetupForm()
{
// Define installation paths in %LOCALAPPDATA%
string appData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
installDir = Path.Combine(appData, "ContextMenuHashChecker");
installExePath = Path.Combine(installDir, Path.GetFileName(Application.ExecutablePath));
this.Text = "Context Menu Hash Checker - Setup";
this.Size = new Size(420, 240);
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.StartPosition = FormStartPosition.CenterScreen;
// --- CLICKABLE HEADER AREA ---
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;
// --- CONTENT AREA ---
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
{
// 1. Copy the executable to the Local AppData directory
if (!Directory.Exists(installDir))
{
Directory.CreateDirectory(installDir);
}
// Only copy if we are not already running from the destination folder
if (!Application.ExecutablePath.Equals(installExePath, StringComparison.OrdinalIgnoreCase))
{
File.Copy(Application.ExecutablePath, installExePath, true);
}
// 2. Write to Registry using the new installed path
string basePath = @"Software\Classes\*\shell\Checksum";
using (RegistryKey key = Registry.CurrentUser.CreateSubKey(basePath))
{
key.SetValue("MUIVerb", "Checksum");
key.SetValue("SubCommands", "");
key.SetValue("Icon", $"\"{installExePath}\",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("", $"\"{installExePath}\" \"%1\" {algo.ToLower()}");
}
}
}
MessageBox.Show("Successfully installed to the context menu!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("An error occurred during installation:\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void BtnUninstall_Click(object sender, EventArgs e)
{
try
{
// 1. Remove Registry Keys
Registry.CurrentUser.DeleteSubKeyTree(@"Software\Classes\*\shell\Checksum", false);
// 2. Handle File Deletion
bool isRunningFromInstallDir = Application.ExecutablePath.Equals(installExePath, StringComparison.OrdinalIgnoreCase);
if (isRunningFromInstallDir)
{
// If running from the installed directory, we cannot delete the exe directly.
// We trigger a self-deleting batch script and exit.
MessageBox.Show("Successfully removed from the context menu.\nThe application will now close and clean up its files.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
DeleteSelfAndExit();
}
else
{
// If running from somewhere else (e.g., Downloads), delete the installed directory safely.
if (Directory.Exists(installDir))
{
Directory.Delete(installDir, true);
}
MessageBox.Show("Successfully removed from the context menu and cleaned up installation files.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show("An error occurred during removal:\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void DeleteSelfAndExit()
{
try
{
string batchPath = Path.Combine(Path.GetTempPath(), "uninstall_hashchecker.bat");
string exePath = Application.ExecutablePath;
string dirPath = Path.GetDirectoryName(exePath);
// Batch script: wait 2 seconds for the app to close, delete the exe, delete the folder, then delete the batch script itself.
string batchContent = $@"
@echo off
ping 127.0.0.1 -n 2 > nul
del /f /q ""{exePath}""
cd ..
rd /s /q ""{dirPath}""
del ""%~f0""
";
File.WriteAllText(batchPath, batchContent);
ProcessStartInfo psi = new ProcessStartInfo()
{
FileName = batchPath,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false
};
Process.Start(psi);
}
catch { /* Silently fail if self-deletion script fails to execute */ }
finally
{
Application.Exit();
}
}
}
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);
}
}
}
}