Sample code for 30+ languages & platforms
Delphi DLL

Validate a JWS with a Public Key

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.SetPublicKey, which sets the public key used to validate a signature of a loaded JWS.

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 public key verifies a signature created with the corresponding private key. Validate returns 1 when valid, 0 when not, or a negative value on error.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
jws: HCkJws;
jwsCompact: PWideChar;
pubKey: HCkPublicKey;
v: Integer;

begin
success := False;

jws := CkJws_Create();

//  Load the JWS compact serialization.
jwsCompact := 'eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>';
success := CkJws_LoadJws(jws,jwsCompact);
if (success = False) then
  begin
    Memo1.Lines.Add(CkJws__lastErrorText(jws));
    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.
//  Load the signer's public key and set it for validating signature 0.
pubKey := CkPublicKey_Create();
success := CkPublicKey_LoadFromFile(pubKey,'qa_data/signer_pubkey.pem');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPublicKey__lastErrorText(pubKey));
    Exit;
  end;
success := CkJws_SetPublicKey(jws,0,pubKey);
if (success = False) then
  begin
    Memo1.Lines.Add(CkJws__lastErrorText(jws));
    Exit;
  end;

//  Validate the signature.  Validate returns 1 when valid, 0 when not, or a negative value on error.
v := CkJws_Validate(jws,0);
if (v < 0) then
  begin
    Memo1.Lines.Add(CkJws__lastErrorText(jws));
    Exit;
  end;
if (v <> 1) then
  begin
    Memo1.Lines.Add('The signature is not valid.');
    Exit;
  end;
Memo1.Lines.Add('The signature is valid.');

CkJws_Dispose(jws);
CkPublicKey_Dispose(pubKey);