1 |
/* |
2 |
* md5.c by Torben Nielsen |
3 |
* |
4 |
*/ |
5 |
|
6 |
#include <stdlib.h> |
7 |
#include <stdio.h> |
8 |
#include <openssl/md5.h> |
9 |
|
10 |
int main(int argc, char *argv[]) |
11 |
{ |
12 |
FILE *file; |
13 |
unsigned char *output; |
14 |
unsigned char *buf; |
15 |
int num; |
16 |
int size; |
17 |
int i; |
18 |
MD5_CTX *md5struct; |
19 |
|
20 |
size = sizeof(unsigned char); |
21 |
|
22 |
if (argc != 2) |
23 |
{ |
24 |
printf("Usage: md5 <file>\n"); |
25 |
return 1; |
26 |
} |
27 |
|
28 |
file = fopen(argv[1],"r"); |
29 |
|
30 |
if (file == 0) |
31 |
{ |
32 |
printf("Could not open %s\n", argv[1]); |
33 |
return 2; |
34 |
} |
35 |
|
36 |
md5struct = malloc( sizeof(MD5_CTX) ); |
37 |
MD5_Init(md5struct); |
38 |
|
39 |
buf = malloc( size * 128); |
40 |
while (!feof(file)) |
41 |
{ |
42 |
num = fread(buf, size, 128, file); |
43 |
MD5_Update(md5struct, buf, num*size) ; |
44 |
} |
45 |
fclose(file); |
46 |
|
47 |
output = malloc( size * 16); |
48 |
|
49 |
MD5_Final(output, md5struct); |
50 |
|
51 |
for (i=0;i<16;i++) |
52 |
{ |
53 |
printf("%02x",output[i]); |
54 |
} |
55 |
printf(" %s\n", argv[1]); |
56 |
|
57 |
free(md5struct); |
58 |
free(output); |
59 |
|
60 |
return 0; |
61 |
} |