1 /* 2 * $Id: md5.c,v 1.4 1995/02/26 02:00:35 phk Exp $ 3 * 4 * Derived from: 5 */ 6 7 /* 8 * MDDRIVER.C - test driver for MD2, MD4 and MD5 9 */ 10 11 /* 12 * Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All 13 * rights reserved. 14 * 15 * RSA Data Security, Inc. makes no representations concerning either 16 * the merchantability of this software or the suitability of this 17 * software for any particular purpose. It is provided "as is" 18 * without express or implied warranty of any kind. 19 * 20 * These notices must be retained in any copies of any part of this 21 * documentation and/or software. 22 */ 23 24 #include <stdio.h> 25 #include <time.h> 26 #include <string.h> 27 #include "global.h" 28 #include <md5.h> 29 30 /* 31 * Length of test block, number of test blocks. 32 */ 33 #define TEST_BLOCK_LEN 1000 34 #define TEST_BLOCK_COUNT 1000 35 36 static void MDString PROTO_LIST((char *)); 37 static void MDTimeTrial PROTO_LIST((void)); 38 static void MDTestSuite PROTO_LIST((void)); 39 static void MDFilter PROTO_LIST((int)); 40 41 /* Main driver. 42 43 Arguments (may be any combination): 44 -sstring - digests string 45 -t - runs time trial 46 -x - runs test script 47 filename - digests file 48 (none) - digests standard input 49 */ 50 int 51 main(argc, argv) 52 int argc; 53 char *argv[]; 54 { 55 int i; 56 char *p; 57 58 if (argc > 1) 59 for (i = 1; i < argc; i++) 60 if (argv[i][0] == '-' && argv[i][1] == 's') 61 MDString(argv[i] + 2); 62 else if (strcmp(argv[i], "-t") == 0) 63 MDTimeTrial(); 64 else if (strcmp(argv[i], "-p") == 0) 65 MDFilter(1); 66 else if (strcmp(argv[i], "-x") == 0) 67 MDTestSuite(); 68 else { 69 p = MD5File(argv[i]); 70 if (!p) 71 perror(argv[i]); 72 else 73 printf("MD5 (%s) = %s\n", argv[i], p); 74 } 75 else 76 MDFilter(0); 77 78 return (0); 79 } 80 /* 81 * Digests a string and prints the result. 82 */ 83 static void 84 MDString(string) 85 char *string; 86 { 87 unsigned int len = strlen(string); 88 89 printf("MD5 (\"%s\") = %s\n", string, MD5Data(string, len)); 90 } 91 /* 92 * Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks. 93 */ 94 static void 95 MDTimeTrial() 96 { 97 MD5_CTX context; 98 time_t endTime, startTime; 99 unsigned char block[TEST_BLOCK_LEN]; 100 unsigned int i; 101 char *p; 102 103 printf 104 ("MD5 time trial. Digesting %d %d-byte blocks ...", 105 TEST_BLOCK_LEN, TEST_BLOCK_COUNT); 106 107 /* Initialize block */ 108 for (i = 0; i < TEST_BLOCK_LEN; i++) 109 block[i] = (unsigned char) (i & 0xff); 110 111 /* Start timer */ 112 time(&startTime); 113 114 /* Digest blocks */ 115 MD5Init(&context); 116 for (i = 0; i < TEST_BLOCK_COUNT; i++) 117 MD5Update(&context, block, TEST_BLOCK_LEN); 118 p = MD5End(&context); 119 120 /* Stop timer */ 121 time(&endTime); 122 123 printf(" done\n"); 124 printf("Digest = %s", p); 125 printf("\nTime = %ld seconds\n", (long) (endTime - startTime)); 126 /* Be careful that endTime-startTime is not zero. (Bug fix from Ric 127 * Anderson, ric@Artisoft.COM.) */ 128 printf 129 ("Speed = %ld bytes/second\n", 130 (long) TEST_BLOCK_LEN * (long) TEST_BLOCK_COUNT / ((endTime - startTime) != 0 ? (endTime - startTime) : 1)); 131 } 132 /* 133 * Digests a reference suite of strings and prints the results. 134 */ 135 static void 136 MDTestSuite() 137 { 138 printf("MD5 test suite:\n"); 139 140 MDString(""); 141 MDString("a"); 142 MDString("abc"); 143 MDString("message digest"); 144 MDString("abcdefghijklmnopqrstuvwxyz"); 145 MDString 146 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); 147 MDString 148 ("1234567890123456789012345678901234567890\ 149 1234567890123456789012345678901234567890"); 150 } 151 152 /* 153 * Digests the standard input and prints the result. 154 */ 155 static void 156 MDFilter(int pipe) 157 { 158 MD5_CTX context; 159 int len; 160 unsigned char buffer[BUFSIZ], digest[16]; 161 162 MD5Init(&context); 163 while (len = fread(buffer, 1, BUFSIZ, stdin)) { 164 if(pipe && (len != fwrite(buffer, 1, len, stdout))) { 165 perror("stdout"); 166 exit(1); 167 } 168 MD5Update(&context, buffer, len); 169 } 170 printf("%s\n", MD5End(&context)); 171 } 172