1995-02-20 00:48:50 +00:00
|
|
|
/*
|
|
|
|
* Derived from:
|
1998-07-06 07:04:50 +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
|
|
|
*/
|
|
|
|
|
1998-07-06 07:04:50 +00:00
|
|
|
#ifndef lint
|
|
|
|
static const char rcsid[] =
|
1999-08-28 00:22:10 +00:00
|
|
|
"$FreeBSD$";
|
1998-07-06 07:04:50 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1996-10-25 06:48:29 +00:00
|
|
|
#include <sys/types.h>
|
1998-07-06 07:04:50 +00:00
|
|
|
#include <err.h>
|
1996-10-25 06:48:29 +00:00
|
|
|
#include <md5.h>
|
1994-10-29 23:58:02 +00:00
|
|
|
#include <stdio.h>
|
1996-10-25 06:48:29 +00:00
|
|
|
#include <time.h>
|
1997-12-29 03:40:37 +00:00
|
|
|
#include <unistd.h>
|
2001-06-24 23:04:23 +00:00
|
|
|
#include <stdlib.h>
|
1999-07-21 00:51:14 +00:00
|
|
|
#include <string.h>
|
1996-10-25 06:48:29 +00:00
|
|
|
|
1995-02-20 00:48:50 +00:00
|
|
|
/*
|
|
|
|
* Length of test block, number of test blocks.
|
1994-10-29 23:58:02 +00:00
|
|
|
*/
|
1999-05-01 14:54:21 +00:00
|
|
|
#define TEST_BLOCK_LEN 10000
|
|
|
|
#define TEST_BLOCK_COUNT 100000
|
1994-10-29 23:58:02 +00:00
|
|
|
|
1999-12-04 01:29:43 +00:00
|
|
|
int qflag;
|
1999-11-07 04:14:55 +00:00
|
|
|
int rflag;
|
|
|
|
|
2001-05-22 10:33:43 +00:00
|
|
|
static void MDString(const char *);
|
|
|
|
static void MDTimeTrial(void);
|
|
|
|
static void MDTestSuite(void);
|
|
|
|
static void MDFilter(int);
|
|
|
|
static void usage(void);
|
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
|
2001-05-22 10:33:43 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-10-29 23:58:02 +00:00
|
|
|
{
|
1997-12-29 14:05:49 +00:00
|
|
|
int ch;
|
1995-02-20 00:48:50 +00:00
|
|
|
char *p;
|
1995-07-12 09:14:46 +00:00
|
|
|
char buf[33];
|
1995-02-20 00:48:50 +00:00
|
|
|
|
2001-05-22 10:33:43 +00:00
|
|
|
while ((ch = getopt(argc, argv, "ps:qrtx")) != -1)
|
|
|
|
switch (ch) {
|
|
|
|
case 'p':
|
|
|
|
MDFilter(1);
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
qflag = 1;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
rflag = 1;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
MDString(optarg);
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
MDTimeTrial();
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
MDTestSuite();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
1997-12-29 03:40:37 +00:00
|
|
|
}
|
2001-05-22 10:33:43 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (*argv) {
|
|
|
|
do {
|
|
|
|
p = MD5File(*argv, buf);
|
1997-12-29 03:40:37 +00:00
|
|
|
if (!p)
|
2001-05-22 10:33:43 +00:00
|
|
|
warn("%s", *argv);
|
1997-12-29 03:40:37 +00:00
|
|
|
else
|
1999-12-04 01:29:43 +00:00
|
|
|
if (qflag)
|
|
|
|
printf("%s\n", p);
|
|
|
|
else if (rflag)
|
2001-05-22 10:33:43 +00:00
|
|
|
printf("%s %s\n", p, *argv);
|
1999-11-07 04:14:55 +00:00
|
|
|
else
|
2001-05-22 10:33:43 +00:00
|
|
|
printf("MD5 (%s) = %s\n", *argv, p);
|
|
|
|
} while (*++argv);
|
1997-12-29 03:40:37 +00:00
|
|
|
} 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
|
2001-05-22 10:33:43 +00:00
|
|
|
MDString(const char *string)
|
1994-10-29 23:58:02 +00:00
|
|
|
{
|
1999-07-21 00:51:14 +00:00
|
|
|
size_t len = strlen(string);
|
1995-07-12 09:14:46 +00:00
|
|
|
char buf[33];
|
1994-10-29 23:58:02 +00:00
|
|
|
|
1999-12-04 01:29:43 +00:00
|
|
|
if (qflag)
|
|
|
|
printf("%s\n", MD5Data(string, len, buf));
|
|
|
|
else if (rflag)
|
1999-11-11 11:59:03 +00:00
|
|
|
printf("%s \"%s\"\n", MD5Data(string, len, buf), string);
|
1999-11-07 04:14:55 +00:00
|
|
|
else
|
|
|
|
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
|
2001-05-22 10:33:43 +00:00
|
|
|
MDTimeTrial(void)
|
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);
|
1999-05-01 14:54:21 +00:00
|
|
|
fflush(stdout);
|
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
|
2001-05-22 10:33:43 +00:00
|
|
|
MDTestSuite(void)
|
1994-10-29 23:58:02 +00:00
|
|
|
{
|
1997-12-29 14:05:49 +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
|
2001-05-22 10:33:43 +00:00
|
|
|
MDFilter(int tee)
|
1994-10-29 23:58:02 +00:00
|
|
|
{
|
1995-02-20 00:48:50 +00:00
|
|
|
MD5_CTX context;
|
2001-05-22 10:33:43 +00:00
|
|
|
unsigned int len;
|
1997-12-29 03:40:37 +00:00
|
|
|
unsigned char buffer[BUFSIZ];
|
1995-07-12 09:14:46 +00:00
|
|
|
char buf[33];
|
1995-02-20 00:48:50 +00:00
|
|
|
|
|
|
|
MD5Init(&context);
|
1997-12-29 03:40:37 +00:00
|
|
|
while ((len = fread(buffer, 1, BUFSIZ, stdin))) {
|
2001-05-22 10:33:43 +00:00
|
|
|
if (tee && len != fwrite(buffer, 1, len, stdout))
|
1998-07-06 07:04:50 +00:00
|
|
|
err(1, "stdout");
|
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
|
|
|
}
|
1997-12-29 03:40:37 +00:00
|
|
|
|
|
|
|
static void
|
2001-05-22 10:33:43 +00:00
|
|
|
usage(void)
|
1997-12-29 03:40:37 +00:00
|
|
|
{
|
1997-12-29 14:05:49 +00:00
|
|
|
|
2000-11-08 20:41:35 +00:00
|
|
|
fprintf(stderr, "usage: md5 [-pqrtx] [-s string] [files ...]\n");
|
1997-12-29 03:40:37 +00:00
|
|
|
exit(1);
|
|
|
|
}
|