C
C
Sign a JWS with an HMAC Key from BinData
See more JSON Web Signatures (JWS) Examples
Demonstrates Jws.SetMacKeyBd, which sets the symmetric MAC key for a signature from the raw bytes in a BinData.
Background. This is the in-memory equivalent of SetMacKey, for when the key material is already available as bytes.
Chilkat C Downloads
#include <C_CkJws.h>
#include <C_CkJsonObject.h>
#include <C_CkBinData.h>
void ChilkatSample(void)
{
BOOL success;
HCkJws jws;
HCkJsonObject header;
BOOL bIncludeBom;
HCkBinData bdKey;
const char *jwsCompact;
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) {
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;
}
// Set the symmetric MAC key for signature 0 from the raw bytes in a BinData. In production, obtain
// the key material from a secure source.
bdKey = CkBinData_Create();
CkBinData_AppendEncoded(bdKey,"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=","base64");
success = CkJws_SetMacKeyBd(jws,0,bdKey);
if (success == FALSE) {
printf("%s\n",CkJws_lastErrorText(jws));
CkJws_Dispose(jws);
CkJsonObject_Dispose(header);
CkBinData_Dispose(bdKey);
return;
}
jwsCompact = CkJws_createJws(jws);
if (CkJws_getLastMethodSuccess(jws) == FALSE) {
printf("%s\n",CkJws_lastErrorText(jws));
CkJws_Dispose(jws);
CkJsonObject_Dispose(header);
CkBinData_Dispose(bdKey);
return;
}
printf("%s\n",jwsCompact);
CkJws_Dispose(jws);
CkJsonObject_Dispose(header);
CkBinData_Dispose(bdKey);
}