-
Notifications
You must be signed in to change notification settings - Fork 1
/
MD5Driver.cpp
200 lines (140 loc) · 4.75 KB
/
MD5Driver.cpp
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
// DRIVER.CC - test driver for the C++/object oriented translation and
// modification of MD5.
// Translation and modification (c) 1995 by Mordechai T. Abzug
// This translation/ modification is provided "as is," without express or
// implied warranty of any kind.
// The translator/ modifier does not claim (1) that MD5 will do what you think
// it does; (2) that this translation/ modification is accurate; or (3) that
// this software is "merchantible." (Language for this disclaimer partially
// copied from the disclaimer below).
/* based on:
MDDRIVER.C - test driver for MD2, MD4 and MD5
Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
rights reserved.
RSA Data Security, Inc. makes no representations concerning either
the merchantability of this software or the suitability of this
software for any particular purpose. It is provided "as is"
without express or implied warranty of any kind.
These notices must be retained in any copies of any part of this
documentation and/or software.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "md5.hh"
// Length of test block, number of test blocks.
#define TEST_BLOCK_LEN 1000
#define TEST_BLOCK_COUNT 1000
static void MD5_timeTrial (void);
static void MD5_testSuite (void);
static void MD5_file (char *);
static void MD5_filter (void);
static void MD5_string (unsigned char *string);
static char *MD5_usage (void);
// Main driver.
int main (int argc, char *argv[]){
int i;
if (argc > 1)
for (i = 1; i < argc; i++)
if (argv[i][0] == '-' && argv[i][1] == 's')
MD5_string ( (unsigned char *) argv[i] + 2);
else if (strcmp (argv[i], "-t") == 0)
MD5_timeTrial ();
else if (strcmp (argv[i], "-x") == 0)
MD5_testSuite ();
else if (strcmp (argv[i], "-h") == 0)
cout << MD5_usage()<< flush;
else if (strcmp (argv[i], "-help")==0)
cout << MD5_usage()<< flush;
else if (argv[i][0] == '-'){
cerr << argv[i] << " is an unknown option.\n" << MD5_usage()<< flush;
exit (1);
}
else
MD5_file (argv[i]);
else
MD5_filter ();
return (0);
}
// Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks.
static void MD5_timeTrial ()
{
MD5 context;
time_t endTime, startTime;
unsigned char block[TEST_BLOCK_LEN];
unsigned int i;
cout << "MD5 time trial. Digesting "<< TEST_BLOCK_LEN << " ";
cout << TEST_BLOCK_COUNT << "-byte blocks ...";
// Initialize block
for (i = 0; i < TEST_BLOCK_LEN; i++)
block[i] = (unsigned char)(i & 0xff);
// Start timer
time (&startTime);
// Digest blocks
for (i = 0; i < TEST_BLOCK_COUNT; i++)
context.update (block, TEST_BLOCK_LEN);
context.finalize();
// Stop timer
time (&endTime);
cout << " done" << endl;
cout << "Digest = " << context << endl;
cout << "Time = "<< (long)(endTime-startTime) << " seconds" << endl;
cout << "Speed = ";
cout << (long)TEST_BLOCK_LEN * (long)TEST_BLOCK_COUNT/(endTime-startTime);
cout << "bytes/second" <<endl;
}
// Digests a reference suite of strings and prints the results.
static void MD5_testSuite ()
{
cout << "MD5 test suite:" << endl;
MD5_string ( (unsigned char*) "");
MD5_string ( (unsigned char*) "a");
MD5_string ( (unsigned char*) "abc");
MD5_string ( (unsigned char*) "message digest");
MD5_string ( (unsigned char*) "abcdefghijklmnopqrstuvwxyz");
MD5_string
( (unsigned char*) "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
MD5_string
( (unsigned char*) "1234567890123456789012345678901234567890\
1234567890123456789012345678901234567890");
}
// Digests a file and prints the result.
static void MD5_file (char *filename){
ifstream file(filename);
if (!file)
cerr << filename <<" can't be opened" << endl;
else {
MD5 context(file);
cout << "MD5 (" << filename << ") = " << context << endl;
}
}
// Digests the standard input and prints the result.
static void MD5_filter ()
{
MD5 context(cin); // ie. run istream version of MD5 on cin.
// Could also run file version of MD5 on stdin.
cout << context << endl;
}
// Digests a string and prints the result.
void MD5_string (unsigned char *string){
MD5 context;
unsigned int len = strlen ( (char *)string);
context.update (string, len);
context.finalize ();
cout << "MD5 (\"" << (char *)string << "\") = " << context <<endl;
}
static char *MD5_usage(){
return "\n\
MD5\n\n\
USAGE:\n\n\
MD5 [-sstring] [-t] [-x] [-h] [-help] [filename]\n\n\
Arguments (may be any combination):\n\
-sstring - digests string\n\
-t - runs time trial\n\
-x - runs test script\n\
-h - displays this message\n\
-help - displays this message\n\
filename - digests file\n\
(none) - digests standard input\n\n";
}