MD5 File Hashing - match Delphi output with PHP md5_file function

人盡茶涼 提交于 2019-11-30 19:07:38

问题


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


回答1:


Compare your results with:

  • md5(file_get_contents( )) in PHP

  • hash("md5", ) from the PHP hash framework extension

  • the command line programs md5(1) aka md5sum(1)

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




回答2:


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.



来源:https://stackoverflow.com/questions/1952929/md5-file-hashing-match-delphi-output-with-php-md5-file-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!