-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRainbow_MD5_O.c
56 lines (50 loc) · 1.55 KB
/
Rainbow_MD5_O.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/md5.h>
// Format the data as a hexadecimal string. The buffer must have
// space for `2 * length + 1` characters.
const char *hexString(unsigned char *data, size_t length, char *buffer) {
const char *hexDigits = "0123456789abcdef";
char *dest = buffer;
for (size_t i = 0; i < length; i++) {
*dest++ = hexDigits[data[i] >> 4];
*dest++ = hexDigits[data[i] & 0x0F];
}
*dest = 0;
return buffer;
}
int main(void) {
FILE *infile = fopen("file.txt", "r");
if (infile == NULL) {
perror("Cannot open input file");
exit(EXIT_FAILURE);
}
FILE *outfile = fopen("MD.txt","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) {
if (lineLength > 0 && line[lineLength - 1] == '\n') {
// Remove newline character
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
char hexBuffer[2 * MD5_DIGEST_LENGTH + 1];
fputs(hexString(md5hash, MD5_DIGEST_LENGTH, hexBuffer), outfile);
fputc('\n', outfile);
}
free(line);
// Close output files
fclose(infile);
fclose(outfile);
}