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