4 * MDDRIVER.C - test driver for MD2, MD4 and MD5
8 * Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
11 * RSA Data Security, Inc. makes no representations concerning either
12 * the merchantability of this software or the suitability of this
13 * software for any particular purpose. It is provided "as is"
14 * without express or implied warranty of any kind.
16 * These notices must be retained in any copies of any part of this
17 * documentation and/or software.
21 static const char rcsid[] =
25 #include <sys/types.h>
35 * Length of test block, number of test blocks.
37 #define TEST_BLOCK_LEN 10000
38 #define TEST_BLOCK_COUNT 100000
44 static void MDString(const char *);
45 static void MDTimeTrial(void);
46 static void MDTestSuite(void);
47 static void MDFilter(int);
48 static void usage(void);
52 Arguments (may be any combination):
53 -sstring - digests string
56 filename - digests file
57 (none) - digests standard input
60 main(int argc, char *argv[])
66 while ((ch = getopt(argc, argv, "pqrs:tx")) != -1)
95 p = MD5File(*argv, buf);
102 printf("%s %s\n", p, *argv);
104 printf("MD5 (%s) = %s\n", *argv, p);
106 } else if (!sflag && (optind == 1 || qflag || rflag))
112 * Digests a string and prints the result.
115 MDString(const char *string)
117 size_t len = strlen(string);
121 printf("%s\n", MD5Data(string, len, buf));
123 printf("%s \"%s\"\n", MD5Data(string, len, buf), string);
125 printf("MD5 (\"%s\") = %s\n", string, MD5Data(string, len, buf));
128 * Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks.
134 time_t endTime, startTime;
135 unsigned char block[TEST_BLOCK_LEN];
140 ("MD5 time trial. Digesting %d %d-byte blocks ...",
141 TEST_BLOCK_COUNT, TEST_BLOCK_LEN);
144 /* Initialize block */
145 for (i = 0; i < TEST_BLOCK_LEN; i++)
146 block[i] = (unsigned char) (i & 0xff);
153 for (i = 0; i < TEST_BLOCK_COUNT; i++)
154 MD5Update(&context, block, TEST_BLOCK_LEN);
155 p = MD5End(&context,buf);
161 printf("Digest = %s", p);
162 printf("\nTime = %ld seconds\n", (long) (endTime - startTime));
163 /* Be careful that endTime-startTime is not zero. (Bug fix from Ric
164 * Anderson, ric@Artisoft.COM.) */
166 ("Speed = %ld bytes/second\n",
167 (long) TEST_BLOCK_LEN * (long) TEST_BLOCK_COUNT / ((endTime - startTime) != 0 ? (long)(endTime - startTime) : 1));
170 * Digests a reference suite of strings and prints the results.
176 printf("MD5 test suite:\n");
181 MDString("message digest");
182 MDString("abcdefghijklmnopqrstuvwxyz");
184 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
186 ("1234567890123456789012345678901234567890\
187 1234567890123456789012345678901234567890");
191 * Digests the standard input and prints the result.
198 unsigned char buffer[BUFSIZ];
202 while ((len = fread(buffer, 1, BUFSIZ, stdin))) {
203 if (tee && len != fwrite(buffer, 1, len, stdout))
205 MD5Update(&context, buffer, len);
207 printf("%s\n", MD5End(&context,buf));
214 fprintf(stderr, "usage: md5 [-pqrtx] [-s string] [files ...]\n");