md5.c revision 1.5 1 /* $NetBSD: md5.c,v 1.5 2004/06/20 22:20:15 jmc Exp $ */
2
3 /*
4 * MDDRIVER.C - test driver for MD2, MD4 and MD5
5 */
6
7 /*
8 * Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
9 * rights reserved.
10 *
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.
15 *
16 * These notices must be retained in any copies of any part of this
17 * documentation and/or software.
18 */
19
20 #if HAVE_NBTOOL_CONFIG_H
21 #include "nbtool_config.h"
22 #endif
23
24 #include <sys/cdefs.h>
25 #if defined(__RCSID) && !defined(lint)
26 __RCSID("$NetBSD: md5.c,v 1.5 2004/06/20 22:20:15 jmc Exp $");
27 #endif /* not lint */
28
29 #include <sys/types.h>
30
31 #include <err.h>
32 #include <md5.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <time.h>
36
37 void MD5Filter __P((int));
38 void MD5String __P((const char *));
39 void MD5TestSuite __P((void));
40 void MD5TimeTrial __P((void));
41
42 #ifndef HASHTYPE
43 #define HASHTYPE "MD5"
44 #endif
45
46 #ifndef HASHLEN
47 #define HASHLEN 32
48 #endif
49
50 /*
51 * Length of test block, number of test blocks.
52 */
53 #define TEST_BLOCK_LEN 1000
54 #define TEST_BLOCK_COUNT 1000
55
56 /*
57 * Digests a string and prints the result.
58 */
59 void
60 MD5String(string)
61 const char *string;
62 {
63 unsigned int len = strlen(string);
64 char buf[HASHLEN + 1];
65
66 printf("%s (\"%s\") = %s\n", HASHTYPE, string,
67 MD5Data(string, len, buf));
68 }
69
70 /*
71 * Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks.
72 */
73 void
74 MD5TimeTrial()
75 {
76 MD5_CTX context;
77 time_t endTime, startTime;
78 unsigned char block[TEST_BLOCK_LEN];
79 unsigned int i;
80 char *p, buf[HASHLEN + 1];
81
82 printf("%s time trial. Digesting %d %d-byte blocks ...", HASHTYPE,
83 TEST_BLOCK_LEN, TEST_BLOCK_COUNT);
84 fflush(stdout);
85
86 /* Initialize block */
87 for (i = 0; i < TEST_BLOCK_LEN; i++)
88 block[i] = (unsigned char) (i & 0xff);
89
90 /* Start timer */
91 time(&startTime);
92
93 /* Digest blocks */
94 MD5Init(&context);
95 for (i = 0; i < TEST_BLOCK_COUNT; i++)
96 MD5Update(&context, block, TEST_BLOCK_LEN);
97 p = MD5End(&context,buf);
98
99 /* Stop timer */
100 time(&endTime);
101
102 printf(" done\n");
103 printf("Digest = %s\n", p);
104 printf("Time = %ld seconds\n", (long) (endTime - startTime));
105
106 /*
107 * Be careful that endTime-startTime is not zero.
108 * (Bug fix from Ric * Anderson, ric (at) Artisoft.COM.)
109 */
110 printf("Speed = %ld bytes/second\n",
111 (long) TEST_BLOCK_LEN * (long) TEST_BLOCK_COUNT /
112 ((endTime - startTime) != 0 ? (endTime - startTime) : 1));
113 }
114
115 /*
116 * Digests a reference suite of strings and prints the results.
117 */
118 void
119 MD5TestSuite()
120 {
121 printf("%s test suite:\n", HASHTYPE);
122
123 MD5String("");
124 MD5String("a");
125 MD5String("abc");
126 MD5String("message digest");
127 MD5String("abcdefghijklmnopqrstuvwxyz");
128 MD5String("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");
129 MD5String
130 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
131 MD5String
132 ("1234567890123456789012345678901234567890\
133 1234567890123456789012345678901234567890");
134 }
135
136 /*
137 * Digests the standard input and prints the result.
138 */
139 void
140 MD5Filter(pipe)
141 int pipe;
142 {
143 MD5_CTX context;
144 int len;
145 unsigned char buffer[BUFSIZ];
146 char buf[HASHLEN + 1];
147
148 MD5Init(&context);
149 while ((len = fread(buffer, 1, BUFSIZ, stdin)) > 0) {
150 if (pipe && (len != fwrite(buffer, 1, len, stdout)))
151 err(1, "stdout");
152 MD5Update(&context, buffer, len);
153 }
154 printf("%s\n", MD5End(&context,buf));
155 }
156