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