views:

442

answers:

2

I'm currently using this code for md5 hashing in Delphi 7:

function MD5(const fileName : string) : string;
var
  idmd5 : TIdHashMessageDigest5;
  fs : TFileStream;
begin
  idmd5 := TIdHashMessageDigest5.Create;
  fs := TFileStream.Create(fileName, fmOpenRead OR fmShareDenyWrite) ;
  try
    result := idmd5.AsHex(idmd5.HashValue(fs)) ;
  finally
    fs.Free;
    idmd5.Free;
  end;
end;

and I'm trying to get the output the same as the PHP function

md5_file()

I've had a look around and common problems seem to be encoding and not padding with zeroes, but I don't know how to do either of these using TIdHashMessageDigest5 or whether they are already done in the function.

If anyone has any functions they use for this it'd be very appreciated!

Or possibly a way of changing the php function to match the Indy one

+3  A: 

Well, you didn't give a Delphi version number, but if you're on D2007 or later you might want to check out this article.

Mason Wheeler
I'm trying to do this in Delphi 7 but thanks anyway
Mikey
Thanks for mentioning my article!--jeroen
Jeroen Pluimers
The article Mason points to got updated today:Edit: 20091223: Since Delphi 7.01, Indy has provided the unit IdHashMessageDigest which also does md5, see the comments.
Jeroen Pluimers
+4  A: 

Compare your results with:

If all but one agree what the sum is, then you know where to dig.

fsb
thanks, I'll check it out
Mikey
I've found the problem now thanks!
Mikey