-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathget_dgst.c
103 lines (78 loc) · 1.99 KB
/
get_dgst.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* get_dgst.c
*
* Created on: Nov 19, 2015
* Author: tslld
*/
#include "ecdsa.h"
#include "hash_functions.h"
/** Given a file. Function hashes and returns a string
* \param in_fname name of the input file
* \return hash digest of the file in_fname
*/
char* get_dgst_224(const char* in_fname) {
char *hash = malloc(SHA224_DIGEST_STRING_LENGTH);
hash[SHA224_DIGEST_STRING_LENGTH] = '\0';
FILE *msg_fp = NULL;
SHA224_Context ctx;
uchar buf[1000];
uchar sha224sum[SHA224_DIGEST_LENGTH];
int i;
if (!(msg_fp = fopen( in_fname, "rb"))) {
hash = sha224(in_fname);
} else {
sha224_init( &ctx );
while ((i = fread( buf, 1, sizeof(buf), msg_fp )) > 0) {
sha224_update(&ctx, buf, i);
}
sha224_final(&ctx, sha224sum);
for(i = 0; i < SHA224_DIGEST_LENGTH ; ++i) {
sprintf(hash +i*2, "%02x", sha224sum[i]);
}
}
return hash;
}
char* get_dgst_256(const char* msg) {
int i;
SHA256_Context ctx;
uchar buf[1000];
uchar sha256sum[SHA256_DIGEST_LENGTH];
char* hash = malloc(SHA256_DIGEST_STRING_LENGTH);
hash[SHA256_DIGEST_STRING_LENGTH] = '\0';
FILE *msg_fp = NULL;
if (!(msg_fp = fopen( msg, "rb"))) {
hash = sha256(msg);
} else {
sha256_init( &ctx );
while ((i = fread( buf, 1, sizeof(buf), msg_fp )) > 0) {
sha256_update(&ctx, buf, i);
}
sha256_final(&ctx, sha256sum);
for(i = 0; i < SHA256_DIGEST_LENGTH ; ++i) {
sprintf(hash +i*2, "%02x", sha256sum[i]);
}
}
return hash;
}
char* get_dgst_384(const char* msg) {
int i;
SHA384_Context ctx;
uchar buf[1000];
uchar sha384sum[SHA384_DIGEST_LENGTH];
char* hash = malloc(SHA384_DIGEST_STRING_LENGTH);
hash[SHA384_DIGEST_STRING_LENGTH] = '\0';
FILE *msg_fp = NULL;
if (!(msg_fp = fopen( msg, "rb"))) {
hash = sha384(msg);
} else {
sha384_init( &ctx );
while ((i = fread( buf, 1, sizeof(buf), msg_fp )) > 0) {
sha384_update(&ctx, buf, i);
}
sha384_final(&ctx, sha384sum);
for(i = 0; i < SHA384_DIGEST_LENGTH ; ++i) {
sprintf(hash +i*2, "%02x", sha384sum[i]);
}
}
return hash;
}