]> git.jsancho.org Git - lugaru.git/blob - Source/DRIVER.CC
fba567be08ce534349fbe15f2869888e0c3d3ed0
[lugaru.git] / Source / DRIVER.CC
1 // DRIVER.CC - test driver for the C++/object oriented translation and 
2 //             modification of MD5.
3
4 // Translation and modification (c) 1995 by Mordechai T. Abzug 
5
6 // This translation/ modification is provided "as is," without express or 
7 // implied warranty of any kind.
8
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).
13
14 /* based on:
15
16    MDDRIVER.C - test driver for MD2, MD4 and MD5
17
18   Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
19 rights reserved.
20
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.
25
26 These notices must be retained in any copies of any part of this
27 documentation and/or software.
28  */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <time.h>
33 #include <string.h>
34
35 #include "md5.h"
36
37
38 // Length of test block, number of test blocks.
39
40 #define TEST_BLOCK_LEN 1000
41 #define TEST_BLOCK_COUNT 1000
42
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);
49
50 // Main driver.
51 /*
52 int main (int argc, char *argv[]){
53
54   int i;
55
56   MD5_testSuite();
57
58   if (argc > 1)
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)
63         MD5_timeTrial ();
64       else if (strcmp (argv[i], "-x") == 0)
65         MD5_testSuite ();
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;
72         exit (1);
73       }
74       else
75         MD5_file (argv[i]);
76   else
77     MD5_filter ();
78
79   return (0);
80 }
81 */
82
83 // Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks.
84
85 static void MD5_timeTrial ()
86 {
87   MD5 context;
88   time_t endTime, startTime;
89   unsigned char block[TEST_BLOCK_LEN];
90   unsigned int i;
91
92
93   cout << "MD5 time trial. Digesting "<< TEST_BLOCK_LEN << " ";
94   cout << TEST_BLOCK_COUNT << "-byte blocks ...";
95
96   // Initialize block
97   for (i = 0; i < TEST_BLOCK_LEN; i++)
98     block[i] = (unsigned char)(i & 0xff);
99
100   // Start timer
101   time (&startTime);
102
103   // Digest blocks
104   for (i = 0; i < TEST_BLOCK_COUNT; i++)
105     context.update (block, TEST_BLOCK_LEN);
106
107   context.finalize();
108
109   // Stop timer
110   time (&endTime);
111
112   cout << " done" << endl;
113
114   cout << "Digest = " << context << endl;
115
116   cout << "Time = "<< (long)(endTime-startTime) << " seconds" << endl;
117
118   cout << "Speed = ";
119   cout << (long)TEST_BLOCK_LEN * (long)TEST_BLOCK_COUNT/(endTime-startTime);
120   cout << "bytes/second" <<endl;
121
122 }
123
124 // Digests a reference suite of strings and prints the results.
125 /*
126 static void MD5_testSuite ()
127 {
128   cout << "MD5 test suite:" << endl;
129
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");
135   MD5_string
136  ( (unsigned char*) "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
137   MD5_string
138  ( (unsigned char*) "1234567890123456789012345678901234567890\
139 1234567890123456789012345678901234567890");
140 }
141 */
142
143 // Digests a file and prints the result.
144
145 static void MD5_file (char *filename){
146
147   ifstream file(ConvertFileName(filename));
148
149   if (!file)
150     cerr << filename <<" can't be opened" << endl;
151   else {
152     MD5 context(file);
153     cout <<  "MD5 ("  << filename <<  ") = "  <<  context << endl;
154   }
155 }
156
157 // Digests the standard input and prints the result.
158
159 static void MD5_filter ()
160 {
161
162   MD5 context(cin);  // ie. run istream version of MD5 on cin.
163                      // Could also run file version of MD5 on stdin.
164
165   cout << context << endl;
166 }
167
168
169
170 // Digests a string and prints the result.
171 /*
172 void MD5_string (unsigned char *string){
173
174   MD5 context;
175   unsigned int len = strlen ( (char *)string);
176
177   context.update   (string, len);
178   context.finalize ();
179
180   cout << "MD5 (\"" << (char *)string << "\") = " << context <<endl;
181 }*/
182
183
184
185 static char *MD5_usage(){
186
187   return "\n\
188   MD5\n\n\
189 USAGE:\n\n\
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";
199 }
200
201
202
203