Delphi DLL
Delphi DLL
Sign a JWS with a Private Key
See more JSON Web Signatures (JWS) Examples
Demonstrates Jws.SetPrivateKey, which sets the private key used to create a signature. The example uses RS256 (RSASSA-PKCS1-v1_5 with SHA-256).
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. Public-key JWS signs with a private key so that anyone holding the corresponding public key can verify the signature.
Chilkat Delphi DLL Downloads
var
success: Boolean;
jws: HCkJws;
header: HCkJsonObject;
bIncludeBom: Boolean;
privKey: HCkPrivateKey;
jwsCompact: PWideChar;
begin
success := False;
jws := CkJws_Create();
// Protected header specifying RSASSA-PKCS1-v1_5 with SHA-256 (RS256).
header := CkJsonObject_Create();
CkJsonObject_UpdateString(header,'alg','RS256');
success := CkJws_SetProtectedHeader(jws,0,header);
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;
// 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 private key and set it for signature 0.
privKey := CkPrivateKey_Create();
success := CkPrivateKey_LoadPemFile(privKey,'qa_data/signer_privkey.pem');
if (success = False) then
begin
Memo1.Lines.Add(CkPrivateKey__lastErrorText(privKey));
Exit;
end;
success := CkJws_SetPrivateKey(jws,0,privKey);
if (success = False) then
begin
Memo1.Lines.Add(CkJws__lastErrorText(jws));
Exit;
end;
jwsCompact := CkJws__createJws(jws);
if (CkJws_getLastMethodSuccess(jws) = False) then
begin
Memo1.Lines.Add(CkJws__lastErrorText(jws));
Exit;
end;
Memo1.Lines.Add(jwsCompact);
CkJws_Dispose(jws);
CkJsonObject_Dispose(header);
CkPrivateKey_Dispose(privKey);