Search code examples
inno-setuppascalscript

Verify file version against set of known MD5 checksums in Inno Setup


The following code works:

[Code]
var
  ComMD5: Integer;
  FileExit: Boolean;

function InitializeSetup: boolean;
begin
  FileExit := FileExists(ExpandConstant('C:\Program Files (x86)\A\A.exe'));

  if FileExit = false then begin
    MsgBox('File does not exist!', mbInformation, MB_OK);
    Result := False;
  end else begin
    ComMD5 := CompareStr(GetMD5OfFile(ExpandConstant('C:\Program Files (x86)\A\A.exe')), 'c2d33f81e31eb54adf9323c27a9af536');
    if ComMD5 = 0 then begin
    MsgBox('The file version is incorrect, please install the correct version!', mbInformation, MB_OK);
    Result := False;
    end else
    Result := True;
  end;
end;

I want to perform MD5 verification on multiple versions, what should I do?

Condition: A.exe has been installed (there are 5 versions of A.exe successively, MD5 are aMD5/bMD5/cMD5/dMD5/eMD5 respectively).

Use the installation package made by Inno Setup to compare the MD5 of A.exe at runtime.

If equal to aMD5 or bMD5, the installer should continue.

If it is equal to cMD5 or dMD5 or eMD5, it should display

The file version is incorrect, please install the correct version!


Solution

  • Problem has been solved based on:

    [Code]
    function InitializeSetup: boolean;
    var
      Path: string;
      MD5: string;
    begin
      Path := ExpandConstant('{commonpf32}\A\A.exe');
      Result := FileExists(Path);
    
      if not Result then begin
        MsgBox('File does not exist!', mbError, MB_OK);
      end else begin
        MD5 := GetMD5OfFile(Path);
        if (MD5 = 'ab6fb7694839b404b53f0505f4d89f0e') or
           (MD5 = '152f7a2d99d3569202726864f4afbb45') then
        begin
          MsgBox('The file version is incorrect, please install the correct version!',
            mbError, MB_OK);
          Result := False;
        end else
        if (MD5 = '34f871d51120216bed8deefa7147b857') or
           (MD5 = '5cd3e16c3a8374c448346277f11243c8') or
           (MD5 = '8fd90b3061795c553978d020c29d1f0b') then
        begin
          Log('The file version is correct');
        end
          else
        begin
          MsgBox('Unknown file version, please install a supported version!',
            mbError, MB_OK);
          Result := False;
        end;
      end;
    end;