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