2 Copyright (C) 2003, 2010 - Wolfire Games
4 This file is part of Lugaru.
6 Lugaru is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 See the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 // DRIVER.CC - test driver for the C++/object oriented translation and
23 // modification of MD5.
25 // Translation and modification (c) 1995 by Mordechai T. Abzug
27 // This translation/ modification is provided "as is," without express or
28 // implied warranty of any kind.
30 // The translator/ modifier does not claim (1) that MD5 will do what you think
31 // it does; (2) that this translation/ modification is accurate; or (3) that
32 // this software is "merchantible." (Language for this disclaimer partially
33 // copied from the disclaimer below).
37 MDDRIVER.C - test driver for MD2, MD4 and MD5
39 Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
42 RSA Data Security, Inc. makes no representations concerning either
43 the merchantability of this software or the suitability of this
44 software for any particular purpose. It is provided "as is"
45 without express or implied warranty of any kind.
47 These notices must be retained in any copies of any part of this
48 documentation and/or software.
59 // Length of test block, number of test blocks.
61 #define TEST_BLOCK_LEN 1000
62 #define TEST_BLOCK_COUNT 1000
64 static void MD5_timeTrial (void);
65 static void MD5_testSuite (void);
66 static void MD5_file (char *);
67 static void MD5_filter (void);
68 static void MD5_string (unsigned char *string);
69 static char *MD5_usage (void);
73 int main (int argc, char *argv[]){
80 for (i = 1; i < argc; i++)
81 if (argv[i][0] == '-' && argv[i][1] == 's')
82 MD5_string ( (unsigned char *) argv[i] + 2);
83 else if (strcmp (argv[i], "-t") == 0)
85 else if (strcmp (argv[i], "-x") == 0)
87 else if (strcmp (argv[i], "-h") == 0)
88 cout << MD5_usage()<< flush;
89 else if (strcmp (argv[i], "-help")==0)
90 cout << MD5_usage()<< flush;
91 else if (argv[i][0] == '-'){
92 cerr << argv[i] << " is an unknown option.\n" << MD5_usage()<< flush;
104 // Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks.
106 static void MD5_timeTrial ()
109 time_t endTime, startTime;
110 unsigned char block[TEST_BLOCK_LEN];
114 cout << "MD5 time trial. Digesting "<< TEST_BLOCK_LEN << " ";
115 cout << TEST_BLOCK_COUNT << "-byte blocks ...";
118 for (i = 0; i < TEST_BLOCK_LEN; i++)
119 block[i] = (unsigned char)(i & 0xff);
125 for (i = 0; i < TEST_BLOCK_COUNT; i++)
126 context.update (block, TEST_BLOCK_LEN);
133 cout << " done" << endl;
135 cout << "Digest = " << context << endl;
137 cout << "Time = "<< (long)(endTime-startTime) << " seconds" << endl;
140 cout << (long)TEST_BLOCK_LEN * (long)TEST_BLOCK_COUNT/(endTime-startTime);
141 cout << "bytes/second" <<endl;
145 // Digests a reference suite of strings and prints the results.
147 static void MD5_testSuite ()
149 cout << "MD5 test suite:" << endl;
151 MD5_string ( (unsigned char*) "");
152 MD5_string ( (unsigned char*) "a");
153 MD5_string ( (unsigned char*) "abc");
154 MD5_string ( (unsigned char*) "message digest");
155 MD5_string ( (unsigned char*) "abcdefghijklmnopqrstuvwxyz");
157 ( (unsigned char*) "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
159 ( (unsigned char*) "1234567890123456789012345678901234567890\
160 1234567890123456789012345678901234567890");
164 // Digests a file and prints the result.
166 static void MD5_file (char *filename){
168 ifstream file(ConvertFileName(filename));
171 cerr << filename <<" can't be opened" << endl;
174 cout << "MD5 (" << filename << ") = " << context << endl;
178 // Digests the standard input and prints the result.
180 static void MD5_filter ()
183 MD5 context(cin); // ie. run istream version of MD5 on cin.
184 // Could also run file version of MD5 on stdin.
186 cout << context << endl;
191 // Digests a string and prints the result.
193 void MD5_string (unsigned char *string){
196 unsigned int len = strlen ( (char *)string);
198 context.update (string, len);
201 cout << "MD5 (\"" << (char *)string << "\") = " << context <<endl;
206 static char *MD5_usage(){
211 MD5 [-sstring] [-t] [-x] [-h] [-help] [filename]\n\n\
212 Arguments (may be any combination):\n\
213 -sstring - digests string\n\
214 -t - runs time trial\n\
215 -x - runs test script\n\
216 -h - displays this message\n\
217 -help - displays this message\n\
218 filename - digests file\n\
219 (none) - digests standard input\n\n";