FILE checksum of a file in c/c++ ? 2 ways needed.

Posted on 2006-05-28
Last Modified: 2008-02-01
Hi i need a simple class win32, visual c/c++ 6.0 NO MFC ofcourse ;-)

like :

char * get_MD5_sum(char * filename);
char * get_SHA_SUM(char* filename);


I am looking for something SIMPLE nothing fancy.
The reason is that i need to know when someone has messed up my pc, aka
hacked it, trojanned it, infected it.....

Thats why i want to write this tool.


Question by:marco_coder
    LVL 23

    Accepted Solution

    Here is a platform-independent C implementation of MD5:

    Although "Advanced Checksum Verifier" utility seems to be what you want:
    LVL 24

    Expert Comment

    you also will find something for that in the openssl library
    Here and example for calculating the md5 sum:
    (ommitting much of the recommended error handling)
    Implemented on Linux Debian AMD 64 gcc 4.x

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>

    #include <openssl/evp.h>

    unsigned char * calculate_md5_of(void *content, ssize_t len){
      EVP_MD_CTX mdctx;
      const EVP_MD *md;
      unsigned char *md_value[EVP_MAX_MD_SIZE];
      unsigned char *result = malloc(EVP_MAX_MD_SIZE * 2);
      char as_hex[10];
      unsigned int md_len, i;

      if (NULL == result) {
         return NULL;
      md = EVP_get_digestbyname("MD5");
      EVP_DigestInit_ex(&mdctx, md, NULL);
      EVP_DigestUpdate(&mdctx, content, (size_t) len);
      EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
      for(i = 0; i < md_len; i++){
        sprintf(as_hex, "%02x", md_value[i]);
        strcat(result, as_hex);

      return result;

    int main (void) {
      char *file_content = NULL;
      char *file_name = "t1.txt";
      struct stat stat_buf;
      int i_rval = 0;
      int in_fd = -1;
      off_t size_of_file;
      ssize_t read_bytes;
      unsigned char *md5_sum;

      i_rval = stat(file_name, &stat_buf);
      size_of_file = stat_buf.st_size;
      file_content = malloc(size_of_file);
      if (NULL == file_content){
        goto clean;
      in_fd = open(file_name, 0, O_RDONLY);
      if (in_fd < 0 ){
        goto clean;
      /* slurp in all from the file at once */
      read_bytes = read(in_fd, file_content, size_of_file);
      if ( read_bytes < 0 ) {
        fprintf(stderr, "something has gone wrong while reading from the file\n");
        goto clean;
      md5_sum = calculate_md5_of(file_content, size_of_file);
      if (NULL == md5_sum){
          goto clean;

      printf("md5 of file = %s\n", md5_sum);
      return 0;

      if (file_content) free(file_content);
      if (in_fd > 0) close(in_fd);



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
    A short article about problems I had with the new location API and permissions in Marshmallow
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now