Added hash function computation compatible with openssl 3.0

This commit is contained in:
alessandro ferro 2023-04-27 17:20:54 +02:00
parent 046ff8b3cd
commit 8bce8f5e67
1 changed files with 60 additions and 12 deletions

View File

@ -355,6 +355,54 @@ unsigned char* sha1(char *filename)
return hash; return hash;
} }
unsigned char* sha1_openssl3(char *filename){
EVP_MD_CTX *mdctx; // envelope context
const EVP_MD *md; // envelope mode (SHA1)
unsigned char *hash = malloc(EVP_MAX_MD_SIZE * sizeof(unsigned char)); // result will be here
unsigned int digest_len, i;
FILE *f = fopen(filename, "rb");
if (f == NULL)
{
fprintf(stderr, "Couldn't open %s\n", filename);
return NULL;
}
mdctx = EVP_MD_CTX_new();
if (mdctx == NULL) {
return NULL;
}
md = EVP_sha1();
if (!EVP_DigestInit_ex(mdctx, md, NULL)) {
return NULL;
}
#define BYTES_TO_READ_AT_ONCE 512000 // 500KiB
unsigned int bytes; // how many bytes we have actually read from fread
#if BYTES_TO_READ_AT_ONCE > UINT_MAX
#error Trying to read more bytes than what is possible to handle. Recompile using unsigned long or reduce BYTES_TO_READ_AT_ONCE
#endif
unsigned char databuffer[BYTES_TO_READ_AT_ONCE];
while ((bytes = fread(databuffer, 1, BYTES_TO_READ_AT_ONCE, f)) != 0)
{
if (!EVP_DigestUpdate(mdctx, databuffer, bytes)) {
return NULL;
}
}
if (!EVP_DigestFinal_ex(mdctx, hash, &digest_len)) {
return NULL;
}
EVP_MD_CTX_free(mdctx);
// print the result in hexadecimal format
for (i = 0; i < digest_len; i++) {
printf("%02x", hash[i]);
}
fclose(f);
return hash;
}
/// @brief Combines two paths /// @brief Combines two paths
/// @param path1 /// @param path1
/// @param path2 /// @param path2