QuickHash Library C++ Interface

The following sample demonstrates how to use the CHMAC class to calculate the HMAC for a string using RIPEMD128 hash algorithm.

#include <iostream>
#include <QuickHash.h>

using namespace std;
using namespace QuickHash;

void ConvertToHex( char* dest, const unsigned char* src, unsigned int count );

int main()
{
    char buff[ 256 ];
    char key[ 256 ];

    unsigned char hmac[ CHMAC<CRIPEMD128>::DIGESTSIZE ];
    char          hmachex[ CHMAC<CRIPEMD128>::HEXDIGESTSIZE ]; //0 terminated

    do
    {
        //Get the string and the key from the user

        cout << "\nEnter a string: ";
        cin.getline( buff, 256, '\n' );

        cout << "\nEnter a key: ";
        cin.getline( key, 256, '\n' );

        cout << "\nRIPEMD128 HMAC for \"" << buff <<"\" with key \"" << key << "\":";

        //Calculate the HMAC using CalculateHex

        cout << "\nCalculated using CalculateHex:        ";
        
        CHMAC<CRIPEMD128>::CalculateHex( hmachex, (const unsigned char*)buff, strlen( buff ), (const unsigned char*)key, strlen( key ) );

        cout << hmachex;

        //Calculate the HMAC using Calculate

        cout << "\nCalculated using Calculate:           ";

        CHMAC<CRIPEMD128>::Calculate( hmac, (const unsigned char*)buff, strlen( buff ), (const unsigned char*)key, strlen( key ) );

        ConvertToHex( hmachex, hmac, CHMAC<CRIPEMD128>::DIGESTSIZE );

        cout << hmachex;

        //Instantiate a CHMAC object

        CHMAC<CRIPEMD128> hm( (const unsigned char*)key, strlen( key ) );

        //Calculate the HMAC using Update and FinalHex

        cout << "\nCalculated using Update and FinalHex: ";

        hm.Update( (const unsigned char*)buff, strlen( buff ) );
        hm.FinalHex( hmachex ); //FinalHex reinitializes the hm object for the next use 

        cout << hmachex;

        //Calculate the HMAC using Update and Final

        cout << "\nCalculated using Update and Final:    ";

        hm.Update( (const unsigned char*)buff, strlen( buff ) );
        hm.Final( hmac ); //Final reinitializes the hm object for the next use

        ConvertToHex( hmachex, hmac, CHMAC<CRIPEMD128>::DIGESTSIZE );

        cout << hmachex;

        //Continue?

        cout << "\nContinue (Y/N)?";

        cin.getline( buff, 10 );

    }while ( *buff == 'y' || *buff == 'Y' );

    return 0;
}

void ConvertToHex( char* dest, const unsigned char* src, unsigned int count )
{
    static char hex[ 16 ] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    for( unsigned int i = 0; i < count; ++i )
    {
        *dest++ = hex[ *src / 16 ];
        *dest++ = hex[ *src++ % 16 ];
    }

    *dest = '\0';
}

        


 

 

Send Feedback to SlavaSoft Inc. Tell a friend about QuickHash Library