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