-
Notifications
You must be signed in to change notification settings - Fork 2
/
ufPrincipal.pas
177 lines (155 loc) · 4 KB
/
ufPrincipal.pas
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
{
Autor: Luis Gustavo Carneiro
email: [email protected]
}
unit ufPrincipal;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.ExtCtrls,
_threadDigest, Vcl.ComCtrls;
type
TFPrincipal = class(TForm)
Label1: TLabel;
edArquivo: TEdit;
Label2: TLabel;
edMD5: TEdit;
btAbrir: TBitBtn;
btFechar: TBitBtn;
fOpen: TFileOpenDialog;
Label3: TLabel;
edResMD5: TEdit;
pbStatus: TProgressBar;
Panel1: TPanel;
lbFile: TLabel;
lbStatus: TLabel;
pnStatus: TPanel;
Label4: TLabel;
edResSHA256: TEdit;
edSHA256: TEdit;
Label5: TLabel;
pnMD5Status: TPanel;
pnSHA256Status: TPanel;
procedure btFecharClick(Sender: TObject);
procedure btAbrirClick(Sender: TObject);
private
f_lastPerc: Integer;
f_threadDigest: TThreadDigest;
procedure OnRead(const bytesRead: Int64; const fileSize: Int64);
procedure OnFileOpen(const fileSize: Int64);
procedure Done(var md5: string);
end;
var
FPrincipal: TFPrincipal;
implementation
{$R *.dfm}
function FormatByteSize(const bytes: Int64): string;
const
B = 1; //byte
KB = 1024 * B; //kilobyte
MB = 1024 * KB; //megabyte
GB = 1024 * MB; //gigabyte
begin
if bytes > GB then
result := FormatFloat('#.## GB', bytes / GB)
else
if bytes > MB then
result := FormatFloat('#.## MB', bytes / MB)
else
if bytes > KB then
result := FormatFloat('#.## KB', bytes / KB)
else
result := FormatFloat('#.## bytes', bytes) ;
end;
procedure TFPrincipal.btAbrirClick(Sender: TObject);
var
v_file: string;
begin
pbStatus.Position := 0;
edResMD5.Text := EmptyStr;
edResSHA256.Text := EmptyStr;
if fOpen.Execute then v_file := fOpen.FileName;
edArquivo.Text := v_file;
if v_file = EmptyStr then Exit;
f_threadDigest := TThreadDigest.Create(_SUSPENDED);
try
f_threadDigest.OnFileOpen := Self.OnFileOpen;
f_threadDigest.OnRead := Self.OnRead;
f_threadDigest.OnDone := Self.Done;
f_threadDigest.FileName := v_file;
f_threadDigest.Start;
//f_threadDigest.WaitFor;
finally
//FreeAndNil(f_threadDigest);
end;
end;
procedure TFPrincipal.btFecharClick(Sender: TObject);
begin
Self.Close;
end;
procedure TFPrincipal.Done(var md5: string);
begin
edResMD5.Text := f_threadDigest.MD5String;
edResSha256.Text := f_threadDigest.SHA256String;
f_threadDigest.Terminate;
if edMD5.Text <> EmptyStr then
begin
if edResMD5.Text = edMD5.Text then
begin
pnMD5Status.Color := clLime;
pnMD5Status.Caption := 'MD5 OK';
end else
begin
pnMD5Status.Color := clRed;
pnMD5Status.Caption := 'CORROMPIDO';
end;
end else
begin
pnMD5Status.Color := clBlue;
pnMD5Status.Caption := '...';
end;
if edSHA256.Text <> EmptyStr then
begin
if edResSHA256.Text = edSHA256.Text then
begin
pnSHA256Status.Color := clLime;
pnSHA256Status.Caption := 'MD5 OK';
end else
begin
pnSHA256Status.Color := clRed;
pnSHA256Status.Caption := 'CORROMPIDO';
end;
end else
begin
pnSHA256Status.Color := clBlue;
pnSHA256Status.Caption := '...';
end;
pnStatus.Color := clLime;
pnStatus.Caption := 'CONCLUIDO';
end;
procedure TFPrincipal.OnFileOpen(const fileSize: Int64);
begin
pnStatus.Color := clYellow;
pnStatus.Caption := 'AGUARDE...';
f_lastPerc := 0;
pbStatus.Position := 0;
if fileSize > MaxInt then
pbStatus.Max := MaxInt
else
pbStatus.Max := fileSize;
lbFile.Caption := 'Tamanho do arquivo: ' + FormatByteSize(fileSize);
end;
procedure TFPrincipal.OnRead(const bytesRead, fileSize: Int64);
var
currPerc: Integer;
begin
currPerc := Round(bytesRead / fileSize * pbStatus.Max);
if currPerc > f_lastPerc then
begin
f_lastPerc := currPerc;
pbStatus.Position := currPerc;
pbStatus.Update;
end;
FPrincipal.lbStatus.Caption := 'Bytes lidos: ' + FormatByteSize(bytesRead);
end;
end.