GC !__DragonFly__ section.
[dragonfly.git] / sbin / md5 / md5.c
CommitLineData
984263bc
MD
1/*
2 * Derived from:
3 *
4 * MDDRIVER.C - test driver for MD2, MD4 and MD5
1de703da
MD
5 *
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 $
984263bc
MD
8 */
9
10/*
11 * Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
12 * rights reserved.
13 *
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.
18 *
19 * These notices must be retained in any copies of any part of this
20 * documentation and/or software.
21 */
22
984263bc
MD
23#include <sys/types.h>
24#include <err.h>
25#include <md5.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <time.h>
30#include <unistd.h>
31
32/*
33 * Length of test block, number of test blocks.
34 */
35#define TEST_BLOCK_LEN 10000
36#define TEST_BLOCK_COUNT 100000
37
38int qflag;
39int rflag;
40int sflag;
41
42static void MDString(const char *);
43static void MDTimeTrial(void);
44static void MDTestSuite(void);
45static void MDFilter(int);
46static void usage(void);
47
48/* Main driver.
49
50Arguments (may be any combination):
51 -sstring - digests string
52 -t - runs time trial
53 -x - runs test script
54 filename - digests file
55 (none) - digests standard input
56 */
57int
58main(int argc, char *argv[])
59{
60 int ch;
61 char *p;
62 char buf[33];
63
64 while ((ch = getopt(argc, argv, "pqrs:tx")) != -1)
65 switch (ch) {
66 case 'p':
67 MDFilter(1);
68 break;
69 case 'q':
70 qflag = 1;
71 break;
72 case 'r':
73 rflag = 1;
74 break;
75 case 's':
76 sflag = 1;
77 MDString(optarg);
78 break;
79 case 't':
80 MDTimeTrial();
81 break;
82 case 'x':
83 MDTestSuite();
84 break;
85 default:
86 usage();
87 }
88 argc -= optind;
89 argv += optind;
90
91 if (*argv) {
92 do {
93 p = MD5File(*argv, buf);
94 if (!p)
95 warn("%s", *argv);
96 else
97 if (qflag)
98 printf("%s\n", p);
99 else if (rflag)
100 printf("%s %s\n", p, *argv);
101 else
102 printf("MD5 (%s) = %s\n", *argv, p);
103 } while (*++argv);
104 } else if (!sflag && (optind == 1 || qflag || rflag))
105 MDFilter(0);
106
107 return (0);
108}
109/*
110 * Digests a string and prints the result.
111 */
112static void
113MDString(const char *string)
114{
115 size_t len = strlen(string);
116 char buf[33];
117
118 if (qflag)
119 printf("%s\n", MD5Data(string, len, buf));
120 else if (rflag)
121 printf("%s \"%s\"\n", MD5Data(string, len, buf), string);
122 else
123 printf("MD5 (\"%s\") = %s\n", string, MD5Data(string, len, buf));
124}
125/*
126 * Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks.
127 */
128static void
129MDTimeTrial(void)
130{
131 MD5_CTX context;
132 time_t endTime, startTime;
133 unsigned char block[TEST_BLOCK_LEN];
134 unsigned int i;
135 char *p, buf[33];
136
137 printf
138 ("MD5 time trial. Digesting %d %d-byte blocks ...",
139 TEST_BLOCK_COUNT, TEST_BLOCK_LEN);
140 fflush(stdout);
141
142 /* Initialize block */
143 for (i = 0; i < TEST_BLOCK_LEN; i++)
144 block[i] = (unsigned char) (i & 0xff);
145
146 /* Start timer */
147 time(&startTime);
148
149 /* Digest blocks */
150 MD5Init(&context);
151 for (i = 0; i < TEST_BLOCK_COUNT; i++)
152 MD5Update(&context, block, TEST_BLOCK_LEN);
153 p = MD5End(&context,buf);
154
155 /* Stop timer */
156 time(&endTime);
157
158 printf(" done\n");
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.) */
163 printf
164 ("Speed = %ld bytes/second\n",
165 (long) TEST_BLOCK_LEN * (long) TEST_BLOCK_COUNT / ((endTime - startTime) != 0 ? (long)(endTime - startTime) : 1));
166}
167/*
168 * Digests a reference suite of strings and prints the results.
169 */
170static void
171MDTestSuite(void)
172{
173
174 printf("MD5 test suite:\n");
175
176 MDString("");
177 MDString("a");
178 MDString("abc");
179 MDString("message digest");
180 MDString("abcdefghijklmnopqrstuvwxyz");
181 MDString
182 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
183 MDString
184 ("1234567890123456789012345678901234567890\
1851234567890123456789012345678901234567890");
186}
187
188/*
189 * Digests the standard input and prints the result.
190 */
191static void
192MDFilter(int tee)
193{
194 MD5_CTX context;
195 unsigned int len;
196 unsigned char buffer[BUFSIZ];
197 char buf[33];
198
199 MD5Init(&context);
200 while ((len = fread(buffer, 1, BUFSIZ, stdin))) {
201 if (tee && len != fwrite(buffer, 1, len, stdout))
202 err(1, "stdout");
203 MD5Update(&context, buffer, len);
204 }
205 printf("%s\n", MD5End(&context,buf));
206}
207
208static void
209usage(void)
210{
211
212 fprintf(stderr, "usage: md5 [-pqrtx] [-s string] [files ...]\n");
213 exit(1);
214}