-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash-sha512.c
More file actions
57 lines (45 loc) · 1.26 KB
/
hash-sha512.c
File metadata and controls
57 lines (45 loc) · 1.26 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
49
50
51
52
53
54
55
56
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <openssl/sha.h>
#include "chest.h"
char *ChestHashSHA512FromFile(char *filename) {
FILE *fp = fopen(filename, "rb");
if (fp == NULL) {
printf("ChestHashSHA512FromFile() error: Cannot open %s: %s\n",
filename, strerror(errno));
exit(1);
}
fseek(fp, 0, SEEK_END);
long filesize = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *pw = malloc(filesize);
if (pw == NULL) {
printf("ChestHashSHA512FromString() error: malloc() returned NULL, exiting.\n");
fclose(fp);
exit(ENOMEM);
}
fread(pw, 1, filesize, fp);
fclose(fp);
char *sum = malloc(SHA512_DIGEST_LENGTH);
if (sum == NULL) {
printf("ChestHashSHA512FromFile() error: malloc() returned NULL, exiting.\n");
exit(1);
}
memset(sum, 0, SHA512_DIGEST_LENGTH);
SHA512((const unsigned char *)pw, filesize, (unsigned char *)sum);
free(pw);
return sum;
}
char *ChestHashSHA512FromString(const char *pw) {
char *sum = malloc(SHA512_DIGEST_LENGTH);
if (sum == NULL) {
printf("ChestHashSHA512FromString() error: malloc() returned NULL, exiting.\n");
exit(ENOMEM);
}
memset(sum, 0, SHA512_DIGEST_LENGTH);
SHA512((const unsigned char *)pw, strlen(pw), (unsigned char *)sum);
return sum;
}