For a project that I am working on right now, I was wondering how I could add a progress indicator to the Openssl SHA256 hashing function. I will be working with some large files (1-10+ GB) and would like to be able to see the progress completed versus left. I have implemented the Stack Overflow questions here and here, and have the SHA256 hash generating working correctly (it only lacks a progress indicator).
I am fairly new to C, so this will be a learning experience for me. I thought that I might be able to use fread or SHA256_Update somehow, but I am having trouble understanding exactly how I would get a response of how much of the file has been read.
char _genSHA256Hash(char *path, char outputBuffer[65])
{
FILE *file = fopen(path, "rb");
if(!file) return -1;
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
const int bufSize = 32768;
char *buffer = malloc(bufSize);
int bytesRead = 0;
if(!buffer) return -1;
while((bytesRead = fread(buffer, 1, bufSize, file)))
{
SHA256_Update(&sha256, buffer, bytesRead);
}
SHA256_Final(hash, &sha256);
sha256_hash_string(hash, outputBuffer);
//fclose(file);
//free(buffer);
return 0;
}
EDIT: I have added the sha256_hash_string function:
void sha256_hash_string (unsigned char hash[SHA256_DIGEST_LENGTH], char outputBuffer[65]) {
int i = 0;
for(i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
sprintf(outputBuffer + (i * 2), "%02x", hash[i]);
}
outputBuffer[64] = 0;
}
Thank you,
Tim