Sample code for 30+ languages & platforms
Unicode C

Get a JWS Protected Header

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.GetProtectedH, which copies the decoded protected header of a signature into a JsonObject.

Background. The protected header is integrity-protected by the signature. Inspecting it (for example the alg value) is useful before deciding how to validate.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkJwsW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJwsW jws;
    const wchar_t *jwsCompact;
    HCkJsonObjectW json;

    success = FALSE;

    jws = CkJwsW_Create();

    //  Load the JWS compact serialization.
    jwsCompact = L"eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>";
    success = CkJwsW_LoadJws(jws,jwsCompact);
    if (success == FALSE) {
        wprintf(L"%s\n",CkJwsW_lastErrorText(jws));
        CkJwsW_Dispose(jws);
        return;
    }

    //  Copy the decoded protected header of signature 0 into a JsonObject.
    json = CkJsonObjectW_Create();
    success = CkJwsW_GetProtectedH(jws,0,json);
    if (success == FALSE) {
        wprintf(L"%s\n",CkJwsW_lastErrorText(jws));
        CkJwsW_Dispose(jws);
        CkJsonObjectW_Dispose(json);
        return;
    }

    CkJsonObjectW_putEmitCompact(json,FALSE);
    wprintf(L"%s\n",CkJsonObjectW_emit(json));


    CkJwsW_Dispose(jws);
    CkJsonObjectW_Dispose(json);

    }