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

Export a Public Key as DER into a BinData

Demonstrates the Chilkat PublicKey.GetDerBd method, which writes the public key as binary DER into a BinData. The first argument selects the representation (as for GetPem) and the second is the BinData that receives the bytes (its contents are replaced).

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: The in-memory binary export: the raw DER lands in a BinData ready to hash, transmit, or hand to another API without a temporary file. It is the BinData counterpart to the text exporters, and the first argument picks the same PKCS #1 versus SubjectPublicKeyInfo structure.

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

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

procedure RunDemo;
var
  success: Boolean;
  pubKey: TPublicKey;
  preferPkcs1: Boolean;
  bd: TBinData;

begin
  success := False;

  //  Load a public key to export.  LoadFromFile inspects the content, so it accepts DER, PEM, XML,
  //  JWK, and other supported representations.
  pubKey := TPublicKey.Create;
  success := pubKey.LoadFromFile('qa_data/public.pem');
  if (success = False) then
    begin
      WriteLn(pubKey.LastErrorText);
      Exit;
    end;

  //  Write the public key as binary DER into a BinData.  The 1st argument selects the representation
  //  (as for GetPem) and the 2nd is the BinData that receives the bytes (its contents are replaced).
  preferPkcs1 := False;
  bd := TBinData.Create;
  success := pubKey.GetDerBd(preferPkcs1,bd);
  if (success = False) then
    begin
      WriteLn(pubKey.LastErrorText);
      Exit;
    end;
  WriteLn('Exported ' + bd.NumBytes + ' DER bytes.');


  pubKey.Free;
  bd.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.