#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<CTiger>::DIGESTSIZE ];
char hmachex[ CHMAC<CTiger>::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 << "\nTIGER HMAC for \"" << buff <<"\" with key \"" << key << "\":";
//Calculate the HMAC using CalculateHex
cout << "\nCalculated using CalculateHex: ";
CHMAC<CTiger>::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<CTiger>::Calculate( hmac, (const unsigned char*)buff, strlen( buff ), (const unsigned char*)key, strlen( key ) );
ConvertToHex( hmachex, hmac, CHMAC<CTiger>::DIGESTSIZE );
cout << hmachex;
//Instantiate a CHMAC object
CHMAC<CTiger> 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<CTiger>::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';
}
|