#include <fstream>
#include <iostream>
#include <FastCRC.h>
using namespace std;
using namespace FastCRC;
bool CalculateFileChecksum1( const char* filename, char* checksumhex )
{
//Generate checksum using Calculate
unsigned long checksum;
if( CFastCRC32::Calculate( &checksum, filename ) != 0 )
return false;
//Get the hexadecimal representation of checksum
SL_FCRC_ConvertToHex32( checksumhex, checksum, 0 );
return true;
}
bool CalculateFileChecksum2( const char* filename, char* checksumhex )
{
//Generate checksum using Update and Final
fstream file( filename, ios::in | ios::binary );
if( !file )
{
cout << "\nCould not open file: " << filename;
return false;
}
const unsigned int BUFF_SIZE = 1024;
unsigned char buff[ BUFF_SIZE ];
//Instantiate a CFastCRC32 object
CFastCRC32 crcobj;
//Calculate the checksum by calling Update for each block of the file
while( !file.eof() )
{
file.read( ( char* )buff, BUFF_SIZE );
crcobj.Update( buff, file.gcount() );
}
//Do final changes and get the checksum
unsigned long checksum = crcobj.Final(); // Final reinitializes crcobj object for the next use
//Get the hexadecimal representation of checksum
SL_FCRC_ConvertToHex32( checksumhex, checksum, 0 );
file.close();
return true;
}
int main()
{
char buff[ 10 ];
char checksumhex[ SLC_FCRC_MAXHEXSIZE ]; //0 terminated
do
{
//Get the file name from the user
cout << "\nEnter a file name:\n";
char filename[ 256 ];
cin.getline( filename, 256 );
//Calculate checksum using Calculate
if( CalculateFileChecksum1( filename, checksumhex ) )
cout << "\nChecksum calculated using Calculate: " << checksumhex;
//Calculate checksum using Update and Final
if( CalculateFileChecksum2( filename, checksumhex ) )
cout << "\nChecksum calculated using Update and Final: " << checksumhex;
//Continue?
cout << "\nContinue (Y/N)?";
cin.getline( buff, 10 );
} while ( *buff == 'y' || *buff == 'Y' );
return 0;
}
|