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

Compute a Private Key's JWK Thumbprint

See more Private Key Examples

Demonstrates the Chilkat PrivateKey.GetJwkThumbprint method, which computes the RFC 7638 thumbprint of the key's public portion using a named hash algorithm. The only argument is the hash algorithm (for example sha256).

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: A JWK thumbprint is a stable, canonical hash of a key's public parameters — a short fingerprint that identifies the key independent of formatting. It is widely used as a key ID (kid) in JWK Sets and JWTs, and for comparing whether two representations are the same key. Because it hashes only the public portion, it can be shared freely even though it is computed here from the private key.

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

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

procedure RunDemo;
var
  success: Boolean;
  privKey: TPrivateKey;
  thumbprint: string;

begin
  success := False;

  //  Load a private key to export.
  privKey := TPrivateKey.Create;
  success := privKey.LoadPemFile('qa_data/private.pem');
  if (success = False) then
    begin
      WriteLn(privKey.LastErrorText);
      Exit;
    end;

  //  Compute the RFC 7638 thumbprint of the key's public portion using the named hash algorithm.
  //  Supported values include "sha256", "sha1", "md5", and others.
  thumbprint := privKey.GetJwkThumbprint('sha256');
  if (privKey.LastMethodSuccess = False) then
    begin
      WriteLn(privKey.LastErrorText);
      Exit;
    end;
  WriteLn('JWK thumbprint: ' + thumbprint);


  privKey.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.