forked from guyongqiangx/cryptography
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd2test.c
executable file
·265 lines (226 loc) · 5.38 KB
/
md2test.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <stdio.h> /* printf, fopen, fread, fclose... */
#include <stdlib.h> /* exit */
#include <string.h> /* strlen */
#include <unistd.h> /* getopt */
#include "md2.h"
#define HASH_DIGEST_SIZE 16 /* md2 digest size */
#define FILE_BLOCK_SIZE 1024
/*
* Print a usage message
*/
void usage(const char *argv0)
{
fprintf(stderr,
"Usage:\n"
"Common options: [-x|-f file|-s string|-h]\n"
"Hash a string:\n"
"\t%s -s string\n"
"Hash a file:\n"
"\t%s -f file [-k key]\n"
"-x\tInternal string hash test\n"
"-h\tDisplay this message\n"
, argv0, argv0);
exit(1);
}
/*
* Print a message digest in hexadecimal
*/
static int print_digest(unsigned char *digest)
{
uint32_t i;
for (i = 0; i < HASH_DIGEST_SIZE; i++)
{
printf ("%02x", digest[i]);
}
return 0;
}
struct HASH_ITEM {
char *str;
uint32_t len;
unsigned char md[HASH_DIGEST_SIZE*2];
} hashes[] =
{
{ /* 0 */
"",
0,
"8350e5a3e24c153df2275c9f80692773"
},
{ /* 1 */
"a",
1,
"32ec01ec4a6dac72c0ab96fb34c0b5d1"
},
{ /* 2 */
"abc",
3,
"da853b0d3f88d99b30283a69e6ded6bb"
},
{ /* 3 */
"message digest",
14,
"ab4f496bfb2a530b219ff33031fe06b0"
},
{ /* 4 */
"abcdefghijklmnopqrstuvwxyz",
26,
"4e8ddff3650292ab5a4108c3aa47940b"
},
{ /* 5 */
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
62,
"da33def2a42df13975352846c30338cd"
},
{ /* 6 */
"12345678901234567890123456789012345678901234567890123456789012345678901234567890",
80,
"d5976f79d83d3a0dc9806c3c66f3efd8"
},
};
/*
* Internal digest tests
*/
static int internal_digest_tests(const char *argv0)
{
unsigned char digest[HASH_DIGEST_SIZE];
struct HASH_ITEM *item;
printf ("Internal hash tests for %s:\n", argv0);
for (item=&hashes[0]; item<(&hashes[0]+sizeof(hashes)/sizeof(hashes[0])); item++)
{
printf("%s(\"%s\")\n", argv0, item->str);
MD2((unsigned char*)item->str, item->len, digest);
printf(" Expect: %s\n", item->md);
printf(" Result: ");
print_digest(digest);
printf("\n\n");
}
return 0;
}
/*
* Hash a string and print the digest
*/
static int digest_string(const char *argv0, const unsigned char *string, uint32_t len)
{
unsigned char digest[HASH_DIGEST_SIZE];
printf("%s(\"%s\") = ", argv0, string);
MD2(string, len, digest);
print_digest(digest);
printf("\n");
return 0;
}
/*
* Hash a file and print the digest
*/
static int digest_file(const char *argv0, const char *filename)
{
MD2_CTX c;
FILE *f;
unsigned char digest[HASH_DIGEST_SIZE];
unsigned char buf[FILE_BLOCK_SIZE];
int len = 0;
int rc = 0;
f = fopen(filename, "rb");
if (NULL == f)
{
printf("Can't open file %s\n", filename);
rc = -1;
}
else
{
printf("%s(%s) = ", argv0, filename);
MD2_Init(&c);
while ((len = fread(buf, 1, FILE_BLOCK_SIZE, f)))
{
MD2_Update(&c, buf, len);
}
MD2_Final(digest, &c);
fclose(f);
print_digest(digest);
printf("\n");
rc = 0;
}
return rc;
}
/*
* Hash the standard input and prints the digest
*/
static void digest_stdin(const char *argv0)
{
MD2_CTX c;
int len;
unsigned char digest[HASH_DIGEST_SIZE];
unsigned char buf[HASH_DIGEST_SIZE];
MD2_Init(&c);
while ((len = fread(buf, 1, HASH_DIGEST_SIZE, stdin)))
{
MD2_Update(&c, buf, len);
}
MD2_Final(digest, &c);
printf("%s(stdin) = ", argv0);
print_digest(digest);
printf("\n");
}
/*
* $ md2 -h
* Usage:
* Common options: [-x|-f file|-s string|-h]
* Hash a string:
* md2 -s string
* Hash a file:
* md2 -f file [-k key]
* -x Internal string hash test
* -h Display this message
*/
int main(int argc, char *argv[])
{
int ch;
int hash_internal = 0;
int hash_str = 0;
int hash_file = 0;
int hash_stdin = 0;
char *str = NULL;
uint32_t len = 0;
char *filename = NULL;
while ((ch = getopt(argc, argv, "s:f:xh")) != -1)
{
switch(ch)
{
case 'x':
hash_internal = 1;
break;
case 's':
hash_str = 1;
str = optarg;
len = strlen(str);
break;
case 'f':
hash_file = 1;
filename = optarg;
break;
case 'h':
default: /* '?' */
usage(argv[0]);
break;
}
}
if (argc == 1)
{
hash_stdin = 1;
}
if (hash_internal)
{
internal_digest_tests(argv[0]);
}
if (hash_str)
{
digest_string(argv[0], (unsigned char *)str, len);
}
if (hash_file)
{
digest_file(argv[0], filename);
}
if (hash_stdin)
{
digest_stdin(argv[0]);
}
return 0;
}