Home    | Products    | Downloads    | Purchase    | Support   

 Products

 Paint Express

 HashCalc

 FSUM

 QuickHash Library

    Download

    Purchase

    Samples

    License Agreement

    Related Links

    F.A.Q.

    Overview

 FastCRC Library

 Company

 About Us

 Contact Us

 Miscellaneous

 Affiliate Program

 Site Map

SlavaSoft QuickHash Library Samples
Sample #5 (C++ Interface)  

All

Previous Next


The following sample demonstrates how to use the CHMAC class to calculate the HMAC for a string using RIPEMD160 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<CRIPEMD160>::DIGESTSIZE ];
    char          hmachex[ CHMAC<CRIPEMD160>::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 << "\nRIPEMD160 HMAC for \"" << buff <<"\" with key \"" << key << "\":";

        //Calculate the HMAC using CalculateHex

        cout << "\nCalculated using CalculateHex:        ";
       
        CHMAC<CRIPEMD160>::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<CRIPEMD160>::Calculate( hmac, (const unsigned char*)buff, strlen( buff ), (const unsigned char*)key, strlen( key ) );

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

        cout << hmachex;

        //Instantiate a CHMAC object

        CHMAC<CRIPEMD160> 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<CRIPEMD160>::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';
}
 
  Copyright SlavaSoft Inc. All rights reserved.