-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_blake2.c
31 lines (27 loc) · 938 Bytes
/
hash_blake2.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/blake2.h>
#include "hash.h"
void calculate_blake2(const char *filename) {
unsigned char hash[BLAKE2B_DIGEST_LENGTH]; // Change to BLAKE2S_DIGEST_LENGTH for BLAKE2s
BLAKE2b_CTX blake2_ctx; // Use BLAKE2s_CTX for BLAKE2s
BLAKE2b_Init(&blake2_ctx); // Use BLAKE2s_Init for BLAKE2s
FILE *file = fopen(filename, "rb");
if (!file) {
perror("File opening failed");
return;
}
unsigned char buffer[1024];
size_t bytesRead;
while ((bytesRead = fread(buffer, 1, sizeof(buffer), file)) != 0) {
BLAKE2b_Update(&blake2_ctx, buffer, bytesRead); // Use BLAKE2s_Update for BLAKE2s
}
BLAKE2b_Final(hash, &blake2_ctx); // Use BLAKE2s_Final for BLAKE2s
fclose(file);
printf("BLAKE2b: ");
for (int i = 0; i < BLAKE2B_DIGEST_LENGTH; i++) {
printf("%02x", hash[i]);
}
printf("\n");
}