Sample code for 30+ languages & platforms
Unicode C

Plaza API (bol.com) HMAC-SHA256 Authentication

See more Encryption Examples

Demonstrates how to compute the Authorization header for bol.com using HMAC-SHA256.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkCrypt2W.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    HCkCrypt2W crypt;
    const wchar_t *publicKey;
    const wchar_t *privateKey;
    const wchar_t *http_verb;
    const wchar_t *content_type;
    const wchar_t *x_bol_date;
    const wchar_t *uri;
    HCkStringBuilderW sb;
    const wchar_t *mac;
    HCkStringBuilderW sbHeader;
    const wchar_t *hdrValue;

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    crypt = CkCrypt2W_Create();

    CkCrypt2W_putEncodingMode(crypt,L"base64");
    CkCrypt2W_putHashAlgorithm(crypt,L"sha256");
    CkCrypt2W_putMacAlgorithm(crypt,L"hmac");

    publicKey = L"oRNWbHFXtAECmhnZmEndcjLIaSKbRMVE";
    privateKey = L"MaQHPOnmYkPZNgeRziPnQyyOJYytUbcFBVJBvbMKoDdpPqaZbaOiLUTWzPAkpPsZFZbJHrcoltdgpZolyNcgvvBaKcmkqFjucFzXhDONTsPAtHHyccQlLUZpkOuywMiOycDWcCySFsgpDiyGnCWCZJkNTtVdPxbSUTWVIFQiUxaPDYDXRQAVVTbSVZArAZkaLDLOoOvPzxSdhnkkJWzlQDkqsXNKfAIgAldrmyfROSyCGMCfvzdQdUQEaYZTPEoA";

    // The string to sign is this:
    // http_verb +'\n\n'+ content_type +'\n'+ x_bol_date +'\n'+ 'x-bol-date:'+ x_bol_date +'\n'+ uri

    http_verb = L"GET";
    content_type = L"application/xml";
    x_bol_date = L"Wed, 17 Feb 2016 00:00:00 GMT";
    uri = L"/services/rest/orders/v2";

    // IMPORTANT: Notice the use of underscore and hyphen (dash) chars in x-bol-date vs. x_bol_date.
    // In one place hypens are used.  In two places, underscore chars are used.
    sb = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sb,http_verb);
    CkStringBuilderW_Append(sb,L"\n\n");
    CkStringBuilderW_Append(sb,content_type);
    CkStringBuilderW_Append(sb,L"\n");
    CkStringBuilderW_Append(sb,x_bol_date);
    CkStringBuilderW_Append(sb,L"\nx-bol-date:");
    CkStringBuilderW_Append(sb,x_bol_date);
    CkStringBuilderW_Append(sb,L"\n");
    CkStringBuilderW_Append(sb,uri);
    wprintf(L"[%s]\n",CkStringBuilderW_getAsString(sb));

    // Set the HMAC key:
    CkCrypt2W_SetMacKeyEncoded(crypt,privateKey,L"ascii");
    mac = CkCrypt2W_macStringENC(crypt,CkStringBuilderW_getAsString(sb));

    // The answer should be: nqzLWvXI1eBhBXrRx5NF23V5hS8Q1xWCloJzPi/RAts=
    wprintf(L"%s\n",mac);

    // The last step is to append the public key with the signature
    sbHeader = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbHeader,publicKey);
    CkStringBuilderW_Append(sbHeader,L":");
    CkStringBuilderW_Append(sbHeader,mac);

    hdrValue = CkStringBuilderW_getAsString(sbHeader);
    wprintf(L"%s\n",hdrValue);


    CkCrypt2W_Dispose(crypt);
    CkStringBuilderW_Dispose(sb);
    CkStringBuilderW_Dispose(sbHeader);

    }