Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 238
Ricardo Acras Avatar asked Nov 28 '22 16:11

Ricardo Acras


2 Answers

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

like image 154
Oszkar Olah Avatar answered Dec 09 '22 16:12

Oszkar Olah


Based on @dummzeuch answere I wrote this function:

function getMD5checksum(s: TStream): string;
 var
  md5: TIdHashMessageDigest5;
  hash : T4x4LongWordRecord;
 begin
  md5 := TIdHashMessageDigest5.Create;
  s.Seek(0,0);
  hash := md5.HashValue(s);
  result := IntToHex(Integer(hash[0]), 4) +
            IntToHex(Integer(hash[1]), 4) +
            IntToHex(Integer(hash[2]), 4) +
            IntToHex(Integer(hash[3]), 4);
 end;
like image 20
Ricardo Acras Avatar answered Dec 09 '22 14:12

Ricardo Acras