|
|
SlavaSoft QuickHash Library Samples |
|
|
|
The following sample demonstrates how to use the
CAdler32
class to calculate the
ADLER32 checksum for a string. |
|
#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 checksum[ CAdler32::DIGESTSIZE ];
char checksumhex[ CAdler32::HEXDIGESTSIZE ]; //0 terminated
do
{
//Get the string from the user
cout << "\nEnter a string: ";
cin.getline( buff, 256, '\n' );
cout << "\nChecksum for \"" << buff <<"\":";
//Calculate the checksum using CalculateHex
cout << "\nCalculated using CalculateHex: ";
CAdler32::CalculateHex( checksumhex, (const unsigned char*)buff, strlen( buff ) );
cout << checksumhex;
//Calculate the checksum using Calculate
cout << "\nCalculated using Calculate: ";
CAdler32::Calculate( checksum, (const unsigned char*)buff, strlen( buff ) );
ConvertToHex( checksumhex, checksum, CAdler32::DIGESTSIZE );
cout << checksumhex;
//Instantiate a CAdler32 object
CAdler32 hash;
//Calculate the checksum using Update and FinalHex
cout << "\nCalculated using Update and FinalHex: ";
hash.Update( (const unsigned char*)buff, strlen( buff ) );
hash.FinalHex( checksumhex ); // FinalHex reinitializes the hash object for the next use
cout << checksumhex;
//Calculate the checksum using Update and Final
cout << "\nCalculated using Update and Final: ";
hash.Update( (const unsigned char*)buff, strlen( buff ) );
hash.Final( checksum ); // Final reinitializes the hash object for the next use
ConvertToHex( checksumhex, checksum, CAdler32::DIGESTSIZE );
cout << checksumhex;
//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';
} |
|
|
|