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