md5.c revision 6725
1/*
2 * $Id: md5.c,v 1.2 1995/02/20 00:48:50 phk Exp $
3 *
4 * Derived from:
5 */
6
7/*
8 * MDDRIVER.C - test driver for MD2, MD4 and MD5
9 */
10
11/*
12 *  Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
13 *  rights reserved.
14 *
15 *  RSA Data Security, Inc. makes no representations concerning either
16 *  the merchantability of this software or the suitability of this
17 *  software for any particular purpose. It is provided "as is"
18 *  without express or implied warranty of any kind.
19 *
20 *  These notices must be retained in any copies of any part of this
21 *  documentation and/or software.
22 */
23
24#include <stdio.h>
25#include <time.h>
26#include <string.h>
27#include "global.h"
28#include <md5.h>
29
30/*
31 * Length of test block, number of test blocks.
32 */
33#define TEST_BLOCK_LEN 1000
34#define TEST_BLOCK_COUNT 1000
35
36static void MDString PROTO_LIST((char *));
37static void MDTimeTrial PROTO_LIST((void));
38static void MDTestSuite PROTO_LIST((void));
39static void MDFilter PROTO_LIST((int));
40
41/* Main driver.
42
43Arguments (may be any combination):
44  -sstring - digests string
45  -t       - runs time trial
46  -x       - runs test script
47  filename - digests file
48  (none)   - digests standard input
49 */
50int
51main(argc, argv)
52	int     argc;
53	char   *argv[];
54{
55	int     i;
56	char   *p;
57
58	if (argc > 1)
59		for (i = 1; i < argc; i++)
60			if (argv[i][0] == '-' && argv[i][1] == 's')
61				MDString(argv[i] + 2);
62			else if (strcmp(argv[i], "-t") == 0)
63				MDTimeTrial();
64			else if (strcmp(argv[i], "-p") == 0)
65				MDFilter(1);
66			else if (strcmp(argv[i], "-x") == 0)
67				MDTestSuite();
68			else {
69				p = MD5File(argv[i]);
70				if (!p)
71					perror(argv[i]);
72				else
73					printf("MD5 (%s) = %s\n", argv[i], p);
74			}
75	else
76		MDFilter(0);
77
78	return (0);
79}
80/*
81 * Digests a string and prints the result.
82 */
83static void
84MDString(string)
85	char   *string;
86{
87	unsigned int len = strlen(string);
88
89	printf("MD5 (\"%s\") = %s\n", string, MD5Data(string, len));
90}
91/*
92 * Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks.
93 */
94static void
95MDTimeTrial()
96{
97	MD5_CTX context;
98	time_t  endTime, startTime;
99	unsigned char block[TEST_BLOCK_LEN];
100	unsigned int i;
101	char   *p;
102
103	printf
104	    ("MD5 time trial. Digesting %d %d-byte blocks ...",
105	    TEST_BLOCK_LEN, TEST_BLOCK_COUNT);
106
107	/* Initialize block */
108	for (i = 0; i < TEST_BLOCK_LEN; i++)
109		block[i] = (unsigned char) (i & 0xff);
110
111	/* Start timer */
112	time(&startTime);
113
114	/* Digest blocks */
115	MD5Init(&context);
116	for (i = 0; i < TEST_BLOCK_COUNT; i++)
117		MD5Update(&context, block, TEST_BLOCK_LEN);
118	p = MD5End(&context);
119
120	/* Stop timer */
121	time(&endTime);
122
123	printf(" done\n");
124	printf("Digest = %s", p);
125	printf("\nTime = %ld seconds\n", (long) (endTime - startTime));
126	/* Be careful that endTime-startTime is not zero. (Bug fix from Ric
127	 * Anderson, ric@Artisoft.COM.) */
128	printf
129	    ("Speed = %ld bytes/second\n",
130	    (long) TEST_BLOCK_LEN * (long) TEST_BLOCK_COUNT / ((endTime - startTime) != 0 ? (endTime - startTime) : 1));
131}
132/*
133 * Digests a reference suite of strings and prints the results.
134 */
135static void
136MDTestSuite()
137{
138	printf("MD5 test suite:\n");
139
140	MDString("");
141	MDString("a");
142	MDString("abc");
143	MDString("message digest");
144	MDString("abcdefghijklmnopqrstuvwxyz");
145	MDString
146	    ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
147	MDString
148	    ("1234567890123456789012345678901234567890\
1491234567890123456789012345678901234567890");
150}
151
152/*
153 * Digests the standard input and prints the result.
154 */
155static void
156MDFilter(int pipe)
157{
158	MD5_CTX context;
159	int     len;
160	unsigned char buffer[BUFSIZ], digest[16];
161
162	MD5Init(&context);
163	while (len = fread(buffer, 1, BUFSIZ, stdin)) {
164		if(pipe && (len != fwrite(buffer, 1, len, stdout)))
165			perror(stdout);
166		MD5Update(&context, buffer, len);
167	}
168	printf("%s\n", MD5End(&context));
169}
170