Bootstrap

Delphi 2010 获取文件MD5

Delphi 计算文件的MD5哈希值

uses StdCtrls, IdHashMessageDigest, IdGlobal, IdHash;


function CalculateFileMD5(const FileName: string): string;
var
  FileStream: TFileStream;
  MD5Hash: TIdHashMessageDigest5;
  MD5Digest: TIdBytes;
  i: Integer;
begin
  FileStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
  MD5Hash := TIdHashMessageDigest5.Create;

  try
    MD5Digest := MD5Hash.HashStream(FileStream);
    Result := '';

    for i := 0 to Length(MD5Digest) - 1 do
      Result := Result + IntToHex(MD5Digest[i], 2);

  finally
    MD5Hash.Free;
    FileStream.Free;
  end;
end;
;