HTTP JSON POST in C/C++ For Thales CipherTrust Tokenization (CT-VL)

Reading Time: < 1 minute

Hello There!

When i get bored in Java or PHP, i do sometime explore new thing in C/C++. I usually wanted to know more in difference way of programming language.

The goal of the code that i created is very simple, which only doing a few requirements like below.

  1. Create HTTP JSON Post Request
  2. Create HTTP JSON Post Response
  3. Parse the JSON
  4. Do logging
  5. Debugging if necessary
#include <curl/curl.h>
#include <json/json.h>
#include "url_helper.h"
using namespace std;

int main() {

    string token_url        = "https://your_cts_domain/vts/rest/v2.0/tokenize";
    string detoken_url      = "https://your_cts_domain/vts/rest/v2.0/detokenize";
    string username_token   = "user_token", password_token = "password_token",
           username_detoken = "user_token", password_detoken = "password_token";
    Json::Value response, token, detoken;

    //initiate the data that need to tokenize
    for(int i=0;i<5;i++){
        Json::Value rootToken;
        char data[100];
        snprintf(data,100,"This is my house that want to token %d",i);
        rootToken["tokengroup"]      = "group1";
        rootToken["tokentemplate"]   = "token_alphanum";
        rootToken["data"]            = data;
        token.append(rootToken);
    }

    //do tokenize and parse the results
    if(hitTheUrl(token_url, username_token, password_token, response, token)){
        u_int size_token = response.size();
        for(int i=0;i<size_token;i++){
            char print_token[100];
            int max_len_token = sizeof print_token;
            const char *tokenString = response[i]["token"].asCString();
            snprintf(print_token,max_len_token,"index = [%d] token = [%s] \n",i, tokenString);
            cout << print_token;

            //try to construct json to perform detokenization
            Json::Value rootDetoken;
            rootDetoken["tokengroup"]      = "group1";
            rootDetoken["tokentemplate"]   = "token_alphanum";
            rootDetoken["token"]            = tokenString;
            detoken.append(rootDetoken);

        }
    }


    //do detokenize and parse the results
    if(hitTheUrl(detoken_url, username_detoken, password_detoken, response, detoken)) {
        u_int size_detoken = response.size();
        for(int i=0;i<size_detoken;i++) {
            char print_detoken[100];
            int max_len_detoken = sizeof print_detoken;
            const char *dataString = response[i]["data"].asCString();
            snprintf(print_detoken, max_len_detoken, "index = [%d] data = [%s] \n", i, dataString);
            cout << print_detoken;
        }
    }

    return 0;
}

You can look and check, the complete code here. If you haven’t know yet, what is Thales CipherTrust Tokenization, you may also find it here.

That’s all folks! Happy coding! πŸ₯°πŸ˜

Note : Tested on MAC OS with apple M1 and CLion IDE

← How to sign a jar file from your *.jks file Simple DateUtils Class in Java β†’

Leave a Reply

Your email address will not be published. Required fields are marked *