1 // DRIVER.CC - test driver for the C++/object oriented translation and
2 // modification of MD5.
4 // Translation and modification (c) 1995 by Mordechai T. Abzug
6 // This translation/ modification is provided "as is," without express or
7 // implied warranty of any kind.
9 // The translator/ modifier does not claim (1) that MD5 will do what you think
10 // it does; (2) that this translation/ modification is accurate; or (3) that
11 // this software is "merchantible." (Language for this disclaimer partially
12 // copied from the disclaimer below).
16 MDDRIVER.C - test driver for MD2, MD4 and MD5
18 Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
21 RSA Data Security, Inc. makes no representations concerning either
22 the merchantability of this software or the suitability of this
23 software for any particular purpose. It is provided "as is"
24 without express or implied warranty of any kind.
26 These notices must be retained in any copies of any part of this
27 documentation and/or software.
38 // Length of test block, number of test blocks.
40 #define TEST_BLOCK_LEN 1000
41 #define TEST_BLOCK_COUNT 1000
43 static void MD5_timeTrial (void);
44 static void MD5_testSuite (void);
45 static void MD5_file (char *);
46 static void MD5_filter (void);
47 static void MD5_string (unsigned char *string);
48 static char *MD5_usage (void);
52 int main (int argc, char *argv[]){
59 for (i = 1; i < argc; i++)
60 if (argv[i][0] == '-' && argv[i][1] == 's')
61 MD5_string ( (unsigned char *) argv[i] + 2);
62 else if (strcmp (argv[i], "-t") == 0)
64 else if (strcmp (argv[i], "-x") == 0)
66 else if (strcmp (argv[i], "-h") == 0)
67 cout << MD5_usage()<< flush;
68 else if (strcmp (argv[i], "-help")==0)
69 cout << MD5_usage()<< flush;
70 else if (argv[i][0] == '-'){
71 cerr << argv[i] << " is an unknown option.\n" << MD5_usage()<< flush;
83 // Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks.
85 static void MD5_timeTrial ()
88 time_t endTime, startTime;
89 unsigned char block[TEST_BLOCK_LEN];
93 cout << "MD5 time trial. Digesting "<< TEST_BLOCK_LEN << " ";
94 cout << TEST_BLOCK_COUNT << "-byte blocks ...";
97 for (i = 0; i < TEST_BLOCK_LEN; i++)
98 block[i] = (unsigned char)(i & 0xff);
104 for (i = 0; i < TEST_BLOCK_COUNT; i++)
105 context.update (block, TEST_BLOCK_LEN);
112 cout << " done" << endl;
114 cout << "Digest = " << context << endl;
116 cout << "Time = "<< (long)(endTime-startTime) << " seconds" << endl;
119 cout << (long)TEST_BLOCK_LEN * (long)TEST_BLOCK_COUNT/(endTime-startTime);
120 cout << "bytes/second" <<endl;
124 // Digests a reference suite of strings and prints the results.
126 static void MD5_testSuite ()
128 cout << "MD5 test suite:" << endl;
130 MD5_string ( (unsigned char*) "");
131 MD5_string ( (unsigned char*) "a");
132 MD5_string ( (unsigned char*) "abc");
133 MD5_string ( (unsigned char*) "message digest");
134 MD5_string ( (unsigned char*) "abcdefghijklmnopqrstuvwxyz");
136 ( (unsigned char*) "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
138 ( (unsigned char*) "1234567890123456789012345678901234567890\
139 1234567890123456789012345678901234567890");
143 // Digests a file and prints the result.
145 static void MD5_file (char *filename){
147 ifstream file(ConvertFileName(filename));
150 cerr << filename <<" can't be opened" << endl;
153 cout << "MD5 (" << filename << ") = " << context << endl;
157 // Digests the standard input and prints the result.
159 static void MD5_filter ()
162 MD5 context(cin); // ie. run istream version of MD5 on cin.
163 // Could also run file version of MD5 on stdin.
165 cout << context << endl;
170 // Digests a string and prints the result.
172 void MD5_string (unsigned char *string){
175 unsigned int len = strlen ( (char *)string);
177 context.update (string, len);
180 cout << "MD5 (\"" << (char *)string << "\") = " << context <<endl;
185 static char *MD5_usage(){
190 MD5 [-sstring] [-t] [-x] [-h] [-help] [filename]\n\n\
191 Arguments (may be any combination):\n\
192 -sstring - digests string\n\
193 -t - runs time trial\n\
194 -x - runs test script\n\
195 -h - displays this message\n\
196 -help - displays this message\n\
197 filename - digests file\n\
198 (none) - digests standard input\n\n";