How to get MD5 from a file in Delphi?

Asked

Viewed 4,768 times

12

How to get the MD5 of a file in Delphi?

2 answers

9


The code below uses Indy, which accompanies 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;
  • 2

    +1 for using a virtually native component.

4

In the older versions of Delphi(as a 7), the component 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, use:

Uses md5;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
 ShowMessage(MD5Print(MD5String('FoooBarrr')));
end;

To calculate the MD5 of a file, is used:

Uses md5;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
 ShowMessage(MD5Print(MD5File('Arquivo.exe')));
end;

:-)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.