#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 ];
unsigned char digest[ CSHA256::DIGESTSIZE ];
char digesthex[ CSHA256::HEXDIGESTSIZE ]; //0 terminated
do
{
//Get the string from the user
cout << "\nEnter a string: ";
cin.getline( buff, 256, '\n' );
cout << "\nDigest for \"" << buff <<"\":";
//Calculate the digest using CalculateHex
cout << "\nCalculated using CalculateHex: ";
CSHA256::CalculateHex( digesthex, (const unsigned char*)buff, strlen( buff ) );
cout << digesthex;
//Calculate the digest using Calculate
cout << "\nCalculated using Calculate: ";
CSHA256::Calculate( digest, (const unsigned char*)buff, strlen( buff ) );
ConvertToHex( digesthex, digest, CSHA256::DIGESTSIZE );
cout << digesthex;
//Instantiate a CSHA256 object
CSHA256 hash;
//Calculate the digest using Update and FinalHex
cout << "\nCalculated using Update and FinalHex: ";
hash.Update( (const unsigned char*)buff, strlen( buff ) );
hash.FinalHex( digesthex ); // FinalHex reinitializes the hash object for the next use
cout << digesthex;
//Calculate the digest using Update and Final
cout << "\nCalculated using Update and Final: ";
hash.Update( (const unsigned char*)buff, strlen( buff ) );
hash.Final( digest ); // Final reinitializes the hash object for the next use
ConvertToHex( digesthex, digest, CSHA256::DIGESTSIZE );
cout << digesthex;
//Continue?
cout << "\nContinue (Y/N)?";
cin.getline( buff, 256 );
} 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';
}
|