The program is compiling and running, however the output into ftest2 is just 0's (129 to be exact), as opposed to the hash value. I've an inotify_event set before the snprintf's to monitor the directory (which functions correctly) however for readability/simplicity I've removed this. hashInBuf & hashOutBuf subsequently DO create correct files / locations.

int main (int argc, char *argv[])
{
        int fd;
        unsigned char buffer[BUFSIZ];
        unsigned int md_len, i;
        size_t len2;
        unsigned char md_value[EVP_MAX_MD_SIZE];
        EVP_MD_CTX *hashctx;
        const EVP_MD *hashptr = EVP_get_digestbyname("SHA512");
        const int event_size = sizeof(struct inotify_event);
        const int buf_len = 1024 * (event_size + FILENAME_MAX);
        char *directory = "/home/joe/Documents/";
        char *hashDirectory = "/home/joe/Documents/_Hash/";
        char hashInBuf[500];
        char hashOutBuf[500];
        char file512[500];

                //Conjoin directory and filename
                snprintf(hashInBuf, sizeof(hashInBuf), "%s/%s", directory, event->name);
                snprintf(hashOutBuf, sizeof(hashOutBuf), "%s/%s.txt", hashDirectory, event->name);

                FILE *ftest=fopen(hashInBuf, "rb");

                hashctx = EVP_MD_CTX_create();
                EVP_DigestInit_ex(hashctx, hashptr, NULL);

                do {
                  len2 = fread(buffer, 1, BUFSIZ, ftest);
                  EVP_DigestUpdate(hashctx, buffer, len2);
                } while (len2 == BUFSIZ);
                unsigned int outlen;
                EVP_DigestFinal_ex(hashctx, buffer, &outlen);
                EVP_MD_CTX_destroy(hashctx);
                if(access(hashOutBuf, F_OK) == -1){
                FILE *ftest2=fopen(hashOutBuf, "wt");
                for(i = 0; i < outlen; i++){
                  fprintf(ftest2, "%02x", md_value[i]);
                  }
                  printf("File hash complete\n");
                  fclose(ftest2);
                  fclose(ftest);
                }

I was testing buffers / size_t hence why there are a few unnecessary things below main and dodgy indentation. I've been attempting to update the hash function to the new EVP method.

0 Answers