1 /* 2 * $Id: md5.c,v 1.5 1995/05/30 06:09:19 rgrimes 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 char buf[33]; 58 59 if (argc > 1) 60 for (i = 1; i < argc; i++) 61 if (argv[i][0] == '-' && argv[i][1] == 's') 62 MDString(argv[i] + 2); 63 else if (strcmp(argv[i], "-t") == 0) 64 MDTimeTrial(); 65 else if (strcmp(argv[i], "-p") == 0) 66 MDFilter(1); 67 else if (strcmp(argv[i], "-x") == 0) 68 MDTestSuite(); 69 else { 70 p = MD5File(argv[i],buf); 71 if (!p) 72 perror(argv[i]); 73 else 74 printf("MD5 (%s) = %s\n", argv[i], p); 75 } 76 else 77 MDFilter(0); 78 79 return (0); 80 } 81 /* 82 * Digests a string and prints the result. 83 */ 84 static void 85 MDString(string) 86 char *string; 87 { 88 unsigned int len = strlen(string); 89 char buf[33]; 90 91 printf("MD5 (\"%s\") = %s\n", string, MD5Data(string, len, buf)); 92 } 93 /* 94 * Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks. 95 */ 96 static void 97 MDTimeTrial() 98 { 99 MD5_CTX context; 100 time_t endTime, startTime; 101 unsigned char block[TEST_BLOCK_LEN]; 102 unsigned int i; 103 char *p, buf[33]; 104 105 printf 106 ("MD5 time trial. Digesting %d %d-byte blocks ...", 107 TEST_BLOCK_LEN, TEST_BLOCK_COUNT); 108 109 /* Initialize block */ 110 for (i = 0; i < TEST_BLOCK_LEN; i++) 111 block[i] = (unsigned char) (i & 0xff); 112 113 /* Start timer */ 114 time(&startTime); 115 116 /* Digest blocks */ 117 MD5Init(&context); 118 for (i = 0; i < TEST_BLOCK_COUNT; i++) 119 MD5Update(&context, block, TEST_BLOCK_LEN); 120 p = MD5End(&context,buf); 121 122 /* Stop timer */ 123 time(&endTime); 124 125 printf(" done\n"); 126 printf("Digest = %s", p); 127 printf("\nTime = %ld seconds\n", (long) (endTime - startTime)); 128 /* Be careful that endTime-startTime is not zero. (Bug fix from Ric 129 * Anderson, ric@Artisoft.COM.) */ 130 printf 131 ("Speed = %ld bytes/second\n", 132 (long) TEST_BLOCK_LEN * (long) TEST_BLOCK_COUNT / ((endTime - startTime) != 0 ? (endTime - startTime) : 1)); 133 } 134 /* 135 * Digests a reference suite of strings and prints the results. 136 */ 137 static void 138 MDTestSuite() 139 { 140 printf("MD5 test suite:\n"); 141 142 MDString(""); 143 MDString("a"); 144 MDString("abc"); 145 MDString("message digest"); 146 MDString("abcdefghijklmnopqrstuvwxyz"); 147 MDString 148 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); 149 MDString 150 ("1234567890123456789012345678901234567890\ 151 1234567890123456789012345678901234567890"); 152 } 153 154 /* 155 * Digests the standard input and prints the result. 156 */ 157 static void 158 MDFilter(int pipe) 159 { 160 MD5_CTX context; 161 int len; 162 unsigned char buffer[BUFSIZ], digest[16]; 163 char buf[33]; 164 165 MD5Init(&context); 166 while (len = fread(buffer, 1, BUFSIZ, stdin)) { 167 if(pipe && (len != fwrite(buffer, 1, len, stdout))) { 168 perror("stdout"); 169 exit(1); 170 } 171 MD5Update(&context, buffer, len); 172 } 173 printf("%s\n", MD5End(&context,buf)); 174 } 175