/* MDDRIVER.C - test driver for MD2, MD4 and MD5
/* Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
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.
/* The following makes MD default to MD5 if it has not already been
defined with C compiler flags.
/* Length of test block, number of test blocks.
#define TEST_BLOCK_LEN 1000
#define TEST_BLOCK_COUNT 1000
static void MDString
PROTO_LIST ((char *));
static void MDTimeTrial
PROTO_LIST ((void));
static void MDTestSuite
PROTO_LIST ((void));
static void MDFile
PROTO_LIST ((char *));
static void MDFilter
PROTO_LIST ((void));
static void MDPrint
PROTO_LIST ((unsigned char [16]));
#define MDUpdate MD2Update
#define MDUpdate MD4Update
#define MDUpdate MD5Update
Arguments (may be any combination):
-sstring - digests string
(none) - digests standard input
for (i
= 1; i
< argc
; i
++)
if (argv
[i
][0] == '-' && argv
[i
][1] == 's')
else if (strcmp (argv
[i
], "-t") == 0)
else if (strcmp (argv
[i
], "-x") == 0)
/* Digests a string and prints the result.
static void MDString (string
)
unsigned char digest
[16];
unsigned int len
= strlen (string
);
MDUpdate (&context
, string
, len
);
MDFinal (digest
, &context
);
printf ("MD%d (\"%s\") = ", MD
, string
);
/* Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte
static void MDTimeTrial ()
time_t endTime
, startTime
;
unsigned char block
[TEST_BLOCK_LEN
], digest
[16];
("MD%d time trial. Digesting %d %d-byte blocks ...", MD
,
TEST_BLOCK_LEN
, TEST_BLOCK_COUNT
);
for (i
= 0; i
< TEST_BLOCK_LEN
; i
++)
block
[i
] = (unsigned char)(i
& 0xff);
for (i
= 0; i
< TEST_BLOCK_COUNT
; i
++)
MDUpdate (&context
, block
, TEST_BLOCK_LEN
);
MDFinal (digest
, &context
);
printf ("\nTime = %ld seconds\n", (long)(endTime
-startTime
));
* Be careful that endTime-startTime is not zero.
* (Bug fix from Ric Anderson, ric@Artisoft.COM.)
("Speed = %ld bytes/second\n",
(long)TEST_BLOCK_LEN
* (long)TEST_BLOCK_COUNT
/((endTime
-startTime
) != 0 ? (endTime
-startTime
):1));
/* Digests a reference suite of strings and prints the results.
static void MDTestSuite ()
printf ("MD%d test suite:\n", MD
);
MDString ("message digest");
MDString ("abcdefghijklmnopqrstuvwxyz");
("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
("1234567890123456789012345678901234567890\
1234567890123456789012345678901234567890");
/* Digests a file and prints the result.
static void MDFile (filename
)
unsigned char buffer
[1024], digest
[16];
if ((file
= fopen (filename
, "rb")) == NULL
)
printf ("%s can't be opened\n", filename
);
while (len
= fread (buffer
, 1, 1024, file
))
MDUpdate (&context
, buffer
, len
);
MDFinal (digest
, &context
);
printf ("MD%d (%s) = ", MD
, filename
);
/* Digests the standard input and prints the result.
unsigned char buffer
[16], digest
[16];
while (len
= fread (buffer
, 1, 16, stdin
))
MDUpdate (&context
, buffer
, len
);
MDFinal (digest
, &context
);
/* Prints a message digest in hexadecimal.
static void MDPrint (digest
)
unsigned char digest
[16];
printf ("%02x", digest
[i
]);