md5sum of file in Linux C

40,381

Solution 1

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

Solution 2

You can use popen to run md5sum and read the output:

#include <stdio.h>
#include <ctype.h>

#define STR_VALUE(val) #val
#define STR(name) STR_VALUE(name)

#define PATH_LEN 256
#define MD5_LEN 32

int CalcFileMD5(char *file_name, char *md5_sum)
{
    #define MD5SUM_CMD_FMT "md5sum %." STR(PATH_LEN) "s 2>/dev/null"
    char cmd[PATH_LEN + sizeof (MD5SUM_CMD_FMT)];
    sprintf(cmd, MD5SUM_CMD_FMT, file_name);
    #undef MD5SUM_CMD_FMT

    FILE *p = popen(cmd, "r");
    if (p == NULL) return 0;

    int i, ch;
    for (i = 0; i < MD5_LEN && isxdigit(ch = fgetc(p)); i++) {
        *md5_sum++ = ch;
    }

    *md5_sum = '\0';
    pclose(p);
    return i == MD5_LEN;
}

int main(int argc, char *argv[])
{
    char md5[MD5_LEN + 1];

    if (!CalcFileMD5("~/testfile", md5)) {
        puts("Error occured!");
    } else {
        printf("Success! MD5 sum is: %s\n", md5);
    }
}

Solution 3

You can use the mhash library (license is LGPL). On Debian systems:

sudo apt-get install libmhash-dev

See the man page man 3 mhash

But I don't think you can just give it the name of a file. You have to open the file yourself, read the data, and feed the data to this library's functions.

Solution 4

An easy answer to the question asked by Raja and using answer from sje397, the md5sum of a file can be calculated within the C program as below. Also notice that there is no need of writing the read command twice when you can use the do while loop.

int calculate_md5sum(char *filename)
{
  //open file for calculating md5sum
  FILE *file_ptr;
  file_ptr = fopen(filename, "r");
  if (file_ptr==NULL)
  {
    perror("Error opening file");
    fflush(stdout);
    return 1;
  }

  int n;
  MD5_CTX c;
  char buf[512];
  ssize_t bytes;
  unsigned char out[MD5_DIGEST_LENGTH];

  MD5_Init(&c);
  do
  {
    bytes=fread(buf, 1, 512, file_ptr);
    MD5_Update(&c, buf, bytes);
  }while(bytes > 0);

  MD5_Final(out, &c);

  for(n=0; n<MD5_DIGEST_LENGTH; n++)
          printf("%02x", out[n]);
  printf("\n");
  return 0;
}
Share:
40,381

Related videos on Youtube

Raja
Author by

Raja

Updated on July 09, 2022

Comments

  • Raja
    Raja almost 2 years

    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.

  • Andre Holzner
    Andre Holzner over 13 years
    thanks for sharing the example here ! (out should be an array of unsigned char and length MD5_DIGEST_LENGTH. And a final printf("\n"); avoids seeing spurious characters on my terminal.)
  • sje397
    sje397 over 13 years
    @Andre Holzner: Fixed. Cheers.
  • nmz787
    nmz787 over 6 years
    When converting this to use a FILE for input, I had to use fread instead of read, otherwise I was getting a different hash
  • unsynchronized
    unsynchronized about 2 years
    this "do.while optimization" is unsafe as it assumes it's ok to call MD5_Update with bytes <=0.