|
Lines 26-34
Link Here
|
| 26 |
#include <err.h> |
26 |
#include <err.h> |
| 27 |
#include <md5.h> |
27 |
#include <md5.h> |
| 28 |
#include <stdio.h> |
28 |
#include <stdio.h> |
|
|
29 |
#include <stdlib.h> |
| 29 |
#include <time.h> |
30 |
#include <time.h> |
| 30 |
#include <unistd.h> |
31 |
#include <unistd.h> |
| 31 |
#include <string.h> |
32 |
#include <string.h> |
|
|
33 |
#include <md5.h> |
| 34 |
#include <sha.h> |
| 35 |
#include <ripemd.h> |
| 32 |
|
36 |
|
| 33 |
/* |
37 |
/* |
| 34 |
* Length of test block, number of test blocks. |
38 |
* Length of test block, number of test blocks. |
|
Lines 39-70
Link Here
|
| 39 |
int qflag; |
43 |
int qflag; |
| 40 |
int rflag; |
44 |
int rflag; |
| 41 |
|
45 |
|
| 42 |
static void MDString(const char *); |
46 |
extern char *__progname; |
| 43 |
static void MDTimeTrial(void); |
47 |
|
| 44 |
static void MDTestSuite(void); |
48 |
static void MDString __P((const char *)); |
| 45 |
static void MDFilter(int); |
49 |
static void MDTimeTrial __P((void *)); |
|
|
50 |
static void MDTestSuite __P((void)); |
| 51 |
static void MDFilter __P((int, void *)); |
| 46 |
static void usage(void); |
52 |
static void usage(void); |
| 47 |
|
53 |
|
| 48 |
/* Main driver. |
54 |
int main __P((int, char *[])); |
|
|
55 |
|
| 56 |
/* |
| 57 |
* Globals for indirection... |
| 58 |
*/ |
| 59 |
void (*MDInit)(); |
| 60 |
void (*MDUpdate)(); |
| 61 |
char * (*MDEnd)(); |
| 62 |
char * (*MDFile)(); |
| 63 |
char * (*MDData)(); |
| 64 |
char *MDType; |
| 49 |
|
65 |
|
| 50 |
Arguments (may be any combination): |
66 |
/* Main driver. |
| 51 |
-sstring - digests string |
67 |
* |
| 52 |
-t - runs time trial |
68 |
* Arguments (may be any combination): |
| 53 |
-x - runs test script |
69 |
* -sstring - digests string |
| 54 |
filename - digests file |
70 |
* -t - runs time trial |
| 55 |
(none) - digests standard input |
71 |
* -x - runs test script |
|
|
72 |
* filename - digests file |
| 73 |
* (none) - digests standard input |
| 56 |
*/ |
74 |
*/ |
| 57 |
int |
75 |
int |
| 58 |
main(int argc, char *argv[]) |
76 |
main(int argc, char *argv[]) |
| 59 |
{ |
77 |
{ |
| 60 |
int ch; |
78 |
int ch; |
| 61 |
char *p; |
79 |
char *p; |
| 62 |
char buf[33]; |
80 |
char buf[41]; |
|
|
81 |
void *context; |
| 82 |
|
| 83 |
/* What were we called as? Default to md5 */ |
| 84 |
if (strcmp(__progname, "sha1") == 0) { |
| 85 |
MDType = "SHA1"; |
| 86 |
MDInit = SHA1_Init; |
| 87 |
MDUpdate = SHA1_Update; |
| 88 |
MDEnd = SHA1_End; |
| 89 |
MDFile = SHA1_File; |
| 90 |
MDData = SHA1_Data; |
| 91 |
if ((context = malloc(sizeof(SHA1_CTX))) == NULL) |
| 92 |
err(1, "malloc"); |
| 93 |
} else if (strcmp(__progname, "rmd160") == 0) { |
| 94 |
MDType = "RMD160"; |
| 95 |
MDInit = RIPEMD160_Init; |
| 96 |
MDUpdate = RIPEMD160_Update; |
| 97 |
MDEnd = RIPEMD160_End; |
| 98 |
MDFile = RIPEMD160_File; |
| 99 |
MDData = RIPEMD160_Data; |
| 100 |
if ((context = malloc(sizeof(RIPEMD160_CTX))) == NULL) |
| 101 |
err(1, "malloc"); |
| 102 |
} else { |
| 103 |
MDType = "MD5"; |
| 104 |
MDInit = MD5Init; |
| 105 |
MDUpdate = MD5Update; |
| 106 |
MDEnd = MD5End; |
| 107 |
MDFile = MD5File; |
| 108 |
MDData = MD5Data; |
| 109 |
if ((context = malloc(sizeof(MD5_CTX))) == NULL) |
| 110 |
err(1, "malloc"); |
| 111 |
} |
| 112 |
|
| 63 |
|
113 |
|
| 64 |
while ((ch = getopt(argc, argv, "ps:qrtx")) != -1) |
114 |
while ((ch = getopt(argc, argv, "ps:qrtx")) != -1) |
| 65 |
switch (ch) { |
115 |
switch (ch) { |
| 66 |
case 'p': |
116 |
case 'p': |
| 67 |
MDFilter(1); |
117 |
MDFilter(1, context); |
| 68 |
break; |
118 |
break; |
| 69 |
case 'q': |
119 |
case 'q': |
| 70 |
qflag = 1; |
120 |
qflag = 1; |
|
Lines 76-82
Link Here
|
| 76 |
MDString(optarg); |
126 |
MDString(optarg); |
| 77 |
break; |
127 |
break; |
| 78 |
case 't': |
128 |
case 't': |
| 79 |
MDTimeTrial(); |
129 |
MDTimeTrial(context); |
| 80 |
break; |
130 |
break; |
| 81 |
case 'x': |
131 |
case 'x': |
| 82 |
MDTestSuite(); |
132 |
MDTestSuite(); |
|
Lines 89-95
Link Here
|
| 89 |
|
139 |
|
| 90 |
if (*argv) { |
140 |
if (*argv) { |
| 91 |
do { |
141 |
do { |
| 92 |
p = MD5File(*argv, buf); |
142 |
p = MDFile(*argv, buf); |
| 93 |
if (!p) |
143 |
if (!p) |
| 94 |
warn("%s", *argv); |
144 |
warn("%s", *argv); |
| 95 |
else |
145 |
else |
|
Lines 98-107
Link Here
|
| 98 |
else if (rflag) |
148 |
else if (rflag) |
| 99 |
printf("%s %s\n", p, *argv); |
149 |
printf("%s %s\n", p, *argv); |
| 100 |
else |
150 |
else |
| 101 |
printf("MD5 (%s) = %s\n", *argv, p); |
151 |
(void)printf("%s (%s) = %s\n", MDType, |
|
|
152 |
*argv, p); |
| 102 |
} while (*++argv); |
153 |
} while (*++argv); |
| 103 |
} else |
154 |
} else |
| 104 |
MDFilter(0); |
155 |
MDFilter(0, context); |
| 105 |
|
156 |
|
| 106 |
return (0); |
157 |
return (0); |
| 107 |
} |
158 |
} |
|
Lines 109-140
Link Here
|
| 109 |
* Digests a string and prints the result. |
160 |
* Digests a string and prints the result. |
| 110 |
*/ |
161 |
*/ |
| 111 |
static void |
162 |
static void |
| 112 |
MDString(const char *string) |
163 |
MDString(string) |
|
|
164 |
const char *string; |
| 113 |
{ |
165 |
{ |
| 114 |
size_t len = strlen(string); |
166 |
size_t len = strlen(string); |
| 115 |
char buf[33]; |
167 |
char buf[41]; |
| 116 |
|
168 |
|
| 117 |
if (qflag) |
169 |
if (qflag) |
| 118 |
printf("%s\n", MD5Data(string, len, buf)); |
170 |
(void)printf("%s (\"%s\") = %s\n", MDType, string, |
|
|
171 |
MDData(string, len, buf)); |
| 119 |
else if (rflag) |
172 |
else if (rflag) |
| 120 |
printf("%s \"%s\"\n", MD5Data(string, len, buf), string); |
173 |
printf("%s \"%s\"\n", MDData(string, len, buf), string); |
| 121 |
else |
174 |
else |
| 122 |
printf("MD5 (\"%s\") = %s\n", string, MD5Data(string, len, buf)); |
175 |
printf("MD5 (\"%s\") = %s\n", string, MDData(string, len, buf)); |
| 123 |
} |
176 |
} |
| 124 |
/* |
177 |
/* |
| 125 |
* Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks. |
178 |
* Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks. |
| 126 |
*/ |
179 |
*/ |
| 127 |
static void |
180 |
static void |
| 128 |
MDTimeTrial(void) |
181 |
MDTimeTrial(context) |
|
|
182 |
void *context; |
| 129 |
{ |
183 |
{ |
| 130 |
MD5_CTX context; |
|
|
| 131 |
time_t endTime, startTime; |
184 |
time_t endTime, startTime; |
| 132 |
unsigned char block[TEST_BLOCK_LEN]; |
185 |
unsigned char block[TEST_BLOCK_LEN]; |
| 133 |
unsigned int i; |
186 |
unsigned int i; |
| 134 |
char *p, buf[33]; |
187 |
char *p, buf[41]; |
| 135 |
|
188 |
|
| 136 |
printf |
189 |
(void)printf("%s time trial. Digesting %d %d-byte blocks ...", MDType, |
| 137 |
("MD5 time trial. Digesting %d %d-byte blocks ...", |
|
|
| 138 |
TEST_BLOCK_COUNT, TEST_BLOCK_LEN); |
190 |
TEST_BLOCK_COUNT, TEST_BLOCK_LEN); |
| 139 |
fflush(stdout); |
191 |
fflush(stdout); |
| 140 |
|
192 |
|
|
Lines 146-167
Link Here
|
| 146 |
time(&startTime); |
198 |
time(&startTime); |
| 147 |
|
199 |
|
| 148 |
/* Digest blocks */ |
200 |
/* Digest blocks */ |
| 149 |
MD5Init(&context); |
201 |
MDInit(context); |
| 150 |
for (i = 0; i < TEST_BLOCK_COUNT; i++) |
202 |
for (i = 0; i < TEST_BLOCK_COUNT; i++) |
| 151 |
MD5Update(&context, block, TEST_BLOCK_LEN); |
203 |
MDUpdate(context, block, TEST_BLOCK_LEN); |
| 152 |
p = MD5End(&context,buf); |
204 |
p = MDEnd(context,buf); |
| 153 |
|
205 |
|
| 154 |
/* Stop timer */ |
206 |
/* Stop timer */ |
| 155 |
time(&endTime); |
207 |
time(&endTime); |
| 156 |
|
208 |
|
| 157 |
printf(" done\n"); |
209 |
(void)printf(" done\nDigest = %s", p); |
| 158 |
printf("Digest = %s", p); |
210 |
(void)printf("\nTime = %ld seconds\n", (long) (endTime - startTime)); |
| 159 |
printf("\nTime = %ld seconds\n", (long) (endTime - startTime)); |
211 |
/* |
| 160 |
/* Be careful that endTime-startTime is not zero. (Bug fix from Ric |
212 |
* Be careful that endTime-startTime is not zero. (Bug fix from Ric |
| 161 |
* Anderson, ric@Artisoft.COM.) */ |
213 |
* Anderson, ric@Artisoft.COM.) |
| 162 |
printf |
214 |
*/ |
| 163 |
("Speed = %ld bytes/second\n", |
215 |
(void)printf("Speed = %ld bytes/second\n", |
| 164 |
(long) TEST_BLOCK_LEN * (long) TEST_BLOCK_COUNT / ((endTime - startTime) != 0 ? (endTime - startTime) : 1)); |
216 |
(long) TEST_BLOCK_LEN * (long) TEST_BLOCK_COUNT / |
|
|
217 |
((endTime - startTime) != 0 ? (endTime - startTime) : 1)); |
| 165 |
} |
218 |
} |
| 166 |
/* |
219 |
/* |
| 167 |
* Digests a reference suite of strings and prints the results. |
220 |
* Digests a reference suite of strings and prints the results. |
|
Lines 170-176
Link Here
|
| 170 |
MDTestSuite(void) |
223 |
MDTestSuite(void) |
| 171 |
{ |
224 |
{ |
| 172 |
|
225 |
|
| 173 |
printf("MD5 test suite:\n"); |
226 |
(void)printf("%s test suite:\n", MDType); |
| 174 |
|
227 |
|
| 175 |
MDString(""); |
228 |
MDString(""); |
| 176 |
MDString("a"); |
229 |
MDString("a"); |
|
Lines 182-207
Link Here
|
| 182 |
MDString |
235 |
MDString |
| 183 |
("1234567890123456789012345678901234567890\ |
236 |
("1234567890123456789012345678901234567890\ |
| 184 |
1234567890123456789012345678901234567890"); |
237 |
1234567890123456789012345678901234567890"); |
|
|
238 |
MDString("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"); |
| 185 |
} |
239 |
} |
| 186 |
|
240 |
|
| 187 |
/* |
241 |
/* |
| 188 |
* Digests the standard input and prints the result. |
242 |
* Digests the standard input and prints the result. |
| 189 |
*/ |
243 |
*/ |
| 190 |
static void |
244 |
static void |
| 191 |
MDFilter(int tee) |
245 |
MDFilter(tee, context) |
|
|
246 |
int tee; |
| 247 |
void *context; |
| 192 |
{ |
248 |
{ |
| 193 |
MD5_CTX context; |
|
|
| 194 |
unsigned int len; |
249 |
unsigned int len; |
| 195 |
unsigned char buffer[BUFSIZ]; |
250 |
unsigned char buffer[BUFSIZ]; |
| 196 |
char buf[33]; |
251 |
char buf[41]; |
| 197 |
|
252 |
|
| 198 |
MD5Init(&context); |
253 |
MDInit(context); |
| 199 |
while ((len = fread(buffer, 1, BUFSIZ, stdin))) { |
254 |
while ((len = fread(buffer, 1, BUFSIZ, stdin))) { |
| 200 |
if (tee && len != fwrite(buffer, 1, len, stdout)) |
255 |
if (tee && len != fwrite(buffer, 1, len, stdout)) |
| 201 |
err(1, "stdout"); |
256 |
err(1, "stdout"); |
| 202 |
MD5Update(&context, buffer, len); |
257 |
MDUpdate(context, buffer, len); |
| 203 |
} |
258 |
} |
| 204 |
printf("%s\n", MD5End(&context,buf)); |
259 |
(void)printf("%s\n", MDEnd(context,buf)); |
| 205 |
} |
260 |
} |
| 206 |
|
261 |
|
| 207 |
static void |
262 |
static void |