md5.c revision 19168
16562Sphk/* 219168Sbde * $Id: md5.c,v 1.6 1995/07/12 09:14:46 phk Exp $ 36562Sphk * 46562Sphk * Derived from: 53995Spst */ 63995Spst 76562Sphk/* 86562Sphk * MDDRIVER.C - test driver for MD2, MD4 and MD5 93995Spst */ 103995Spst 116562Sphk/* 126562Sphk * Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All 136562Sphk * rights reserved. 146562Sphk * 156562Sphk * RSA Data Security, Inc. makes no representations concerning either 166562Sphk * the merchantability of this software or the suitability of this 176562Sphk * software for any particular purpose. It is provided "as is" 186562Sphk * without express or implied warranty of any kind. 196562Sphk * 206562Sphk * These notices must be retained in any copies of any part of this 216562Sphk * documentation and/or software. 223995Spst */ 233995Spst 2419168Sbde#include <sys/types.h> 2519168Sbde#include <md5.h> 2619168Sbde 273995Spst#include <stdio.h> 2819168Sbde#include <string.h> 293995Spst#include <time.h> 3019168Sbde 313995Spst#include "global.h" 323995Spst 336562Sphk/* 346562Sphk * Length of test block, number of test blocks. 353995Spst */ 363995Spst#define TEST_BLOCK_LEN 1000 373995Spst#define TEST_BLOCK_COUNT 1000 383995Spst 396562Sphkstatic void MDString PROTO_LIST((char *)); 406562Sphkstatic void MDTimeTrial PROTO_LIST((void)); 416562Sphkstatic void MDTestSuite PROTO_LIST((void)); 426725Sphkstatic void MDFilter PROTO_LIST((int)); 433995Spst 443995Spst/* Main driver. 453995Spst 463995SpstArguments (may be any combination): 473995Spst -sstring - digests string 483995Spst -t - runs time trial 493995Spst -x - runs test script 503995Spst filename - digests file 513995Spst (none) - digests standard input 523995Spst */ 536562Sphkint 546562Sphkmain(argc, argv) 556562Sphk int argc; 566562Sphk char *argv[]; 573995Spst{ 586562Sphk int i; 596562Sphk char *p; 609489Sphk char buf[33]; 613995Spst 626562Sphk if (argc > 1) 636562Sphk for (i = 1; i < argc; i++) 646562Sphk if (argv[i][0] == '-' && argv[i][1] == 's') 656562Sphk MDString(argv[i] + 2); 666562Sphk else if (strcmp(argv[i], "-t") == 0) 676562Sphk MDTimeTrial(); 686725Sphk else if (strcmp(argv[i], "-p") == 0) 696725Sphk MDFilter(1); 706562Sphk else if (strcmp(argv[i], "-x") == 0) 716562Sphk MDTestSuite(); 726562Sphk else { 739489Sphk p = MD5File(argv[i],buf); 748871Srgrimes if (!p) 756562Sphk perror(argv[i]); 766562Sphk else 776562Sphk printf("MD5 (%s) = %s\n", argv[i], p); 786562Sphk } 796562Sphk else 806725Sphk MDFilter(0); 813995Spst 826562Sphk return (0); 833995Spst} 846562Sphk/* 856562Sphk * Digests a string and prints the result. 863995Spst */ 876562Sphkstatic void 886562SphkMDString(string) 896562Sphk char *string; 903995Spst{ 916562Sphk unsigned int len = strlen(string); 929489Sphk char buf[33]; 933995Spst 949489Sphk printf("MD5 (\"%s\") = %s\n", string, MD5Data(string, len, buf)); 953995Spst} 966562Sphk/* 976562Sphk * Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks. 983995Spst */ 996562Sphkstatic void 1006562SphkMDTimeTrial() 1013995Spst{ 1026562Sphk MD5_CTX context; 1036562Sphk time_t endTime, startTime; 1046725Sphk unsigned char block[TEST_BLOCK_LEN]; 1056562Sphk unsigned int i; 1069489Sphk char *p, buf[33]; 1073995Spst 1086562Sphk printf 1096562Sphk ("MD5 time trial. Digesting %d %d-byte blocks ...", 1106562Sphk TEST_BLOCK_LEN, TEST_BLOCK_COUNT); 1113995Spst 1126562Sphk /* Initialize block */ 1136562Sphk for (i = 0; i < TEST_BLOCK_LEN; i++) 1146562Sphk block[i] = (unsigned char) (i & 0xff); 1153995Spst 1166562Sphk /* Start timer */ 1176562Sphk time(&startTime); 1183995Spst 1196562Sphk /* Digest blocks */ 1206562Sphk MD5Init(&context); 1216562Sphk for (i = 0; i < TEST_BLOCK_COUNT; i++) 1226562Sphk MD5Update(&context, block, TEST_BLOCK_LEN); 1239489Sphk p = MD5End(&context,buf); 1243995Spst 1256562Sphk /* Stop timer */ 1266562Sphk time(&endTime); 1273995Spst 1286562Sphk printf(" done\n"); 1296562Sphk printf("Digest = %s", p); 1306562Sphk printf("\nTime = %ld seconds\n", (long) (endTime - startTime)); 1316562Sphk /* Be careful that endTime-startTime is not zero. (Bug fix from Ric 1326562Sphk * Anderson, ric@Artisoft.COM.) */ 1336562Sphk printf 1346562Sphk ("Speed = %ld bytes/second\n", 1356562Sphk (long) TEST_BLOCK_LEN * (long) TEST_BLOCK_COUNT / ((endTime - startTime) != 0 ? (endTime - startTime) : 1)); 1363995Spst} 1376562Sphk/* 1386562Sphk * Digests a reference suite of strings and prints the results. 1393995Spst */ 1406562Sphkstatic void 1416562SphkMDTestSuite() 1423995Spst{ 1436562Sphk printf("MD5 test suite:\n"); 1443995Spst 1456562Sphk MDString(""); 1466562Sphk MDString("a"); 1476562Sphk MDString("abc"); 1486562Sphk MDString("message digest"); 1496562Sphk MDString("abcdefghijklmnopqrstuvwxyz"); 1506562Sphk MDString 1516562Sphk ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); 1526562Sphk MDString 1536562Sphk ("1234567890123456789012345678901234567890\ 1543995Spst1234567890123456789012345678901234567890"); 1553995Spst} 1563995Spst 1576562Sphk/* 1586562Sphk * Digests the standard input and prints the result. 1593995Spst */ 1606562Sphkstatic void 1616725SphkMDFilter(int pipe) 1623995Spst{ 1636562Sphk MD5_CTX context; 1646562Sphk int len; 1656562Sphk unsigned char buffer[BUFSIZ], digest[16]; 1669489Sphk char buf[33]; 1673995Spst 1686562Sphk MD5Init(&context); 1696725Sphk while (len = fread(buffer, 1, BUFSIZ, stdin)) { 1706726Sphk if(pipe && (len != fwrite(buffer, 1, len, stdout))) { 1716726Sphk perror("stdout"); 1726726Sphk exit(1); 1736726Sphk } 1746562Sphk MD5Update(&context, buffer, len); 1756725Sphk } 1769489Sphk printf("%s\n", MD5End(&context,buf)); 1773995Spst} 178