download rar file :
http://www.osix.net:80/modules/folder/index.php?tid=17799&action=df
this is how the algorithm works
- we read the file using the ReadFile() that malloc/memset memory and copy its contents into a buffer
[MAX_BUF] and return a pointer
- hash data using MD5(RFC 1321)
- print hex code to a fresh generate file(CHECKSUM.MD5]
- that's all -- this code is platform independant it compile on WINDOWS/BSD/POSIX
i have added dev-cpp project for win32 and an exe file , on UNIX :
- cc -g -Wall -o checksum main.c md5.c
/*
Copyright (c) 2006, james mrad <xtremejames183@msn.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of the james mrad nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include "md5.h"
//max buffer
#define MAX_BUF 1024
//copy file data into buffer
char *readFile( FILE *fp);
//generate MD5 hash && print it hex code to CHECKSUM.MD5
bool hash_md5_file(const char *file);
// safe strcpy
char *strCopy( char * str );
int main(int argc, char *argv[])
{
if(hash_md5_file("../main.c") == false ){
perror("cannot hash");
}else{
puts("MD5 Hash...OK check CHECKSUM.MD5");
}
#ifdef WIN32
getch();
#endif
return 0;
}
char *readFile( FILE *fp )
{
if( !fp )
{
return NULL;
}
char *output = NULL;
char buf[ MAX_BUF ];
int bytes, byteCount = 0;
memset( buf, 0, sizeof( buf ) );
while( ( bytes = fread( buf, sizeof( char ), sizeof( buf ) - 1, fp ) ) > 0 )
{
if( !output )
{
/* allocate output */
output = ( char * )malloc( bytes + 1 );
memcpy( output, buf, bytes + 1 );
}
else
{
/* already exist - reallocate! */
char *oldStr = strCopy( output );
output = ( char * )realloc( output, byteCount + bytes + 1 );
memcpy( output, oldStr, byteCount );
free( oldStr );
memcpy( output + byteCount, buf, bytes + 1 );
}
byteCount += bytes;
memset( buf, 0, sizeof( buf ) );
}
return output;
}
bool hash_md5_file(const char *file)
{
/* ****read specified file
***return a pointer
***hash using MD5
***save hex into CHECKSUM.MD5
*/
FILE *fp,*out;
char *data;
const char output[]="CHECKSUM.MD5";
static char HEX_DATA[31];
md5_state_t state;
md5_byte_t digest[16];
if(file==NULL){
return false;
}
fp=fopen(file,"rb");
if(fp==NULL){
return false;
}
data=readFile(fp); //malloc and memset are done into ===>readFile()
if(data==NULL){
return false;
}
//MD5 HASH ALGORITHM
md5_init(&state);
md5_append(&state,(const md5_byte_t *)data,strlen(data));
md5_finish(&state,digest);
int i=0;
for(i;i<16;i++){
snprintf(HEX_DATA+i*2,sizeof(HEX_DATA),"%02x",digest[i]);
}
fclose(fp);
out=fopen(output,"a+");
if(out==NULL){
return false;
}
fprintf(out,"MD5 (%s)= ",file);
fprintf(out,"%s\n",HEX_DATA);
//clean && close
fflush(out);
fclose(out);
return true;
}
char *strCopy( char * str )
{
if( !str )
{
return NULL;
}
char *n = ( char * )malloc( strlen( str ) + 1 );
memcpy( n, str, strlen( str ) + 1 );
return n;
}
|