Unicode C
Unicode 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 Unicode C Downloads
#include <C_CkJwsW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkBinDataW.h>
void ChilkatSample(void)
{
BOOL success;
HCkJwsW jws;
HCkJsonObjectW header;
BOOL bIncludeBom;
HCkBinDataW bdKey;
const wchar_t *jwsCompact;
success = FALSE;
jws = CkJwsW_Create();
// Protected header specifying HMAC-SHA256.
header = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(header,L"alg",L"HS256");
success = CkJwsW_SetProtectedHeader(jws,0,header);
if (success == FALSE) {
wprintf(L"%s\n",CkJwsW_lastErrorText(jws));
CkJwsW_Dispose(jws);
CkJsonObjectW_Dispose(header);
return;
}
bIncludeBom = FALSE;
success = CkJwsW_SetPayload(jws,L"This is the content to sign.",L"utf-8",bIncludeBom);
if (success == FALSE) {
wprintf(L"%s\n",CkJwsW_lastErrorText(jws));
CkJwsW_Dispose(jws);
CkJsonObjectW_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 = CkBinDataW_Create();
CkBinDataW_AppendEncoded(bdKey,L"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=",L"base64");
success = CkJwsW_SetMacKeyBd(jws,0,bdKey);
if (success == FALSE) {
wprintf(L"%s\n",CkJwsW_lastErrorText(jws));
CkJwsW_Dispose(jws);
CkJsonObjectW_Dispose(header);
CkBinDataW_Dispose(bdKey);
return;
}
jwsCompact = CkJwsW_createJws(jws);
if (CkJwsW_getLastMethodSuccess(jws) == FALSE) {
wprintf(L"%s\n",CkJwsW_lastErrorText(jws));
CkJwsW_Dispose(jws);
CkJsonObjectW_Dispose(header);
CkBinDataW_Dispose(bdKey);
return;
}
wprintf(L"%s\n",jwsCompact);
CkJwsW_Dispose(jws);
CkJsonObjectW_Dispose(header);
CkBinDataW_Dispose(bdKey);
}