C
C
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 C Downloads
#include <C_CkJws.h>
#include <C_CkJsonObject.h>
#include <C_CkPrivateKey.h>
void ChilkatSample(void)
{
BOOL success;
HCkJws jws;
HCkJsonObject header;
BOOL bIncludeBom;
HCkPrivateKey privKey;
const char *jwsCompact;
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) {
printf("%s\n",CkJws_lastErrorText(jws));
CkJws_Dispose(jws);
CkJsonObject_Dispose(header);
return;
}
bIncludeBom = FALSE;
success = CkJws_SetPayload(jws,"This is the content to sign.","utf-8",bIncludeBom);
if (success == FALSE) {
printf("%s\n",CkJws_lastErrorText(jws));
CkJws_Dispose(jws);
CkJsonObject_Dispose(header);
return;
}
// 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) {
printf("%s\n",CkPrivateKey_lastErrorText(privKey));
CkJws_Dispose(jws);
CkJsonObject_Dispose(header);
CkPrivateKey_Dispose(privKey);
return;
}
success = CkJws_SetPrivateKey(jws,0,privKey);
if (success == FALSE) {
printf("%s\n",CkJws_lastErrorText(jws));
CkJws_Dispose(jws);
CkJsonObject_Dispose(header);
CkPrivateKey_Dispose(privKey);
return;
}
jwsCompact = CkJws_createJws(jws);
if (CkJws_getLastMethodSuccess(jws) == FALSE) {
printf("%s\n",CkJws_lastErrorText(jws));
CkJws_Dispose(jws);
CkJsonObject_Dispose(header);
CkPrivateKey_Dispose(privKey);
return;
}
printf("%s\n",jwsCompact);
CkJws_Dispose(jws);
CkJsonObject_Dispose(header);
CkPrivateKey_Dispose(privKey);
}