How to get MD5 from a file in Delphi?

12

How to get the MD5 from a Delphi file?

    
asked by anonymous 11.02.2014 / 21:24

2 answers

8

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;
    
11.02.2014 / 21:24
4

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;

: -)

    
15.03.2014 / 00:58