-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMD5_Optimized_Failed.c
44 lines (38 loc) · 1.1 KB
/
MD5_Optimized_Failed.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/md5.h>
int main(void) {
FILE *infile = fopen("infile", "r");
if (infile == NULL) {
perror("Cannot open input file");
exit(EXIT_FAILURE);
}
FILE *outfile = fopen("coutfile","w");
if (outfile == NULL) {
perror("Cannot open output file");
exit(EXIT_FAILURE);
}
// Read file line-by-line
char *line = NULL;
size_t linecap = 0;
ssize_t lineLength;
while ((lineLength = getline(&line, &linecap, infile)) != -1) {
// Remove trailing newline character
if (lineLength > 0 && line[lineLength - 1] == '\n') {
lineLength -= 1;
line[lineLength] = 0;
}
// Compute MD5 hash
unsigned char md5hash[MD5_DIGEST_LENGTH];
MD5((unsigned char*)line, lineLength, md5hash);
// Print hash as hex string
for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
fprintf(outfile, "%02x", md5hash[i]);
}
fprintf(outfile,"\n");
}
free(line);
fclose(infile);
fclose(outfile);
}