Menghitung Check Sum File Executable

Berikut ini jika Anda ingin mengkalkulasi check sum sebuah file executable. Anda dapat menggunakan pustaka ImageHelp. Potongan kode dari Torry’s Tip & Trick berikut akan sangat bermanfaat:
Berikut ini jika Anda ingin mengkalkulasi check sum sebuah file executable. Anda dapat menggunakan pustaka ImageHelp. Potongan kode dari Torry’s Tip & Trick berikut akan sangat bermanfaat:

............

uses ........., ImageHlp;
........................
function ComputePEChecksum(FileName: string): DWORD;
var
  h, hMap: Cardinal;
  pMem: Pointer;
  headersum, checksum, fsizehigh, fsizelow: DWORD;
  nth: PImageNtHeaders;
Label
  cleanup;
begin
  pMem := nil;
  Result := 0;
  headersum := 0;
  checksum  := 0;
  h := Windows.CreateFile(PChar(FileName), GENERIC_READ, FILE_SHARE_READ,
    nil, OPEN_EXISTING, 0, 0);
  if (h = INVALID_HANDLE_VALUE) then
    Exit;
  fsizelow := Windows.GetFileSize(h, Pointer(@fsizehigh));
  hMap := Windows.CreateFileMapping(h, nil, PAGE_READONLY, fsizeHigh, fsizeLow, nil);
  if (hMap = 0) then
    goto cleanup;
  pMem := Windows.MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
  if (pMem = nil) then
    goto cleanup;
  nth := CheckSumMappedFile(pMem, fsizeLow, @headersum, @checksum);
  if (nth = nil) then
    checksum := 0;
  cleanup:
  if (pMem <> nil) then
    Windows.UnmapViewOfFile(pMem);
  if (hMap <> 0) then
    Windows.CloseHandle(hMap);
  if (h <> 0) then
    Windows.CloseHandle(h);
  Result := checksum;
end;

....................
{Contoh penggunaan}
procedure ShowCheckSum;
  x1, x2: DWORD;
begin
  x1 := ComputePEChecksum('c:\1.exe'); // original filename
  x2 := ComputePEChecksum('c:\2.exe');
  WriteLn('Checksum 1: ', x1, #13#10'Checksum 2: ', x2);
end;
......................

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s