Thread: Anyone know a good, easy to implement MD5 function (for file checksums)

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    161

    Anyone know a good, easy to implement MD5 function (for file checksums)

    Even if I knew where to start, I'd probably write something half as efficient as others have already done, so why try? Does anyone have/know a fast, easy to implement function/library for calculating MD5sums on files?
    Last edited by Viper187; 06-24-2008 at 09:46 AM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    There's a program in the GNU coreutils called "md5sum". I'm not sure what it uses, but it's quite fast. You could execute that program if you're using Linux.

    Ah, here's the source for it, I think. http://www.koders.com/c/fid3D64E21C0...A6C6A9EB4.aspx

    Not sure if that's useful or not . . . Googling for "md5sum c library" shows at least one other implementation.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    Quote Originally Posted by dwks View Post
    There's a program in the GNU coreutils called "md5sum". I'm not sure what it uses, but it's quite fast. You could execute that program if you're using Linux.

    Ah, here's the source for it, I think. http://www.koders.com/c/fid3D64E21C0...A6C6A9EB4.aspx

    Not sure if that's useful or not . . . Googling for "md5sum c library" shows at least one other implementation.
    dammit. Why is everything I find written for linux? I need a function/library that'll actually compile in Windows.

    and why the hell can't I find any CRC examples that are in C, NOT C++? This is utterly annoying.
    Last edited by Viper187; 06-24-2008 at 09:17 AM.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    What's wrong with using OpenSSL's md5 functions?

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Viper187 View Post
    Even if I knew where to start, I'd probably write something half as efficient as others have already done, so why try? Does anyone have/know a fast, easy to implement function/library for calculating MD5sums on files?
    http://cr.yp.to/2004-494/gaim/0.81-src/md5.c

    You'll need to create your own .h file to provide prototypes. There are only a few functions, so that should be simple enough.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I would have thought that there is very little in the md5sum code that is linux specific.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by matsp View Post
    I would have thought that there is very little in the md5sum code that is linux specific.
    I doubt there is anything but purely portable code in the algorithm itself. Don't assume things won't compile in Windows until you've tried it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    http://cr.yp.to/2004-494/gaim/0.81-src/md5.c

    You'll need to create your own .h file to provide prototypes. There are only a few functions, so that should be simple enough.
    There's a md5.h here:
    http://cr.yp.to/2004-494/gaim/0.81-src/md5.h

    I just built that on my Windows machine with
    Code:
    gcc -Wall -o md5 md5.c -DTEST
    . The only two changes I had to make was to include stdio.h in the "TEST" section, and put an "int" in front of the main().

    The second teststring doesn't seem to match the md5 value in the comments to the same string - not sure why that is. All the others match. (The second strings produces: 4d52535c7692376e7e7e205940a93ae9)


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    The more I look at this stuff, I think I'd rather use CRC32/64 for this project, if anyone could point me to an easy to use example in C. I don't like the prospect of dealing with all these char arrays of hashes vs a simple array of 32/64 bit integers. The thing I intend to write will recurse subdirectories looking for duplicate files by comparing checksums.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The code that brewbuck posted a link to is pretty straight forward. CRC32 is not much easier, since it also involves a table of pre-computed values.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Viper187 View Post
    The more I look at this stuff, I think I'd rather use CRC32/64 for this project, if anyone could point me to an easy to use example in C. I don't like the prospect of dealing with all these char arrays of hashes vs a simple array of 32/64 bit integers. The thing I intend to write will recurse subdirectories looking for duplicate files by comparing checksums.
    I don't see what the difference in difficulty is between an array of ints versus an array of chars. CRC32 has a significant possibility of collisions. The code is right there, just use it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 10:08 AM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM