Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.0.0+

Sign an email with a key in the Windows Current User certificate store

See more DKIM Examples

Run on Windows as the account that owns the signing certificate. Copy its thumbprint from Current User > Personal and supply it as hexadecimal digits. The thumbprint selects a certificate. The DKIM signature itself uses RSA-SHA256.

The certificate needs an accessible, exportable RSA private key for GetPrivateKey. Publish its matching public key under the selector in DNS. DKIM recipients use that DNS key to verify the message. An X.509 chain is unnecessary for this step.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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.CertStore,
  Chilkat.JsonObject,
  Chilkat.PrivateKey,
  Chilkat.Dkim,
  Chilkat.Cert,
  Chilkat.BinData;

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

procedure RunDemo;
var
  success: Boolean;
  store: TCertStore;
  search: TJsonObject;
  cert: TCert;
  privateKey: TPrivateKey;
  signer: TDkim;
  message: TBinData;

begin
  success := False;

  //  Assumes Chilkat is unlocked. See Global Unlock Sample.

  store := TCertStore.Create;
  success := store.OpenCurrentUserStore(True);
  if (success <> True) then
    begin
      WriteLn(store.LastErrorText);
      Exit;
    end;
  search := TJsonObject.Create;
  search.UpdateString('thumbprint','YOUR_CERTIFICATE_THUMBPRINT');
  cert := TCert.Create;
  success := store.FindCert(search,cert);
  if (success <> True) then
    begin
      WriteLn(store.LastErrorText);
      Exit;
    end;
  if (cert.HasPrivateKey() <> True) then
    begin
      WriteLn('The selected certificate has no accessible private key.');
      Exit;
    end;
  privateKey := TPrivateKey.Create;
  success := cert.GetPrivateKey(privateKey);
  if (success <> True) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;
  signer := TDkim.Create;
  signer.DkimDomain := 'example.com';
  signer.DkimSelector := 'mail2026';
  signer.DkimAlg := 'rsa-sha256';
  success := signer.SetDkimPrivateKey(privateKey);
  if (success <> True) then
    begin
      WriteLn(signer.LastErrorText);
      Exit;
    end;
  message := TBinData.Create;
  success := message.LoadFile('message.eml');
  if (success <> True) then
    begin
      WriteLn('Could not read message.eml.');
      Exit;
    end;
  success := signer.DkimSign(message);
  if (success <> True) then
    begin
      WriteLn(signer.LastErrorText);
      Exit;
    end;
  success := message.WriteFile('signed.eml');
  if (success <> True) then
    begin
      WriteLn('Could not write signed.eml.');
      Exit;
    end;
  WriteLn('Saved signed.eml.');


  store.Free;
  search.Free;
  cert.Free;
  privateKey.Free;
  signer.Free;
  message.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.