[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1317
  • Last Modified:

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

Hi i need a simple class win32, visual c/c++ 6.0 NO MFC ofcourse ;-)


like :

class FILE_CHECKSUM
{
private:
public:
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.


Thanx.









0
marco_coder
Asked:
marco_coder
1 Solution
 
brettmjohnsonCommented:
Here is a platform-independent C implementation of MD5:
http://sourceforge.net/project/showfiles.php?group_id=42360

Although "Advanced Checksum Verifier" utility seems to be what you want:
http://www.irnis.net/gloss/sha-checksum.shtml
0
 
fridomCommented:
you also will find something for that in the openssl library
http://www.openssl.org
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;
   }
 
  OpenSSL_add_all_digests();
  md = EVP_get_digestbyname("MD5");
  EVP_MD_CTX_init(&mdctx);
  EVP_DigestInit_ex(&mdctx, md, NULL);
  EVP_DigestUpdate(&mdctx, content, (size_t) len);
  EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
  EVP_MD_CTX_cleanup(&mdctx);
 
  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;
  }
  close(in_fd);
  md5_sum = calculate_md5_of(file_content, size_of_file);
  if (NULL == md5_sum){
      goto clean;
  }

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

 clean:
  if (file_content) free(file_content);
  if (in_fd > 0) close(in_fd);
  exit(EXIT_FAILURE);

}

Regards
Friedrich
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now