Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Prefetch a DKIM Public Key from DNS
See more DKIM / DomainKey Examples
Demonstrates the Chilkat Dkim.PrefetchPublicKey method, which retrieves a DKIM public key from DNS and caches it for a specific selector and domain. The arguments are the selector and the domain. The TXT lookup is made at selector._domainkey.domain.
Background:
DkimVerify already fetches the key from DNS on demand, so prefetching is about when the lookup happens: doing it up front lets you separate a potentially slow network step from the verification itself, warm the cache before a batch of messages from the same domain, or confirm the key is publishable before relying on it. After a successful prefetch, verification of a matching signature uses the cached key with no further DNS traffic.Chilkat Pascal (Lazarus/Delphi) Downloads
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;
begin
success := False;
// Demonstrates the Dkim.PrefetchPublicKey method, which retrieves a DKIM public key from DNS and
// caches it for a specific selector and domain. The 1st argument is the selector and the 2nd is
// the domain.
dkim := TDkim.Create;
// Retrieve the public key from the DNS TXT record at selector._domainkey.domain and cache it.
// A later DkimVerify for the same selector and domain then uses the cached key.
success := dkim.PrefetchPublicKey('myselector','example.com');
if (success = False) then
begin
WriteLn(dkim.LastErrorText);
Exit;
end;
WriteLn('Public key prefetched from DNS and 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.