Sample code for 30+ languages & platforms
Unicode C

Ibanity HTTP Signature for XS2A, Isabel Connect, Ponto Connect

See more Ibanity Examples

Demonstrates how to add a Signature header for Ibanity HTTP requests.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkJsonObjectW.h>
#include <C_CkDateTimeW.h>
#include <C_CkCrypt2W.h>
#include <C_CkStringBuilderW.h>
#include <C_CkPrivateKeyW.h>
#include <C_CkRsaW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObjectW json;
    const wchar_t *payload;
    HCkDateTimeW dtNow;
    const wchar_t *created;
    HCkCrypt2W crypt;
    HCkStringBuilderW sbDigestHdrValue;
    const wchar_t *request_target;
    HCkStringBuilderW sbSigningString;
    const wchar_t *idempotencyKey;
    const wchar_t *signed_headers_list;
    HCkPrivateKeyW privKey;
    HCkRsaW rsa;
    const wchar_t *sigBase64;
    HCkStringBuilderW sbSigHeaderValue;

    success = FALSE;

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

    // In order to sign your HTTP requests, you have to add 2 headers to the HTTP request: Digest: the digest of the request payload and Signature: the actual signature of the request. 

    // POST /xs2a/customer-access-tokens HTTP/1.1
    // Host: api.ibanity.com
    // Content-Type: application/json
    // Digest: SHA-512=z4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg/SpIdNs6c5H0NE8XYXysP+DGNKHfuwvY7kxvUdBeoGlODJ6+SfaPg==
    // Ibanity-Idempotency-Key: 61f02718-eeee-46e1-b5eb-e8fd6e799c2d
    // Signature: keyId="62f02718-eeee-46e1-b5eb-e8fd6e799c2e",created=1599659223,algorithm="hs2019",headers="(request-target) host digest (created) ibanity-idempotency-key",signature="SjWJWbWN7i0...zsbM="
    // 
    // {"data":{"type":"customerAccessToken", "attributes":{"applicationCustomerReference":"15874569"}}}

    // The payload (body) of the above HTTP request is the JSON.
    // Build the JSON above.
    // Use this online tool to generate code from sample JSON: 
    // Generate Code to Create JSON
    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"data.type",L"customerAccessToken");
    CkJsonObjectW_UpdateString(json,L"data.attributes.applicationCustomerReference",L"15874569");

    payload = CkJsonObjectW_emit(json);
    wprintf(L"payload = %s\n",payload);

    // Step 1: Build the (created) virtual header

    dtNow = CkDateTimeW_Create();
    CkDateTimeW_SetFromCurrentSystemTime(dtNow);
    created = CkDateTimeW_getAsUnixTimeStr(dtNow,FALSE);
    wprintf(L"created = %s\n",created);

    // Step 2: Build the Digest header
    crypt = CkCrypt2W_Create();
    CkCrypt2W_putHashAlgorithm(crypt,L"sha512");
    CkCrypt2W_putEncodingMode(crypt,L"base64");
    CkCrypt2W_putCharset(crypt,L"utf-8");

    sbDigestHdrValue = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbDigestHdrValue,L"SHA-512=");
    CkStringBuilderW_Append(sbDigestHdrValue,CkCrypt2W_hashStringENC(crypt,CkJsonObjectW_emit(json)));

    wprintf(L"%s\n",CkStringBuilderW_getAsString(sbDigestHdrValue));

    // Step 3: Build the (request target) virtual header

    // In order to build the signature you will need a virtual header named (request-target) (the parentheses are important). 
    // The (request-target) is the string concatenation of the HTTP method (in lowercase) with the path and query parameters.
    request_target = L"post /xs2a/customer-access-tokens";

    // Step 4: Build the signing string

    // The signing string is the concatenation of the signed header names (in lowercase) and values separated by a LF.

    // You must always sign the following headers: (request-target), host, (created), digest. 
    // If used, you must also sign the authorization header and any ibanity-* headers, such as ibanity-idempotency-key. 

    sbSigningString = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbSigningString,L"(request-target): ");
    CkStringBuilderW_AppendLine(sbSigningString,request_target,FALSE);
    CkStringBuilderW_Append(sbSigningString,L"host: ");
    CkStringBuilderW_AppendLine(sbSigningString,L"api.ibanity.com",FALSE);
    CkStringBuilderW_Append(sbSigningString,L"digest: ");
    CkStringBuilderW_AppendLine(sbSigningString,CkStringBuilderW_getAsString(sbDigestHdrValue),FALSE);
    CkStringBuilderW_Append(sbSigningString,L"(created): ");
    CkStringBuilderW_AppendLine(sbSigningString,created,FALSE);
    CkStringBuilderW_Append(sbSigningString,L"ibanity-idempotency-key: ");
    idempotencyKey = CkCrypt2W_generateUuid(crypt);
    CkStringBuilderW_Append(sbSigningString,idempotencyKey);

    // Step 5: Build the signed headers list

    // To allow Ibanity to check the signed headers, you must provide a list of the header names. They should be lowercase and in the same order used to create the signing string. 
    signed_headers_list = L"(request-target) host digest (created) ibanity-idempotency-key";

    // Step 6: Build the Signature header

    // This is where the real signing happens. The signature header is a combination of several sub-headers -
    // 
    //     keyId: the identifier for the application's signature certificate, obtained from the Developer Portal
    //     algorithm: the digital signature algorithm used to generate the signature (must be hs2019)
    //     headers: The list of HTTP headers created in step 5
    //     signature: the Base64-encoded digital signature of the signing string created in step 4.

    privKey = CkPrivateKeyW_Create();
    success = CkPrivateKeyW_LoadEncryptedPemFile(privKey,L"my_ibanity_signature_private_key.pem",L"pem_password");
    if (success == FALSE) {
        wprintf(L"%s\n",CkPrivateKeyW_lastErrorText(privKey));
        CkJsonObjectW_Dispose(json);
        CkDateTimeW_Dispose(dtNow);
        CkCrypt2W_Dispose(crypt);
        CkStringBuilderW_Dispose(sbDigestHdrValue);
        CkStringBuilderW_Dispose(sbSigningString);
        CkPrivateKeyW_Dispose(privKey);
        return;
    }

    rsa = CkRsaW_Create();
    CkRsaW_putPssSaltLen(rsa,32);
    CkRsaW_putEncodingMode(rsa,L"base64");
    // Use the RSASSA-PSS signature algorithm
    CkRsaW_putPkcsPadding(rsa,FALSE);

    success = CkRsaW_UsePrivateKey(rsa,privKey);
    if (success == FALSE) {
        wprintf(L"%s\n",CkRsaW_lastErrorText(rsa));
        CkJsonObjectW_Dispose(json);
        CkDateTimeW_Dispose(dtNow);
        CkCrypt2W_Dispose(crypt);
        CkStringBuilderW_Dispose(sbDigestHdrValue);
        CkStringBuilderW_Dispose(sbSigningString);
        CkPrivateKeyW_Dispose(privKey);
        CkRsaW_Dispose(rsa);
        return;
    }

    // Sign the signing string.
    sigBase64 = CkRsaW_signStringENC(rsa,CkStringBuilderW_getAsString(sbSigningString),L"sha-256");
    if (CkRsaW_getLastMethodSuccess(rsa) == FALSE) {
        wprintf(L"%s\n",CkRsaW_lastErrorText(rsa));
        CkJsonObjectW_Dispose(json);
        CkDateTimeW_Dispose(dtNow);
        CkCrypt2W_Dispose(crypt);
        CkStringBuilderW_Dispose(sbDigestHdrValue);
        CkStringBuilderW_Dispose(sbSigningString);
        CkPrivateKeyW_Dispose(privKey);
        CkRsaW_Dispose(rsa);
        return;
    }

    // Build the signature header value.
    sbSigHeaderValue = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbSigHeaderValue,L"keyId=\"");
    // Use your identifier for the application's signature certificate, obtained from the Developer Portal
    CkStringBuilderW_Append(sbSigHeaderValue,L"62f02718-eeee-46e1-b5eb-e8fd6e799c2e");
    CkStringBuilderW_Append(sbSigHeaderValue,L"\",created=");
    CkStringBuilderW_Append(sbSigHeaderValue,created);
    CkStringBuilderW_Append(sbSigHeaderValue,L",algorithm=\"hs2019\",headers=\"");
    CkStringBuilderW_Append(sbSigHeaderValue,signed_headers_list);
    CkStringBuilderW_Append(sbSigHeaderValue,L"\",signature=\"");
    CkStringBuilderW_Append(sbSigHeaderValue,sigBase64);
    CkStringBuilderW_Append(sbSigHeaderValue,L"\"");

    wprintf(L"%s\n",CkStringBuilderW_getAsString(sbSigHeaderValue));


    CkJsonObjectW_Dispose(json);
    CkDateTimeW_Dispose(dtNow);
    CkCrypt2W_Dispose(crypt);
    CkStringBuilderW_Dispose(sbDigestHdrValue);
    CkStringBuilderW_Dispose(sbSigningString);
    CkPrivateKeyW_Dispose(privKey);
    CkRsaW_Dispose(rsa);
    CkStringBuilderW_Dispose(sbSigHeaderValue);

    }