Computing an MD5Sum in C | Unix Linux Forums | Programming

  Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Computing an MD5Sum in C

Programming


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-09-2010
snag49ers snag49ers is offline
Registered User
 
Join Date: Mar 2010
Last Activity: 15 July 2010, 9:51 PM EDT
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Computing an MD5Sum in C

Is it possible to call the unix command md5sum from within a C program. I am trying to write a C program that scans a directory and computes the MD5Sum of all the files in the directory. Whenever I use md5sum 'filename' I get the error 'md5sum undeclared'. Is there a header file or some library that I can include? Thanks for your help.
Sponsored Links
    #2  
Old 04-09-2010
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 19 March 2014, 7:13 AM EDT
Posts: 3,039
Thanks: 33
Thanked 652 Times in 631 Posts
you can use it like


Code:
system("md5sum filename");

Sponsored Links
    #3  
Old 04-09-2010
snag49ers snag49ers is offline
Registered User
 
Join Date: Mar 2010
Last Activity: 15 July 2010, 9:51 PM EDT
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks itkamaraj. The problem I'm having now though is that I am scanning the directory so the file that I'm using is stored in entry->d_name and when I call system("md5sum entry->d_name") it is actually trying to use entry->d_name instead of the actual filename. Do you know how I can get around this? Here's some of my code


Code:
while( ( entry = readdir( dp ) ) != NULL )
    {
        lstat( entry->d_name, &statbuf );
        
        if( S_ISDIR( statbuf.st_mode ) )
        {
            if( strcmp( ".", entry->d_name ) == 0 || strcmp( "..", entry->d_name ) == 0 )
            {
                continue;
            }
            
            printf( "%*s%s/\n", depth, "", entry->d_name );
            printdir( entry->d_name, depth+4 );
        }
        else
        {
            unsigned int md5;
            md5 = system("md5sum entry->d_name");
            printf( "%*s%s%d\n", depth, "", entry->d_name, md5 );
        }
        
    }

    #4  
Old 04-09-2010
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 17 April 2014, 1:24 PM EDT
Location: Saskatchewan
Posts: 18,528
Thanks: 680
Thanked 3,036 Times in 2,856 Posts
Your example doesn't sound like C code at all, can you show your program?

There is indeed a library you can include, see 'man md5'. You use it by feeding MD5_Update arrays of data, which you can read from any source.
Code:
#include <openssl/md5.h>
#include <unistd.h>
int main()
{
        int n;
        MD5_CTX c;
        char buf[512];
        ssize_t bytes;

        MD5_Init(&c);
        bytes=read(STDIN_FILENO, buf, 512);
        while(bytes > 0)
        {
                MD5_Update(&c, buf, bytes);
                bytes=read(STDIN_FILENO, buf, 512);
        }

        MD5_Final(out, &c);

        for(n=0; n<MD5_DIGEST_LENGTH; n++)
                printf("%02x", out[n]);

        return(0);        
}

It reads only from stdin in this example. It outputs the same hash as the commandline md5sum command for the same data. You must link the program with -lssl.

Note that MD5 has been cracked; people can generate disparate strings with the same MD5 hash pretty much on command. For simple checksums this may not be important, for things where security is important MD5 is no good, SHA1 is better.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
md5sum and execve nicos Programming 2 12-14-2008 05:37 PM
recursive md5sum check blowFish@ubuntu Shell Programming and Scripting 2 08-05-2008 01:24 AM
Event Cloud Computing - IBM Turning Data Centers Into ?Computing Cloud? Linux Bot Virtualization and Cloud Computing 0 11-15-2007 07:30 PM
the file: MD5SUM samprax UNIX for Dummies Questions & Answers 1 07-05-2002 03:30 AM
What is md5sum??? solvman UNIX for Dummies Questions & Answers 1 09-25-2001 12:31 PM



All times are GMT -4. The time now is 02:27 PM.