md5.c revision 8871
16562Sphk/*
28871Srgrimes * $Id: md5.c,v 1.4 1995/02/26 02:00:35 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
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;
573995Spst
586562Sphk	if (argc > 1)
596562Sphk		for (i = 1; i < argc; i++)
606562Sphk			if (argv[i][0] == '-' && argv[i][1] == 's')
616562Sphk				MDString(argv[i] + 2);
626562Sphk			else if (strcmp(argv[i], "-t") == 0)
636562Sphk				MDTimeTrial();
646725Sphk			else if (strcmp(argv[i], "-p") == 0)
656725Sphk				MDFilter(1);
666562Sphk			else if (strcmp(argv[i], "-x") == 0)
676562Sphk				MDTestSuite();
686562Sphk			else {
696562Sphk				p = MD5File(argv[i]);
708871Srgrimes				if (!p)
716562Sphk					perror(argv[i]);
726562Sphk				else
736562Sphk					printf("MD5 (%s) = %s\n", argv[i], p);
746562Sphk			}
756562Sphk	else
766725Sphk		MDFilter(0);
773995Spst
786562Sphk	return (0);
793995Spst}
806562Sphk/*
816562Sphk * Digests a string and prints the result.
823995Spst */
836562Sphkstatic void
846562SphkMDString(string)
856562Sphk	char   *string;
863995Spst{
876562Sphk	unsigned int len = strlen(string);
883995Spst
896562Sphk	printf("MD5 (\"%s\") = %s\n", string, MD5Data(string, len));
903995Spst}
916562Sphk/*
926562Sphk * Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks.
933995Spst */
946562Sphkstatic void
956562SphkMDTimeTrial()
963995Spst{
976562Sphk	MD5_CTX context;
986562Sphk	time_t  endTime, startTime;
996725Sphk	unsigned char block[TEST_BLOCK_LEN];
1006562Sphk	unsigned int i;
1016562Sphk	char   *p;
1023995Spst
1036562Sphk	printf
1046562Sphk	    ("MD5 time trial. Digesting %d %d-byte blocks ...",
1056562Sphk	    TEST_BLOCK_LEN, TEST_BLOCK_COUNT);
1063995Spst
1076562Sphk	/* Initialize block */
1086562Sphk	for (i = 0; i < TEST_BLOCK_LEN; i++)
1096562Sphk		block[i] = (unsigned char) (i & 0xff);
1103995Spst
1116562Sphk	/* Start timer */
1126562Sphk	time(&startTime);
1133995Spst
1146562Sphk	/* Digest blocks */
1156562Sphk	MD5Init(&context);
1166562Sphk	for (i = 0; i < TEST_BLOCK_COUNT; i++)
1176562Sphk		MD5Update(&context, block, TEST_BLOCK_LEN);
1186562Sphk	p = MD5End(&context);
1193995Spst
1206562Sphk	/* Stop timer */
1216562Sphk	time(&endTime);
1223995Spst
1236562Sphk	printf(" done\n");
1246562Sphk	printf("Digest = %s", p);
1256562Sphk	printf("\nTime = %ld seconds\n", (long) (endTime - startTime));
1266562Sphk	/* Be careful that endTime-startTime is not zero. (Bug fix from Ric
1276562Sphk	 * Anderson, ric@Artisoft.COM.) */
1286562Sphk	printf
1296562Sphk	    ("Speed = %ld bytes/second\n",
1306562Sphk	    (long) TEST_BLOCK_LEN * (long) TEST_BLOCK_COUNT / ((endTime - startTime) != 0 ? (endTime - startTime) : 1));
1313995Spst}
1326562Sphk/*
1336562Sphk * Digests a reference suite of strings and prints the results.
1343995Spst */
1356562Sphkstatic void
1366562SphkMDTestSuite()
1373995Spst{
1386562Sphk	printf("MD5 test suite:\n");
1393995Spst
1406562Sphk	MDString("");
1416562Sphk	MDString("a");
1426562Sphk	MDString("abc");
1436562Sphk	MDString("message digest");
1446562Sphk	MDString("abcdefghijklmnopqrstuvwxyz");
1456562Sphk	MDString
1466562Sphk	    ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
1476562Sphk	MDString
1486562Sphk	    ("1234567890123456789012345678901234567890\
1493995Spst1234567890123456789012345678901234567890");
1503995Spst}
1513995Spst
1526562Sphk/*
1536562Sphk * Digests the standard input and prints the result.
1543995Spst */
1556562Sphkstatic void
1566725SphkMDFilter(int pipe)
1573995Spst{
1586562Sphk	MD5_CTX context;
1596562Sphk	int     len;
1606562Sphk	unsigned char buffer[BUFSIZ], digest[16];
1613995Spst
1626562Sphk	MD5Init(&context);
1636725Sphk	while (len = fread(buffer, 1, BUFSIZ, stdin)) {
1646726Sphk		if(pipe && (len != fwrite(buffer, 1, len, stdout))) {
1656726Sphk			perror("stdout");
1666726Sphk			exit(1);
1676726Sphk		}
1686562Sphk		MD5Update(&context, buffer, len);
1696725Sphk	}
1706562Sphk	printf("%s\n", MD5End(&context));
1713995Spst}
172