22858 total geeks with 3297 solutions
Recent challengers:
sharepoint how to
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
innocentius
I win at OSIX, I guess. Onward to the other challenges!
sefo
anilg, new comments are deleted automaticall y because of some abuse recently
anilg
this is plain wierd. I submitted comments twice to article 950, and they dont seem to be there. Something wrong with the comment code?
CodeX
shout-boxes in general are old + the staff thing happened to everyone after an issue 2 months ago
anilg
/me is no longer staff :(

Donate
Donate and help us fund new challenges
Donate!
Due Date: Sep 30
September Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
Email worm wants to
party like it"s
1999 (almost)
Oracle sneaks out
Solaris 10 refresh
Firefox 4 preview
knocks back Jäger
shot
Google search index
splits with
MapReduce
Steve Jobs lectures
devs, dodges
antitrust action
Ex-Sun CEO sees
rosy future in
health
Opensourcers get
personal over
Ellison"s Google
fight
Apple issues
moral
regulations> apps dev guide
Dell launches
Opteron 4100s into
Boxes-o-Cloud
Clegg"s taking away
Your Freedom
Slashdot
Swedish Police Shoe
Database May Tread
On Copyright
Australia"s
National Broadband
Network To Go Ahead
Robots Taught to
Deceive
Apple Relaxes iOS
Development Tool
Restrictions
HDR Video a Reality
Swedish Police Shoe
Database May Tread
on Copyright
New Email Worm
Squirming Through
Windows Users"
Inboxes
Researchers Create
Real Tractor Beams
Broadcom Releases
Source Code For
Drivers
Mozilla Unleashes
JaegerMonkey
Enabled Firefox 4
Article viewer

MD5 File Hash



Written by:xtremejames183
Published by:Nightscript
Published on:2007-07-17 09:54:53
Topic:C
Search OSI about C.More articles by xtremejames183.
 viewed 12304 times send this article printer friendly

Digg this!
    Rate this article :
This code hashes a file using the Message Digest 5 and print its hex code to CHECKSUM.MD5 , like the bsd command (md5/checksum)

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;
}

Did you like this article? There are hundreds more.

Comments:
Frost_Drake
2007-07-18 01:39:16
Is there a point to this?

There is already an article explaining the MD5 algorithm and I seriously doubt anyone here would require a article teaching them how to apply MD5.
Quantris
2007-07-18 22:53:24
Also, it would be better to write the output to stdout. That way I have the option of just copy-pasting the MD5 to where I need it, or redirecting output to a file of my choice.

Quickly looking at the code, it looks like the file to open is hard-coded in the source, making this much less useful. You should accept the filename from the commandline. Further, this program has a hard limit on filesize, which is unnecessarily restrictive for MD5 and uses more memory than needed. You can process the input a block at a time to get the digest.

Finally I must agree with the previous comment -- since we already have an article explaining the MD5 algorithm itself, a posting of actual code is not much use.
niazi587
2007-11-01 10:37:37
if any one of you have the solution of MD5 decryption process then plz mail me on niazi587@yahoo.com
i have studied this article it helped but couldnot solve the porblem,,,
Regards
Data entry
Broccolizz
2009-01-29 17:57:55
md5 , this interesting me

and where's an article that previous comment was talking about.

I'll go ahead to read this interesting article as well

and finally
thank you
Anonymous
2009-08-14 23:24:40
You want this:
http://userpages.umbc.edu/~mabzug1/cs/md5/md5.html

C++ (as well as many other languages) are included here

Do not re-invent the wheel, and use the more refined codes already available online.

--Jurand
Anonymous
2010-02-27 17:43:43
All the world's latest and most authoritative website with all kinds of uniforms. We are recognized worldwide network of sales jersey website. Here you can choose a variety of uniforms such as MLB jerseys, NFL jerseys, NHL jerseys and NBA jerseys. Professional security identification products with credit guarantee, first-class quality and reasonable prices!
--Lin Guowang
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
amisauv
Creating a Lexical Analyzer in C on Tue 9th Dec 11am
#include<stdio.h> #include<string.h> #include<conio.h> #include<ctype.h> /*************************************** ************************* Functions prototype. **************************************** *************************/ void Open_File(
amisauv
Controling digital circuit through computer on Tue 9th Dec 10am
this code access the lpt port.here only 4 of the total 8 pins are used but can be modified for full 8 pins.it has a complete GUI with mouse & keyboard interactive control panel.works well in win98, but not in winxp. #include<stdio.h> #include<conio.
amisauv
/* Computerised Electrical Equipment Control */ /* PC BASED DEVICE CONTROLLER * on Tue 9th Dec 10am
#include<stdio.h> #include<conio.h> #include<dos.h> void main() { void tone(void); int p=0x0378; char ex={"Created By Mrc"}; int j; char ex1={"For Further Details & Improvements"}; int k; char ex2={"Contact : E-mail : anbudan
amisauv
Calendar Program on Tue 9th Dec 10am
This program prints Weekdays of specified date. It even prints calendar of a given year too. /*Ccalendar library*/ #include<stdio.h> #include<string.h> #include<conio.h> int getNumberOfDays(int month,int year) { switch(month) { case
amisauv
Calculator: on Tue 9th Dec 10am
#include"graphics.h" #include"dos.h" #include"stdio.h" #include"math.h" union REGS i,o; char text={ "7","8","9","*","4","5","6","/","1","2", "3","+","0","00",".","-","M","M+", "M-","+/-","MR","MC","x^2","sr","OFF","A C","CE","="}; int s=0,k=0,pass
amisauv
INFECTED CODES WRITTEN IN C\C++ on Tue 9th Dec 10am
This is a simple code that changes system time and date. It is written using c/c++ but can be easily converted to java. #include "stdio.h" #include "process.h" #include "dos.h" int main(void) { struct date new_date; struct date old_date; s
amisauv
A C programme which can print the file name it is kept in on Tue 9th Dec 9am
#include<stdio.h> main(){ printf(”the source file name is %s\n”,__FILE__); } actually __FILE__ is a macro which stands for the file name the programme is kept in and the compiler does the rest .. for you ..
amisauv
BOOTSECTOR EDITOR: on Tue 9th Dec 9am
Code : /*program to save the partion table of your hard disk for future use. it will save your partition table in a file partition.dat */ #include<stdio.h> #include<bios.h> #include<conio.h> #include<stdlib.h> #include<ctype.h> void main () {
amisauv
BLINKING STAR : on Tue 9th Dec 9am
#include<conio.h> #include<graphics.h> #include<stdlib.h> #include<dos.h> void main() { int gdriver=DETECT,gmode; int i,x,y; initgraph(&gdriver,&gmode,"e: cgi"); while(!kbhit()) { x=random(640); y=random(480); setcolor
amisauv
// To print semicolons using C programming without using semicolons any where i on Tue 9th Dec 9am
// To print semicolons using C programming without using semicolons any where in the C code in program. // #include<stdio.h> #include<conio.h> void main() { char a; a=59; if(printf("%c",a)){} getch();

Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
BSD sockets API by skrye

This is a test of your knowledge of the BSD socket interface
C Programming by keoki

This test is aimed at a C programmer that is at an intermediate level.


     
Your Ad Here
 
Copyright Open Source Institute, 2006