Delphi DLL
Delphi DLL
Set the Optional JWS Unprotected Header
See more JSON Web Signatures (JWS) Examples
Demonstrates Jws.SetUnprotectedHeader, which sets the optional unprotected header for a signature.
Background. Unlike the protected header, the unprotected header is not covered by the signature. It is carried in the JSON serialization forms of a JWS, so producing a JWS with an unprotected header yields a JSON serialization rather than compact form.
Chilkat Delphi DLL Downloads
var
success: Boolean;
jws: HCkJws;
header: HCkJsonObject;
unprotected: HCkJsonObject;
bIncludeBom: Boolean;
base64Key: PWideChar;
jwsJson: PWideChar;
begin
success := False;
jws := CkJws_Create();
// Protected header specifying HMAC-SHA256.
header := CkJsonObject_Create();
CkJsonObject_UpdateString(header,'alg','HS256');
success := CkJws_SetProtectedHeader(jws,0,header);
if (success = False) then
begin
Memo1.Lines.Add(CkJws__lastErrorText(jws));
Exit;
end;
// Set the optional unprotected header for signature 0. Unlike the protected header, it is not
// covered by the signature. It is carried in the JSON serialization forms of a JWS.
unprotected := CkJsonObject_Create();
CkJsonObject_UpdateString(unprotected,'kid','signer-key-1');
success := CkJws_SetUnprotectedHeader(jws,0,unprotected);
if (success = False) then
begin
Memo1.Lines.Add(CkJws__lastErrorText(jws));
Exit;
end;
bIncludeBom := False;
success := CkJws_SetPayload(jws,'This is the content to sign.','utf-8',bIncludeBom);
if (success = False) then
begin
Memo1.Lines.Add(CkJws__lastErrorText(jws));
Exit;
end;
base64Key := 'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=';
success := CkJws_SetMacKey(jws,0,base64Key,'base64');
if (success = False) then
begin
Memo1.Lines.Add(CkJws__lastErrorText(jws));
Exit;
end;
// Because an unprotected header is present, the JWS is produced in a JSON serialization form.
jwsJson := CkJws__createJws(jws);
if (CkJws_getLastMethodSuccess(jws) = False) then
begin
Memo1.Lines.Add(CkJws__lastErrorText(jws));
Exit;
end;
Memo1.Lines.Add(jwsJson);
CkJws_Dispose(jws);
CkJsonObject_Dispose(header);
CkJsonObject_Dispose(unprotected);