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

Set the JWS Protected Header

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.SetProtectedHeader, which sets the integrity-protected header for a signer from a JsonObject. The header specifies the signature algorithm (alg).

Background. The protected header is covered by the signature, so it cannot be altered without invalidating the JWS. It is configured before creating the JWS.

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

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

procedure RunDemo;
var
  success: Boolean;
  jws: TJws;
  header: TJsonObject;

begin
  success := False;

  jws := TJws.Create;

  //  Build the JWS protected header.  The "alg" member names the signature algorithm.
  header := TJsonObject.Create;
  header.UpdateString('alg','HS256');

  //  Set the protected header for signer index 0.  The protected header is integrity-protected: it is
  //  covered by the signature.
  success := jws.SetProtectedHeader(0,header);
  if (success = False) then
    begin
      WriteLn(jws.LastErrorText);
      Exit;
    end;
  WriteLn('Protected header set.');


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