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

Sign a JWS with a Certificate

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.SetSigningCert, which sets the certificate whose associated private key is used to create a signature.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. The certificate must have an accessible private key. This is convenient when the signing identity is held as a certificate (for example loaded from a PFX).

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.Jws,
  Chilkat.Cert,
  Chilkat.JsonObject;

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

procedure RunDemo;
var
  success: Boolean;
  jws: TJws;
  header: TJsonObject;
  bIncludeBom: Boolean;
  pfxPassword: string;
  cert: TCert;
  jwsCompact: string;

begin
  success := False;

  jws := TJws.Create;

  //  Protected header specifying RSASSA-PKCS1-v1_5 with SHA-256 (RS256).
  header := TJsonObject.Create;
  header.UpdateString('alg','RS256');
  success := jws.SetProtectedHeader(0,header);
  if (success = False) then
    begin
      WriteLn(jws.LastErrorText);
      Exit;
    end;

  bIncludeBom := False;
  success := jws.SetPayload('This is the content to sign.','utf-8',bIncludeBom);
  if (success = False) then
    begin
      WriteLn(jws.LastErrorText);
      Exit;
    end;

  //  The file path is relative to the application's current working directory.  An absolute path
  //  may also be used.  Supply the path appropriate to your own environment.
  //  The PFX password should come from a secure source rather than being hard-coded.
  pfxPassword := 'myPfxPassword';

  //  Load a certificate that has an associated private key, then set it as the signing certificate for
  //  signature 0.  The certificate's private key is used to create the signature.
  cert := TCert.Create;
  success := cert.LoadPfxFile('qa_data/signer.pfx',pfxPassword);
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;
  success := jws.SetSigningCert(0,cert);
  if (success = False) then
    begin
      WriteLn(jws.LastErrorText);
      Exit;
    end;

  jwsCompact := jws.CreateJws();
  if (jws.LastMethodSuccess = False) then
    begin
      WriteLn(jws.LastErrorText);
      Exit;
    end;
  WriteLn(jwsCompact);


  jws.Free;
  header.Free;
  cert.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.