#include <stdio.h>
#include <string.h>
#include <windows.h>
/* All function indices in QuickHash.dll can be found in:*/
/* QuickHash Library Help (QuickHash Library Run-Time Dynamic Linking page) */
#define MD5_INIT_INDEX 51
#define MD5_UPDATE_INDEX 52
#define MD5_FINAL_INDEX 53
#define MD5_FINALHEX_INDEX 55
#define MD5_CALCULATE_INDEX 54
#define MD5_CALCULATEHEX_INDEX 56
/* These constants can be found in QuickHash.h */
#define MD5_DIGESTSIZE 16
#define MD5_HEXDIGESTSIZE ( MD5_DIGESTSIZE * 2 + 1 )
#define MD5_CONTEXTSIZE 88
/* Declare pointer types for each function you need to use */
typedef void ( _stdcall *PINIT )( void* );
typedef void ( _stdcall *PUPDATE )( void*, const void*, unsigned int );
typedef void ( _stdcall *PFINAL )( void*, void* );
typedef void ( _stdcall *PFINALHEX )( void*, void*, int );
typedef void ( _stdcall *PCALCULATE )( void*, const void*, unsigned int );
typedef void ( _stdcall *PCALCULATEHEX )( void*, const void*, unsigned int, int );
void ConvertToHex( char* dest, const unsigned char* src, unsigned int count );
int main()
{
PINIT pInit;
PUPDATE pUpdate;
PFINAL pFinal;
PFINALHEX pFinalHex;
PCALCULATE pCalculate;
PCALCULATEHEX pCalculateHex;
char buff[ 256 ];
unsigned char digest[ MD5_DIGESTSIZE ];
char digesthex[ MD5_HEXDIGESTSIZE ]; /*0 terminated*/
unsigned char context[ MD5_CONTEXTSIZE ];
/*****Load QuickHash.dll*************/
HMODULE hModule = LoadLibrary( "QuickHash.dll" );
if( !hModule )
{
printf( "Could not load QuickHash.dll library" );
return 1;
}
/*****Get all function addresses*****/
pInit = (PINIT)GetProcAddress( hModule, (LPCSTR)MD5_INIT_INDEX );
pUpdate = (PUPDATE)GetProcAddress( hModule, (LPCSTR)MD5_UPDATE_INDEX );
pFinal = (PFINAL)GetProcAddress( hModule, (LPCSTR)MD5_FINAL_INDEX );
pFinalHex = (PFINALHEX)GetProcAddress( hModule, (LPCSTR)MD5_FINALHEX_INDEX );
pCalculate = (PCALCULATE)GetProcAddress( hModule, (LPCSTR)MD5_CALCULATE_INDEX );
pCalculateHex = (PCALCULATEHEX)GetProcAddress( hModule, (LPCSTR)MD5_CALCULATEHEX_INDEX );
do
{
/*****Get the string from the user****************************************/
printf( "\nEnter a string: " );
gets( buff );
printf( "\nDigest for \"%s\":", buff );
/*****Calculate the digest using CalculateHex*****************************/
printf( "\nCalculated using CalculateHex: " );
( *pCalculateHex )( digesthex, buff, strlen( buff ), 0 );
printf( "%s", digesthex );
/*****Calculate the digest using Calculate********************************/
printf( "\nCalculated using Calculate: " );
( *pCalculate )( digest, buff, strlen( buff ) );
ConvertToHex( digesthex, digest, MD5_DIGESTSIZE );
printf( "%s", digesthex );
/*****Initialize the context before calling Update, Final, or FinalHex****/
( *pInit )( context );
/*****Calculate the digest using Update and FinalHex**********************/
printf( "\nCalculated using Update and FinalHex: " );
( *pUpdate )( context, buff, strlen( buff ) );
( *pFinalHex )( context, digesthex, 0 ); /* FinalHex reinitializes the context for the next use */
printf( "%s", digesthex );
/*****Calculate the digest using Update and Final*************************/
printf( "\nCalculated using Update and Final: " );
( *pUpdate )( context, buff, strlen( buff ) );
( *pFinal )( context, digest ); /* Final reinitializes the context for the next use */
ConvertToHex( digesthex, digest, MD5_DIGESTSIZE );
printf( "%s", digesthex );
/*****Continue?***********************************************************/
printf( "\nContinue (Y/N)?" );
gets( buff );
}while( *buff == 'y' || *buff == 'Y' );
FreeLibrary( hModule );
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' };
unsigned int i = 0;
for( ; i < count; ++i )
{
*dest++ = hex[ *src / 16 ];
*dest++ = hex[ *src++ % 16 ];
}
*dest = '\0';
}
|