-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.pas
More file actions
236 lines (205 loc) · 5.43 KB
/
Copy pathMain.pas
File metadata and controls
236 lines (205 loc) · 5.43 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
{****************************************************
Snap MD5
Copyright (C) 2011 - Dan Hersam http://dan.hersam.com
*****************************************************}
{%RunFlags BUILD-}
unit Main;
interface
uses
SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,
ClipBrd, LResources, Menus, ComCtrls, Md5, Sha1, About, ShellApi, FileUtil;
type
{ TMainForm }
TMainForm = class(TForm)
BrowseBtn: TButton;
MenuReset: TMenuItem;
MenuHash: TMenuItem;
sha1: TEdit;
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
FileField: TEdit;
md5sum: TEdit;
GroupBox3: TGroupBox;
Clipfield: TEdit;
GroupBox4: TGroupBox;
MatchStatus: TLabel;
MainMenu1: TMainMenu;
MenuFile: TMenuItem;
MenuHelp: TMenuItem;
MenuAbout: TMenuItem;
MenuOpen: TMenuItem;
MenuExit: TMenuItem;
StatusBar1: TStatusBar;
procedure ActivateForm(Sender: TObject);
procedure BrowseBtnClick(Sender: TObject);
procedure ResetFields(Sender: TObject);
procedure CloseApp(Sender: TObject);
procedure DropFiles(Sender: TObject; const FileNames: array of String);
procedure FillFields(Sender: TObject);
procedure ClipfieldChange(Sender: TObject);
procedure FormKeyPress(Sender: TObject; var Key: Char);
procedure SelectField(Sender: TObject);
procedure ShowAbout(Sender: TObject);
procedure UpdateStatus();
procedure GetTextFromClipboard();
procedure CalcHashes(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
procedure TMainForm.BrowseBtnClick(Sender: TObject);
var
openDialog : TOpenDialog;
begin
openDialog := TOpenDialog.Create(self);
openDialog.FileName := '';
openDialog.InitialDir := GetCurrentDir;
openDialog.Options := [ofFileMustExist];
openDialog.Filter := '*.*';
if openDialog.Execute then
begin
FileField.Text := OpenDialog.FileName;
end;
openDialog.Free;
CalcHashes(Sender);
end;
procedure TMainForm.ResetFields(Sender: TObject);
begin
FileField.Text := '';
md5sum.Text := '';
sha1.Text := '';
GetTextFromClipboard();
MatchStatus.Caption := '';
MatchStatus.Color := clNone;
// This will set the status bar appropriately
CalcHashes(Sender);
end;
procedure TMainForm.ActivateForm(Sender: TObject);
begin
WindowState := wsNormal;
ShowInTaskBar := stDefault;
Show;
// Only update if Filename is populated, status is empty and both hash fields are empty
if (Length(FileField.Text) > 0) And (Length(StatusBar1.SimpleText) = 0) And
(Length(md5sum.Text) = 0) And (Length(sha1.Text) = 0) then
CalcHashes(Sender);
end;
procedure TMainForm.CloseApp(Sender: TObject);
begin
Close;
end;
procedure TMainForm.DropFiles(Sender: TObject; const FileNames: array of String);
begin
FileField.Text := FileNames[0];
CalcHashes(Sender);
end;
procedure TMainForm.CalcHashes(Sender: TObject);
var fname : String;
begin
fname := UTF8ToSys(FileField.Text);
md5sum.Text := '';
sha1.Text := '';
MatchStatus.Caption := '';
MatchStatus.Color := clNone;
GetTextFromClipboard();
if Length(fname) = 0 then
begin
StatusBar1.SimpleText := 'Browse to a file';
Exit;
end;
if Not FileExists(fname) then
begin
StatusBar1.SimpleText := 'That file doesn''t exist';
Exit;
end;
try
StatusBar1.SimpleText := 'Generating MD5 sum...';
MainForm.Repaint;
md5sum.Text := MD5Print(MD5File(fname));
MainForm.Repaint;
StatusBar1.SimpleText := 'Generating SHA-1 hash...';
MainForm.Repaint;
sha1.Text := SHA1Print(SHA1File(fname));
StatusBar1.SimpleText := 'Done';
UpdateStatus();
except
StatusBar1.SimpleText := 'Error opening file: ' + fname;
end;
end;
procedure TMainForm.UpdateStatus();
begin
if (md5sum.Text = '') or (Clipfield.Text = '') then
begin
Exit
end;
// Get rid of trailing whitespace
Clipfield.Text := trim(Clipfield.Text);
if md5sum.Text = Clipfield.Text then
begin
MatchStatus.Caption := 'MD5 sum matches';
MatchStatus.Color := clGreen;
StatusBar1.SimpleText := 'Success';
end
else if sha1.Text = Clipfield.Text then
begin
MatchStatus.Caption := 'SHA-1 hash matches';
MatchStatus.Color := clTeal;
StatusBar1.SimpleText := 'Success';
end
else begin
MatchStatus.Caption := 'No match';
MatchStatus.Color := clRed;
StatusBar1.SimpleText := 'File does not match either hash';
end;
end;
procedure TMainForm.FillFields(Sender: TObject);
begin
DragAcceptFiles(Handle, True);
if ParamCount >= 1 then
begin
FileField.Text := SysToUTF8(ParamStr(1));
end;
GetTextFromClipboard();
StatusBar1.SimpleText := '';
end;
procedure TMainForm.GetTextFromClipboard();
var
clipText : String;
begin
clipText := Clipboard.AsText;
if length(clipText) = 0 then
begin
Clipfield.Text := 'No text was in the clipboard';
end
else begin
setLength(clipText, 40);
Clipfield.Text := clipText;
end;
end;
procedure TMainForm.ClipfieldChange(Sender: TObject);
begin
updateStatus();
end;
procedure TMainForm.FormKeyPress(Sender: TObject; var Key: Char);
begin
if key = #27 then Close;
end;
procedure TMainForm.SelectField(Sender: TObject);
begin
If (Sender is TEdit) then
TEdit(Sender).SelectAll;
end;
procedure TMainForm.ShowAbout(Sender: TObject);
var AboutForm : TAboutForm;
begin
AboutForm := TAboutForm.Create(nil);
AboutForm.ShowModal;
AboutForm.Release;
end;
initialization
{$i Main.lrs}
end.