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

Cache a DKIM Public Key from a String

See more DKIM / DomainKey Examples

Demonstrates the Chilkat Dkim.LoadPublicKey method, which caches an RSA public key for a specific selector and domain. The arguments are the selector, the domain, and the public key as a string (PEM, or the base64 p= value from the DNS record).

Background: By default DkimVerify fetches the public key from DNS, but caching it in advance is useful when DNS is unavailable, when you want deterministic offline verification (in tests, for example), or to avoid repeated lookups. The cache is keyed on the exact selector and domain strings, so they must match the signature's s= and d= tags for the cached key to be used; otherwise verification falls back to DNS.

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.Dkim;

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

procedure RunDemo;
var
  success: Boolean;
  dkim: TDkim;
  publicKey: string;

begin
  success := False;

  //  Demonstrates the Dkim.LoadPublicKey method, which caches an RSA public key for a specific
  //  selector and domain.  The 1st argument is the selector, the 2nd is the domain, and the 3rd is
  //  the public key (as a string).

  dkim := TDkim.Create;

  //  The public key as a string.  This can be PEM, or the base64 p= value from the DNS TXT record.
  publicKey := '-----BEGIN PUBLIC KEY-----' + #10 + 'MIIBIjANBgkq...IDAQAB' + #10 + '-----END PUBLIC KEY-----';

  //  Cache the key under the exact selector and domain.  A later DkimVerify uses this cached key
  //  when the signature's selector and domain match, instead of doing a DNS lookup.
  success := dkim.LoadPublicKey('myselector','example.com',publicKey);
  if (success = False) then
    begin
      WriteLn(dkim.LastErrorText);
      Exit;
    end;
  WriteLn('Public key cached.');


  dkim.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.