md5sum of file in Linux C

Raja picture Raja · Aug 3, 2010 · Viewed 35.1k times · Source

I want to find md5sum of a file in Linux C, Is there any API where I can send file name to get md5sum of that file.

Answer

sje397 picture sje397 · Aug 3, 2010

There's code here.

Also, the openssl libs have md5 functions (from here):

#include <openssl/md5.h>
#include <unistd.h>
int main()
{
    int n;
    MD5_CTX c;
    char buf[512];
    ssize_t bytes;
    unsigned char out[MD5_DIGEST_LENGTH];

    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]);
    printf("\n");

    return(0);        
}