How to get the MD5 from a Delphi file?
How to get the MD5 from a Delphi file?
The code below uses Indy, which comes with Delphi.
uses
IdHashMessageDigest, IdHash;
function MD5DoArquivo(const FileName: string): string;
var
IdMD5: TIdHashMessageDigest5;
FS: TFileStream;
begin
IdMD5 := nil;
FS := nil;
try
IdMD5 := TIdHashMessageDigest5.Create;
FS := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
Result := IdMD5.HashStreamAsHex(FS)
finally
FS.Free;
IdMD5.Free;
end;
end;
In the older versions of the Delphi
(such as 7), the Indy
is not built in by default. If you want to do this without using components, you can use esse código
redistributed by software developers CACIC
.
For example, to calculate the MD5 of a string, we use:
Uses md5;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
ShowMessage(MD5Print(MD5String('FoooBarrr')));
end;
To calculate the MD5 of a file, it is used:
Uses md5;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
ShowMessage(MD5Print(MD5File('Arquivo.exe')));
end;
: -)