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