#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/md5.h>

int check_hash(char* filename, char* correct_hash) {
   unsigned char h[MD5_DIGEST_LENGTH];
   FILE *inFile = fopen(filename, "rb");
   MD5_CTX mdContext;
   int bytes;
   unsigned char data[1024];

   if (inFile == NULL) {
      return 0;
   }

   MD5_Init(&mdContext);
   while ((bytes = fread(data, 1, 1024, inFile)) != 0) 
      MD5_Update(&mdContext, data, bytes);
   MD5_Final(h, &mdContext);

   char final_cmp[MD5_DIGEST_LENGTH * 2];
   char *p = final_cmp;
   for(int i = 0; i < MD5_DIGEST_LENGTH; i++) {
      sprintf(p, "%02x", h[i]);
      p = p + 2;
   }

   return strcmp(correct_hash, final_cmp) == 0;
}

int main() {
   char filename[] = "hello_world.sh";
   char correct_hash[] = "aa42f09c74acc950e59fb909d03d32f2";

   if (check_hash(filename, correct_hash)) {
      char* newargv[] = {NULL};
      char* newenv[] = {NULL};
      sleep(3);
      setuid(0);
      int i = execve("./hello_world.sh", newargv, newenv);
      printf("could not execute program\n");
      perror("execve");
      return EXIT_SUCCESS;
   } else {
      printf("Invalid hash, will not execute");
   }
}
