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