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