md5.c revision 9489
16562Sphk/* 29489Sphk * $Id: md5.c,v 1.5 1995/05/30 06:09:19 rgrimes 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 243995Spst#include <stdio.h> 253995Spst#include <time.h> 263995Spst#include <string.h> 273995Spst#include "global.h" 283995Spst#include <md5.h> 293995Spst 306562Sphk/* 316562Sphk * Length of test block, number of test blocks. 323995Spst */ 333995Spst#define TEST_BLOCK_LEN 1000 343995Spst#define TEST_BLOCK_COUNT 1000 353995Spst 366562Sphkstatic void MDString PROTO_LIST((char *)); 376562Sphkstatic void MDTimeTrial PROTO_LIST((void)); 386562Sphkstatic void MDTestSuite PROTO_LIST((void)); 396725Sphkstatic void MDFilter PROTO_LIST((int)); 403995Spst 413995Spst/* Main driver. 423995Spst 433995SpstArguments (may be any combination): 443995Spst -sstring - digests string 453995Spst -t - runs time trial 463995Spst -x - runs test script 473995Spst filename - digests file 483995Spst (none) - digests standard input 493995Spst */ 506562Sphkint 516562Sphkmain(argc, argv) 526562Sphk int argc; 536562Sphk char *argv[]; 543995Spst{ 556562Sphk int i; 566562Sphk char *p; 579489Sphk char buf[33]; 583995Spst 596562Sphk if (argc > 1) 606562Sphk for (i = 1; i < argc; i++) 616562Sphk if (argv[i][0] == '-' && argv[i][1] == 's') 626562Sphk MDString(argv[i] + 2); 636562Sphk else if (strcmp(argv[i], "-t") == 0) 646562Sphk MDTimeTrial(); 656725Sphk else if (strcmp(argv[i], "-p") == 0) 666725Sphk MDFilter(1); 676562Sphk else if (strcmp(argv[i], "-x") == 0) 686562Sphk MDTestSuite(); 696562Sphk else { 709489Sphk p = MD5File(argv[i],buf); 718871Srgrimes if (!p) 726562Sphk perror(argv[i]); 736562Sphk else 746562Sphk printf("MD5 (%s) = %s\n", argv[i], p); 756562Sphk } 766562Sphk else 776725Sphk MDFilter(0); 783995Spst 796562Sphk return (0); 803995Spst} 816562Sphk/* 826562Sphk * Digests a string and prints the result. 833995Spst */ 846562Sphkstatic void 856562SphkMDString(string) 866562Sphk char *string; 873995Spst{ 886562Sphk unsigned int len = strlen(string); 899489Sphk char buf[33]; 903995Spst 919489Sphk printf("MD5 (\"%s\") = %s\n", string, MD5Data(string, len, buf)); 923995Spst} 936562Sphk/* 946562Sphk * Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks. 953995Spst */ 966562Sphkstatic void 976562SphkMDTimeTrial() 983995Spst{ 996562Sphk MD5_CTX context; 1006562Sphk time_t endTime, startTime; 1016725Sphk unsigned char block[TEST_BLOCK_LEN]; 1026562Sphk unsigned int i; 1039489Sphk char *p, buf[33]; 1043995Spst 1056562Sphk printf 1066562Sphk ("MD5 time trial. Digesting %d %d-byte blocks ...", 1076562Sphk TEST_BLOCK_LEN, TEST_BLOCK_COUNT); 1083995Spst 1096562Sphk /* Initialize block */ 1106562Sphk for (i = 0; i < TEST_BLOCK_LEN; i++) 1116562Sphk block[i] = (unsigned char) (i & 0xff); 1123995Spst 1136562Sphk /* Start timer */ 1146562Sphk time(&startTime); 1153995Spst 1166562Sphk /* Digest blocks */ 1176562Sphk MD5Init(&context); 1186562Sphk for (i = 0; i < TEST_BLOCK_COUNT; i++) 1196562Sphk MD5Update(&context, block, TEST_BLOCK_LEN); 1209489Sphk p = MD5End(&context,buf); 1213995Spst 1226562Sphk /* Stop timer */ 1236562Sphk time(&endTime); 1243995Spst 1256562Sphk printf(" done\n"); 1266562Sphk printf("Digest = %s", p); 1276562Sphk printf("\nTime = %ld seconds\n", (long) (endTime - startTime)); 1286562Sphk /* Be careful that endTime-startTime is not zero. (Bug fix from Ric 1296562Sphk * Anderson, ric@Artisoft.COM.) */ 1306562Sphk printf 1316562Sphk ("Speed = %ld bytes/second\n", 1326562Sphk (long) TEST_BLOCK_LEN * (long) TEST_BLOCK_COUNT / ((endTime - startTime) != 0 ? (endTime - startTime) : 1)); 1333995Spst} 1346562Sphk/* 1356562Sphk * Digests a reference suite of strings and prints the results. 1363995Spst */ 1376562Sphkstatic void 1386562SphkMDTestSuite() 1393995Spst{ 1406562Sphk printf("MD5 test suite:\n"); 1413995Spst 1426562Sphk MDString(""); 1436562Sphk MDString("a"); 1446562Sphk MDString("abc"); 1456562Sphk MDString("message digest"); 1466562Sphk MDString("abcdefghijklmnopqrstuvwxyz"); 1476562Sphk MDString 1486562Sphk ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); 1496562Sphk MDString 1506562Sphk ("1234567890123456789012345678901234567890\ 1513995Spst1234567890123456789012345678901234567890"); 1523995Spst} 1533995Spst 1546562Sphk/* 1556562Sphk * Digests the standard input and prints the result. 1563995Spst */ 1576562Sphkstatic void 1586725SphkMDFilter(int pipe) 1593995Spst{ 1606562Sphk MD5_CTX context; 1616562Sphk int len; 1626562Sphk unsigned char buffer[BUFSIZ], digest[16]; 1639489Sphk char buf[33]; 1643995Spst 1656562Sphk MD5Init(&context); 1666725Sphk while (len = fread(buffer, 1, BUFSIZ, stdin)) { 1676726Sphk if(pipe && (len != fwrite(buffer, 1, len, stdout))) { 1686726Sphk perror("stdout"); 1696726Sphk exit(1); 1706726Sphk } 1716562Sphk MD5Update(&context, buffer, len); 1726725Sphk } 1739489Sphk printf("%s\n", MD5End(&context,buf)); 1743995Spst} 175