Buff/cache increases by
almost 44MB (same as file size) everytime I generate the
hash and free decreases. I believe the file is being
loaded into buffer and not being freed.
I am using the below code to
compute the message digest. This code is part of a
function ComputeHash and the file pointer here is fph.
EVP_add_digest(EVP_sha256());
md
= EVP_get_digestbyname("sha256");
if(!md)
{
printf("Unknown message digest \n");
exit(1);
}
printf("Message
digest algorithm successfully loaded\n");
mdctx
= EVP_MD_CTX_create();
EVP_DigestInit_ex(mdctx,
md, NULL);
//
Reading data to array of unsigned chars
long
long int bytes_read = 0;
printf("FILE
size of the file to be hashed is %ld",filesize);
//reading
image file in chunks below and fph is the file pointer
to the 44MB file
while
((bytes_read = fread (message_data, 1, BUFFER_SIZE,
fph)) != 0)
EVP_DigestUpdate(mdctx,
message_data, bytes_read);
EVP_DigestFinal_ex(mdctx,
hash_data.md_value, &hash_data.md_len);
printf("\n%d\n",EVP_MD_CTX_size(mdctx));
printf("\n%d\n",EVP_MD_CTX_type(mdctx));
hash_data.md_type=EVP_MD_CTX_type(mdctx);
EVP_MD_CTX_destroy(mdctx);
//fclose(fp);
printf("Generated
Digest is:\n ");
for(i
= 0; i < hash_data.md_len; i++)
printf("%02x", hash_data.md_value[i]);
printf("\n");
EVP_cleanup();
return hash_data;
In the the code below, I have done fclose(fp)
verify_hash=ComputeHash(fp,size1);
fclose(fp);
I believe that instead of
loading the entire file all at once I am reading the
44MB file in chunks and computing the hash using the
piece of code below: (fph is the file pointer)
while ((bytes_read = fread
(message_data, 1, BUFFER_SIZE, fph)) != 0)
EVP_DigestUpdate(mdctx,
message_data, bytes_read);
Where I am going wrong? How
can I free the buff/cache after computation of message
digest? Please suggest ways to tackle this.
Thanks and Regards,
Prithiraj