mirror of https://github.com/xfarrow/dircomp.git
Added hash function computation compatible with openssl 3.0
This commit is contained in:
parent
046ff8b3cd
commit
8bce8f5e67
48
dircomp.c
48
dircomp.c
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue