-
Notifications
You must be signed in to change notification settings - Fork 8
/
hash_file.c
35 lines (32 loc) · 905 Bytes
/
hash_file.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
32
33
34
35
/*
This is an example C program using LekKit' SHA256 library
(https://github.com/LekKit/sha256)
Produces a hash for a given file, much like sha256sum CLI utility
*/
#include "sha256.h"
#include <stdio.h>
int main(int argc, char** argv){
if (argc < 2){
printf("Usage: %s [file]\n", argv[0]);
return 0;
}
FILE* file = fopen(argv[1], "rb");
if (!file){
printf("Cannot open file\n");
return 0;
}
char buffer[1024];
size_t size;
struct sha256_buff buff;
sha256_init(&buff);
while (!feof(file)){
/* Hash file by 1kb chunks, instead of loading into RAM at once */
size = fread(buffer, 1, 1024, file);
sha256_update(&buff, buffer, size);
}
char hash[65] = {0}; /* hash[64] is null-byte */
sha256_finalize(&buff);
sha256_read_hex(&buff, hash);
printf("%s\n", hash);
return 0;
}