4 * MDDRIVER.C - test driver for MD2, MD4 and MD5
6 * $FreeBSD: src/sbin/md5/md5.c,v 1.20.2.5 2001/12/26 09:44:56 phk Exp $
7 * $DragonFly: src/sbin/md5/md5.c,v 1.2 2003/06/17 04:27:33 dillon Exp $
11 * Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
14 * RSA Data Security, Inc. makes no representations concerning either
15 * the merchantability of this software or the suitability of this
16 * software for any particular purpose. It is provided "as is"
17 * without express or implied warranty of any kind.
19 * These notices must be retained in any copies of any part of this
20 * documentation and/or software.
23 #include <sys/types.h>
33 * Length of test block, number of test blocks.
35 #define TEST_BLOCK_LEN 10000
36 #define TEST_BLOCK_COUNT 100000
42 static void MDString(const char *);
43 static void MDTimeTrial(void);
44 static void MDTestSuite(void);
45 static void MDFilter(int);
46 static void usage(void);
50 Arguments (may be any combination):
51 -sstring - digests string
54 filename - digests file
55 (none) - digests standard input
58 main(int argc, char *argv[])
64 while ((ch = getopt(argc, argv, "pqrs:tx")) != -1)
93 p = MD5File(*argv, buf);
100 printf("%s %s\n", p, *argv);
102 printf("MD5 (%s) = %s\n", *argv, p);
104 } else if (!sflag && (optind == 1 || qflag || rflag))
110 * Digests a string and prints the result.
113 MDString(const char *string)
115 size_t len = strlen(string);
119 printf("%s\n", MD5Data(string, len, buf));
121 printf("%s \"%s\"\n", MD5Data(string, len, buf), string);
123 printf("MD5 (\"%s\") = %s\n", string, MD5Data(string, len, buf));
126 * Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks.
132 time_t endTime, startTime;
133 unsigned char block[TEST_BLOCK_LEN];
138 ("MD5 time trial. Digesting %d %d-byte blocks ...",
139 TEST_BLOCK_COUNT, TEST_BLOCK_LEN);
142 /* Initialize block */
143 for (i = 0; i < TEST_BLOCK_LEN; i++)
144 block[i] = (unsigned char) (i & 0xff);
151 for (i = 0; i < TEST_BLOCK_COUNT; i++)
152 MD5Update(&context, block, TEST_BLOCK_LEN);
153 p = MD5End(&context,buf);
159 printf("Digest = %s", p);
160 printf("\nTime = %ld seconds\n", (long) (endTime - startTime));
161 /* Be careful that endTime-startTime is not zero. (Bug fix from Ric
162 * Anderson, ric@Artisoft.COM.) */
164 ("Speed = %ld bytes/second\n",
165 (long) TEST_BLOCK_LEN * (long) TEST_BLOCK_COUNT / ((endTime - startTime) != 0 ? (long)(endTime - startTime) : 1));
168 * Digests a reference suite of strings and prints the results.
174 printf("MD5 test suite:\n");
179 MDString("message digest");
180 MDString("abcdefghijklmnopqrstuvwxyz");
182 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
184 ("1234567890123456789012345678901234567890\
185 1234567890123456789012345678901234567890");
189 * Digests the standard input and prints the result.
196 unsigned char buffer[BUFSIZ];
200 while ((len = fread(buffer, 1, BUFSIZ, stdin))) {
201 if (tee && len != fwrite(buffer, 1, len, stdout))
203 MD5Update(&context, buffer, len);
205 printf("%s\n", MD5End(&context,buf));
212 fprintf(stderr, "usage: md5 [-pqrtx] [-s string] [files ...]\n");