Crypto++ Setup in Linux

Download Crypto++ library source from https://www.cryptopp.com/#download

Unzip it and put it in a convenient location

Go inside the directory and run

make
make install

It will install the library in default location

Write the program as shown below to calculate MD5sum

//main.cpp
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include <ctime>
#include <iostream>
#include "cryptopp565/modes.h"
#include "cryptopp565/md5.h"
#include "cryptopp565/hex.h"
using namespace std;

string getMD5Hash(string message){
	CryptoPP::Weak::MD5 hash;
	byte digest[ CryptoPP::Weak::MD5::DIGESTSIZE ];

	hash.CalculateDigest( digest, (byte*) message.c_str(), message.length() );

	CryptoPP::HexEncoder encoder;
	string output;
	encoder.Attach( new CryptoPP::StringSink( output ) );
	encoder.Put( digest, sizeof(digest) );
	encoder.MessageEnd();

	cout << "Calculated MD5 Hash. Mesage: " << message << " | hash: " << output << endl;

	return output;
}

int main()
{
	cout << getMD5Hash("Hello World!") << endl;

	return 0;
}

Then we can compile it using -L/usr/local/lib -lcryptopp flag.

g++ -std=c++11 main.cpp -L/usr/local/lib -lcryptopp

Crypto++ library is very vast. This is a simple example of finding MD5sum.

Parsing Uber bills

It’s the time of the year I have to generate expense reports and I need three details for the invoices. First the invoice number, second the invoice date and the third gross amount paid.

Now the Uber invoice is pdf as shown in figure below:2017-07-27 12_44_30-Clipboard

As opening PDF files and getting data from it manually was kind of painful, so I wrote a python script for my help. Here it is:

import PyPDF2
import os

dirPath = 'C:/path_to_directory_containing_invoices/'
os.chdir(dirPath)
fileList = os.listdir()

for fileName in fileList:

    f = open(dirPath+fileName, 'rb')
    reader = PyPDF2.PdfFileReader(f)
    contents = reader.getPage(0).extractText().split('\n')
    invoice_number = ""
    invoice_date = ""
    gross_amount = ""

    for i in range(len(contents)):
        if contents[i].find("Invoice Number") != -1:
            invoice_number = contents[i].split(': ')[1]
        if contents[i].find("Invoice Date") != -1:
            invoice_date = contents[i].split(': ')[1]
        if contents[i].find("Gross Amount") != -1:
            gross_amount = contents[i+1].split(' ')[0]

    print(invoice_number + ", " + invoice_date + ", " + gross_amount)
    f.close()

It uses PyPDF library. This link tells you how to install it and use it.

Convert XML to Java Classes

To convert a given XML file to Java Classes we need two tools:

  1. xsd-gen
  2. xjc

xsd-gen

xsd-gen is a tool which comes as part of “The Joy of Unix in Windows Tool Bundle”:

Install WizTools and add the location of bin folder to the path C:\Program Files (x86)\WizTools_org\Cli_Tools_Bundle\bin

xjc

xjc comes along with jdk and you may find it in jdk bin path. You have to add jdk bin to the path.

Once above tools are set. We can open cmd [command prompt] and enter following command.

xsd-gen xmlFileName.xml > xsdFileName.xsd

 

This will generate an xsd file which will be used by xjc to convert it to classes.

Tree.xml
Tree.xml
Tree.xsd
Tree.xsd

Once xsd is generated we can use xjc to generate java classes.

xjc -p com.package.name xsdFileName.xsd

 

This will create folder structure as per the package specified and necessary java classes inside the folder.

Radix Sort

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

void printArr ( int arr[], int arrSize )
{
    for ( int i = 0; i < arrSize; i++ ) {
        cout << arr[i] << "   ";
    }
    cout << endl;
}

void radixSort ( int arr[], int arrSize )
{
    const int MaxBuckets = 10;
    //const int MaxBucketSize = 100;
    vector < vector<int> > buckets(MaxBuckets);
    
    //find max value for termination condition
    int maxValue = arr[0];
    for ( int i = 1; i < arrSize; i++ ) {
        if ( arr[i] > maxValue ) {
            maxValue = arr[i];
        }
    }
    
    int radixPos = 0;
    int extractor = pow(10, radixPos);
    while (maxValue / extractor > 0) {
    
        //push the elements to the respective buckets for the radix position
        for ( int i = 0; i < arrSize; i++ ) {
            int bucketIdx = (arr[i] / extractor) % 10;
            buckets[bucketIdx].push_back(arr[i]); 
        }
        
        //put the elements back from the bucket to the array
        int arrIdx = 0;
        for ( int i = 0; i < MaxBuckets; i++ ) {
            for ( int j = 0; j < buckets[i].size(); j++ ) {
                arr[arrIdx++] = buckets[i][j];
            }
        }
        buckets.clear();
        buckets.resize(10);
        cout << "iter "<< radixPos << ": ";
        printArr(arr, arrSize);
        
        radixPos++;
        extractor = pow(10, radixPos);
    }
    
}

int main ( )
{
    cout << "Enter the size: ";
    int arrSize;
    cin >> arrSize;
    
    cout << "Enter the numbers\n";
    int arr[arrSize];
    for ( int i = 0; i < arrSize; i++ ) {
        cin >> arr[i];
    }
    
    radixSort(arr, arrSize);
    
    cout << "--------Array sorted------\n";
    printArr(arr, arrSize);
    
    return 0;
}

Selection Sort

#include <iostream>

using namespace std;

void swap ( int &a, int &b )
{
    int temp = a;
    a = b;
    b = temp;
    return;
}

void printArr ( int arr[], int arrSize )
{
    for ( int i = 0; i < arrSize; i++ ) {
        cout << arr[i] << "   ";
    }
    cout << endl;
}

void selectionSort ( int arr[], int arrSize )
{
    int sortedIndex = 0;
    
    while ( sortedIndex < arrSize ) {
        int min = arr[sortedIndex];
        int minIndex = sortedIndex;
        for ( int i = sortedIndex + 1; i < arrSize; i++ ) {
            if ( arr[i] < min ) {
                min = arr[i];
                minIndex = i;
            }
        }
        swap(arr[minIndex], arr[sortedIndex]);
        cout << "iter " << sortedIndex << ": ";
        printArr(arr, arrSize);
        sortedIndex++;
    }
}

int main ( )
{
    cout << "Enter the size: ";
    int arrSize;
    cin >> arrSize;
    
    cout << "Enter the numbers\n";
    int arr[arrSize];
    for ( int i = 0; i < arrSize; i++ ) {
        cin >> arr[i];
    }
    
    selectionSort(arr, arrSize);
    
    cout << "--------Array sorted------\n";
    printArr(arr, arrSize);
    
    return 0;
}

selection sort image

Insertion Sort

#include <iostream>

using namespace std;

void swap ( int &a, int &b )
{
    int temp = a;
    a = b;
    b = temp;
    return;
}

void printArr ( int arr[], int arrSize )
{
    for ( int i = 0; i < arrSize; i++ ) {
        cout << arr[i] << "   ";
    }
    cout << endl;
}

void insertionSort ( int arr[], int arrSize )
{
    int sortedIndex = 0;
    
    while ( sortedIndex < arrSize ) {
        for ( int i = sortedIndex; i > 0; i-- ) {
            if ( arr[i] < arr[i-1] ) {
                swap(arr[i], arr[i-1]);
            } else {
                break;
            }
        }
        cout << "iter " << sortedIndex << ": ";
        printArr(arr, arrSize);
        sortedIndex++;
    }
}

int main ( )
{
    cout << "Enter the size: ";
    int arrSize;
    cin >> arrSize;
    
    cout << "Enter the numbers\n";
    int arr[arrSize];
    for ( int i = 0; i < arrSize; i++ ) {
        cin >> arr[i];
    }
    
    insertionSort(arr, arrSize);
    
    cout << "--------Array sorted------\n";
    printArr(arr, arrSize);
    
    return 0;
}

Insertion Sort gif