1995-02-20 00:48:50 +00:00
|
|
|
/*
|
1997-02-22 14:40:44 +00:00
|
|
|
* $Id$
|
1995-02-20 00:48:50 +00:00
|
|
|
*
|
|
|
|
* Derived from:
|
1994-10-29 23:58:02 +00:00
|
|
|
*/
|
|
|
|
|
1995-02-20 00:48:50 +00:00
|
|
|
/*
|
|
|
|
* MDDRIVER.C - test driver for MD2, MD4 and MD5
|
1994-10-29 23:58:02 +00:00
|
|
|
*/
|
|
|
|
|
1995-02-20 00:48:50 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
|
|
|
|
* rights reserved.
|
|
|
|
*
|
|
|
|
* RSA Data Security, Inc. makes no representations concerning either
|
|
|
|
* the merchantability of this software or the suitability of this
|
|
|
|
* software for any particular purpose. It is provided "as is"
|
|
|
|
* without express or implied warranty of any kind.
|
|
|
|
*
|
|
|
|
* These notices must be retained in any copies of any part of this
|
|
|
|
* documentation and/or software.
|
1994-10-29 23:58:02 +00:00
|
|
|
*/
|
|
|
|
|
1996-10-25 06:48:29 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <md5.h>
|
|
|
|
|
1994-10-29 23:58:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
1996-10-25 06:48:29 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
1994-10-29 23:58:02 +00:00
|
|
|
#include "global.h"
|
|
|
|
|
1995-02-20 00:48:50 +00:00
|
|
|
/*
|
|
|
|
* Length of test block, number of test blocks.
|
1994-10-29 23:58:02 +00:00
|
|
|
*/
|
|
|
|
#define TEST_BLOCK_LEN 1000
|
|
|
|
#define TEST_BLOCK_COUNT 1000
|
|
|
|
|
1995-02-20 00:48:50 +00:00
|
|
|
static void MDString PROTO_LIST((char *));
|
|
|
|
static void MDTimeTrial PROTO_LIST((void));
|
|
|
|
static void MDTestSuite PROTO_LIST((void));
|
1995-02-26 01:55:31 +00:00
|
|
|
static void MDFilter PROTO_LIST((int));
|
1994-10-29 23:58:02 +00:00
|
|
|
|
|
|
|
/* Main driver.
|
|
|
|
|
|
|
|
Arguments (may be any combination):
|
|
|
|
-sstring - digests string
|
|
|
|
-t - runs time trial
|
|
|
|
-x - runs test script
|
|
|
|
filename - digests file
|
|
|
|
(none) - digests standard input
|
|
|
|
*/
|
1995-02-20 00:48:50 +00:00
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
1994-10-29 23:58:02 +00:00
|
|
|
{
|
1995-02-20 00:48:50 +00:00
|
|
|
int i;
|
|
|
|
char *p;
|
1995-07-12 09:14:46 +00:00
|
|
|
char buf[33];
|
1995-02-20 00:48:50 +00:00
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
if (argv[i][0] == '-' && argv[i][1] == 's')
|
|
|
|
MDString(argv[i] + 2);
|
|
|
|
else if (strcmp(argv[i], "-t") == 0)
|
|
|
|
MDTimeTrial();
|
1995-02-26 01:55:31 +00:00
|
|
|
else if (strcmp(argv[i], "-p") == 0)
|
|
|
|
MDFilter(1);
|
1995-02-20 00:48:50 +00:00
|
|
|
else if (strcmp(argv[i], "-x") == 0)
|
|
|
|
MDTestSuite();
|
|
|
|
else {
|
1995-07-12 09:14:46 +00:00
|
|
|
p = MD5File(argv[i],buf);
|
1995-05-30 06:12:45 +00:00
|
|
|
if (!p)
|
1995-02-20 00:48:50 +00:00
|
|
|
perror(argv[i]);
|
|
|
|
else
|
|
|
|
printf("MD5 (%s) = %s\n", argv[i], p);
|
|
|
|
}
|
|
|
|
else
|
1995-02-26 01:55:31 +00:00
|
|
|
MDFilter(0);
|
1995-02-20 00:48:50 +00:00
|
|
|
|
|
|
|
return (0);
|
1994-10-29 23:58:02 +00:00
|
|
|
}
|
1995-02-20 00:48:50 +00:00
|
|
|
/*
|
|
|
|
* Digests a string and prints the result.
|
1994-10-29 23:58:02 +00:00
|
|
|
*/
|
1995-02-20 00:48:50 +00:00
|
|
|
static void
|
|
|
|
MDString(string)
|
|
|
|
char *string;
|
1994-10-29 23:58:02 +00:00
|
|
|
{
|
1995-02-20 00:48:50 +00:00
|
|
|
unsigned int len = strlen(string);
|
1995-07-12 09:14:46 +00:00
|
|
|
char buf[33];
|
1994-10-29 23:58:02 +00:00
|
|
|
|
1995-07-12 09:14:46 +00:00
|
|
|
printf("MD5 (\"%s\") = %s\n", string, MD5Data(string, len, buf));
|
1994-10-29 23:58:02 +00:00
|
|
|
}
|
1995-02-20 00:48:50 +00:00
|
|
|
/*
|
|
|
|
* Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks.
|
1994-10-29 23:58:02 +00:00
|
|
|
*/
|
1995-02-20 00:48:50 +00:00
|
|
|
static void
|
|
|
|
MDTimeTrial()
|
1994-10-29 23:58:02 +00:00
|
|
|
{
|
1995-02-20 00:48:50 +00:00
|
|
|
MD5_CTX context;
|
|
|
|
time_t endTime, startTime;
|
1995-02-26 01:55:31 +00:00
|
|
|
unsigned char block[TEST_BLOCK_LEN];
|
1995-02-20 00:48:50 +00:00
|
|
|
unsigned int i;
|
1995-07-12 09:14:46 +00:00
|
|
|
char *p, buf[33];
|
1995-02-20 00:48:50 +00:00
|
|
|
|
|
|
|
printf
|
|
|
|
("MD5 time trial. Digesting %d %d-byte blocks ...",
|
1997-01-16 10:12:30 +00:00
|
|
|
TEST_BLOCK_COUNT, TEST_BLOCK_LEN);
|
1995-02-20 00:48:50 +00:00
|
|
|
|
|
|
|
/* Initialize block */
|
|
|
|
for (i = 0; i < TEST_BLOCK_LEN; i++)
|
|
|
|
block[i] = (unsigned char) (i & 0xff);
|
|
|
|
|
|
|
|
/* Start timer */
|
|
|
|
time(&startTime);
|
|
|
|
|
|
|
|
/* Digest blocks */
|
|
|
|
MD5Init(&context);
|
|
|
|
for (i = 0; i < TEST_BLOCK_COUNT; i++)
|
|
|
|
MD5Update(&context, block, TEST_BLOCK_LEN);
|
1995-07-12 09:14:46 +00:00
|
|
|
p = MD5End(&context,buf);
|
1995-02-20 00:48:50 +00:00
|
|
|
|
|
|
|
/* Stop timer */
|
|
|
|
time(&endTime);
|
|
|
|
|
|
|
|
printf(" done\n");
|
|
|
|
printf("Digest = %s", p);
|
|
|
|
printf("\nTime = %ld seconds\n", (long) (endTime - startTime));
|
|
|
|
/* Be careful that endTime-startTime is not zero. (Bug fix from Ric
|
|
|
|
* Anderson, ric@Artisoft.COM.) */
|
|
|
|
printf
|
|
|
|
("Speed = %ld bytes/second\n",
|
|
|
|
(long) TEST_BLOCK_LEN * (long) TEST_BLOCK_COUNT / ((endTime - startTime) != 0 ? (endTime - startTime) : 1));
|
1994-10-29 23:58:02 +00:00
|
|
|
}
|
1995-02-20 00:48:50 +00:00
|
|
|
/*
|
|
|
|
* Digests a reference suite of strings and prints the results.
|
1994-10-29 23:58:02 +00:00
|
|
|
*/
|
1995-02-20 00:48:50 +00:00
|
|
|
static void
|
|
|
|
MDTestSuite()
|
1994-10-29 23:58:02 +00:00
|
|
|
{
|
1995-02-20 00:48:50 +00:00
|
|
|
printf("MD5 test suite:\n");
|
|
|
|
|
|
|
|
MDString("");
|
|
|
|
MDString("a");
|
|
|
|
MDString("abc");
|
|
|
|
MDString("message digest");
|
|
|
|
MDString("abcdefghijklmnopqrstuvwxyz");
|
|
|
|
MDString
|
|
|
|
("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
|
|
|
|
MDString
|
|
|
|
("1234567890123456789012345678901234567890\
|
1994-10-29 23:58:02 +00:00
|
|
|
1234567890123456789012345678901234567890");
|
|
|
|
}
|
|
|
|
|
1995-02-20 00:48:50 +00:00
|
|
|
/*
|
|
|
|
* Digests the standard input and prints the result.
|
1994-10-29 23:58:02 +00:00
|
|
|
*/
|
1995-02-20 00:48:50 +00:00
|
|
|
static void
|
1995-02-26 01:55:31 +00:00
|
|
|
MDFilter(int pipe)
|
1994-10-29 23:58:02 +00:00
|
|
|
{
|
1995-02-20 00:48:50 +00:00
|
|
|
MD5_CTX context;
|
|
|
|
int len;
|
|
|
|
unsigned char buffer[BUFSIZ], digest[16];
|
1995-07-12 09:14:46 +00:00
|
|
|
char buf[33];
|
1995-02-20 00:48:50 +00:00
|
|
|
|
|
|
|
MD5Init(&context);
|
1995-02-26 01:55:31 +00:00
|
|
|
while (len = fread(buffer, 1, BUFSIZ, stdin)) {
|
1995-02-26 02:00:35 +00:00
|
|
|
if(pipe && (len != fwrite(buffer, 1, len, stdout))) {
|
|
|
|
perror("stdout");
|
|
|
|
exit(1);
|
|
|
|
}
|
1995-02-20 00:48:50 +00:00
|
|
|
MD5Update(&context, buffer, len);
|
1995-02-26 01:55:31 +00:00
|
|
|
}
|
1995-07-12 09:14:46 +00:00
|
|
|
printf("%s\n", MD5End(&context,buf));
|
1994-10-29 23:58:02 +00:00
|
|
|
}
|