Search code examples
delphimd5delphi-7

File MD5 checksum


In this question is mentioned the wcrypt2.

What I need is simply calculate the MD5 of a file. It would be perfect if I could calculate it without having to save it because it is a downloaded file in stream format.

I would like to have the most straightforward way to do that.

Thanks!


Solution

  • Here is a working code for Indy 10:

    function MD5File(const FileName: string): string;
    var
      IdMD5: TIdHashMessageDigest5;
      FS: TFileStream;
    begin
     IdMD5 := TIdHashMessageDigest5.Create;
     FS := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
     try
       Result := IdMD5.HashStreamAsHex(FS)
     finally
       FS.Free;
       IdMD5.Free;
     end;
    end;
    

    Regards, OscaR1