Home | History | Annotate | Line # | Download | only in cksum
md5.c revision 1.6
      1  1.6  xtraeme /*	$NetBSD: md5.c,v 1.6 2005/01/12 17:04:35 xtraeme 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.6  xtraeme __RCSID("$NetBSD: md5.c,v 1.6 2005/01/12 17:04:35 xtraeme 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.6  xtraeme void	MD5Filter (int);
     38  1.6  xtraeme void	MD5String (const char *);
     39  1.6  xtraeme void	MD5TestSuite (void);
     40  1.6  xtraeme void	MD5TimeTrial (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.6  xtraeme MD5String(const char *string)
     61  1.1  thorpej {
     62  1.1  thorpej 	unsigned int len = strlen(string);
     63  1.3   atatat 	char buf[HASHLEN + 1];
     64  1.1  thorpej 
     65  1.3   atatat 	printf("%s (\"%s\") = %s\n", HASHTYPE, string,
     66  1.3   atatat 	       MD5Data(string, len, buf));
     67  1.1  thorpej }
     68  1.1  thorpej 
     69  1.1  thorpej /*
     70  1.1  thorpej  * Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks.
     71  1.1  thorpej  */
     72  1.1  thorpej void
     73  1.6  xtraeme MD5TimeTrial(void)
     74  1.1  thorpej {
     75  1.1  thorpej 	MD5_CTX context;
     76  1.1  thorpej 	time_t endTime, startTime;
     77  1.1  thorpej 	unsigned char block[TEST_BLOCK_LEN];
     78  1.1  thorpej 	unsigned int i;
     79  1.3   atatat 	char *p, buf[HASHLEN + 1];
     80  1.1  thorpej 
     81  1.3   atatat 	printf("%s time trial.  Digesting %d %d-byte blocks ...", HASHTYPE,
     82  1.1  thorpej 	    TEST_BLOCK_LEN, TEST_BLOCK_COUNT);
     83  1.1  thorpej 	fflush(stdout);
     84  1.1  thorpej 
     85  1.1  thorpej 	/* Initialize block */
     86  1.1  thorpej 	for (i = 0; i < TEST_BLOCK_LEN; i++)
     87  1.1  thorpej 		block[i] = (unsigned char) (i & 0xff);
     88  1.1  thorpej 
     89  1.1  thorpej 	/* Start timer */
     90  1.1  thorpej 	time(&startTime);
     91  1.1  thorpej 
     92  1.1  thorpej 	/* Digest blocks */
     93  1.1  thorpej 	MD5Init(&context);
     94  1.1  thorpej 	for (i = 0; i < TEST_BLOCK_COUNT; i++)
     95  1.1  thorpej 		MD5Update(&context, block, TEST_BLOCK_LEN);
     96  1.1  thorpej 	p = MD5End(&context,buf);
     97  1.1  thorpej 
     98  1.1  thorpej 	/* Stop timer */
     99  1.1  thorpej 	time(&endTime);
    100  1.1  thorpej 
    101  1.1  thorpej 	printf(" done\n");
    102  1.1  thorpej 	printf("Digest = %s\n", p);
    103  1.1  thorpej 	printf("Time = %ld seconds\n", (long) (endTime - startTime));
    104  1.1  thorpej 
    105  1.1  thorpej 	/*
    106  1.1  thorpej 	 * Be careful that endTime-startTime is not zero.
    107  1.1  thorpej 	 * (Bug fix from Ric * Anderson, ric (at) Artisoft.COM.)
    108  1.1  thorpej 	 */
    109  1.1  thorpej 	printf("Speed = %ld bytes/second\n",
    110  1.1  thorpej 	    (long) TEST_BLOCK_LEN * (long) TEST_BLOCK_COUNT /
    111  1.1  thorpej 	    ((endTime - startTime) != 0 ? (endTime - startTime) : 1));
    112  1.1  thorpej }
    113  1.1  thorpej 
    114  1.1  thorpej /*
    115  1.1  thorpej  * Digests a reference suite of strings and prints the results.
    116  1.1  thorpej  */
    117  1.1  thorpej void
    118  1.6  xtraeme MD5TestSuite(void)
    119  1.1  thorpej {
    120  1.3   atatat 	printf("%s test suite:\n", HASHTYPE);
    121  1.1  thorpej 
    122  1.3   atatat 	MD5String("");
    123  1.3   atatat 	MD5String("a");
    124  1.3   atatat 	MD5String("abc");
    125  1.3   atatat 	MD5String("message digest");
    126  1.3   atatat 	MD5String("abcdefghijklmnopqrstuvwxyz");
    127  1.3   atatat 	MD5String("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");
    128  1.3   atatat 	MD5String
    129  1.1  thorpej 	    ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
    130  1.3   atatat 	MD5String
    131  1.1  thorpej 	    ("1234567890123456789012345678901234567890\
    132  1.1  thorpej 1234567890123456789012345678901234567890");
    133  1.1  thorpej }
    134  1.1  thorpej 
    135  1.1  thorpej /*
    136  1.1  thorpej  * Digests the standard input and prints the result.
    137  1.1  thorpej  */
    138  1.1  thorpej void
    139  1.3   atatat MD5Filter(pipe)
    140  1.1  thorpej 	int pipe;
    141  1.1  thorpej {
    142  1.1  thorpej 	MD5_CTX context;
    143  1.1  thorpej 	int len;
    144  1.2    lukem 	unsigned char buffer[BUFSIZ];
    145  1.3   atatat 	char buf[HASHLEN + 1];
    146  1.1  thorpej 
    147  1.1  thorpej 	MD5Init(&context);
    148  1.2    lukem 	while ((len = fread(buffer, 1, BUFSIZ, stdin)) > 0) {
    149  1.1  thorpej 		if (pipe && (len != fwrite(buffer, 1, len, stdout)))
    150  1.1  thorpej 			err(1, "stdout");
    151  1.1  thorpej 		MD5Update(&context, buffer, len);
    152  1.1  thorpej 	}
    153  1.1  thorpej 	printf("%s\n", MD5End(&context,buf));
    154  1.1  thorpej }
    155