-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashing.c
More file actions
48 lines (41 loc) · 1.08 KB
/
hashing.c
File metadata and controls
48 lines (41 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "hashing.h"
int calculate_file_hash(const char* filename,
unsigned char hash[SHA256_DIGEST_LENGTH]) {
FILE* file = fopen(filename, "rb");
if (file == NULL) {
perror("Error opening file");
return -1;
}
EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
if (mdctx == NULL) {
perror("Error creating context");
fclose(file);
return -1;
}
if (EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL) != 1) {
perror("Error initializing digest");
EVP_MD_CTX_free(mdctx);
fclose(file);
return -1;
}
unsigned char buffer[2 << 13];
size_t bytesRead;
while ((bytesRead = fread(buffer, 1, sizeof(buffer), file)) != 0) {
if (EVP_DigestUpdate(mdctx, buffer, bytesRead) != 1) {
perror("Error updating digest");
EVP_MD_CTX_free(mdctx);
fclose(file);
return -1;
}
}
unsigned int digestLength;
if (EVP_DigestFinal_ex(mdctx, hash, &digestLength) != 1) {
perror("Error finalizing digest");
EVP_MD_CTX_free(mdctx);
fclose(file);
return -1;
}
EVP_MD_CTX_free(mdctx);
fclose(file);
return 0;
}