Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Verify DKIM Signatures in a Message

See more DKIM / DomainKey Examples

Demonstrates the Chilkat Dkim.DkimVerify method, which verifies the DKIM-Signature at a given zero-based index in a MIME message. The arguments are the signature index and a BinData holding the complete MIME. This example loops over every signature and reads the JSON VerifyInfo after each.

Tip: JSON parsing code for the VerifyInfo result can be generated at Chilkat Tools.

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: Verifying confirms two things at once: that the message was signed by the claimed domain, and that the signed headers and body have not been altered since. Chilkat uses a cached public key when the selector and domain match one you loaded, and otherwise fetches it from DNS. The message is not modified. After each call, the VerifyInfo JSON records the details — selector, domain, and outcome — which is what you would log or surface when a signature fails. Verification is per-signature, so a message with several signatures may have some pass and others fail.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.BinData,
  Chilkat.Dkim;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  dkim: TDkim;
  mimeData: TBinData;
  numSigs: Integer;
  i: Integer;

begin
  success := False;

  //  Demonstrates the Dkim.DkimVerify method, which verifies the DKIM-Signature at a given index in
  //  a MIME message.  The 1st argument is the zero-based signature index and the 2nd is a BinData
  //  holding the complete MIME.

  dkim := TDkim.Create;

  //  Load the received MIME message.
  mimeData := TBinData.Create;
  success := mimeData.LoadFile('qa_data/received.eml');
  if (success = False) then
    begin
      WriteLn(mimeData.LastErrorText);
      Exit;
    end;

  //  A message may carry more than one DKIM signature.  Verify each in turn.  Signatures are
  //  indexed in top-to-bottom header order.
  numSigs := dkim.NumDkimSigs(mimeData);

  for i := 0 to numSigs - 1 do
    begin
      //  DkimVerify uses a cached public key when one matches the signature's selector and domain;
      //  otherwise it retrieves the key from DNS.
      success := dkim.DkimVerify(i,mimeData);
      if (success = True) then
        begin
          WriteLn('Signature ' + i + ': verified.');
        end
      else
        begin
          WriteLn('Signature ' + i + ': verification failed.');
        end;

      //  VerifyInfo contains JSON describing the most recent DkimVerify call (selector, domain,
      //  result, and so on).
      WriteLn(dkim.VerifyInfo);
      //  JSON parsing code for this result can be generated at Chilkat's online tool:
      //  https://tools.chilkat.io/jsonParse
    end;



  dkim.Free;
  mimeData.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.