mirror of
https://git.FreeBSD.org/src.git
synced 2025-01-04 12:52:15 +00:00
Clean this file up so it is readable...
This commit is contained in:
parent
a3f4faced6
commit
9b1fd0e50e
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=6562
289
sbin/md5/md5.c
289
sbin/md5/md5.c
@ -1,69 +1,42 @@
|
||||
/* MDDRIVER.C - test driver for MD2, MD4 and MD5
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* Derived from:
|
||||
*/
|
||||
|
||||
/* 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.
|
||||
/*
|
||||
* MDDRIVER.C - test driver for MD2, MD4 and MD5
|
||||
*/
|
||||
|
||||
/* The following makes MD default to MD5 if it has not already been
|
||||
defined with C compiler flags.
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MD
|
||||
#define MD 5
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
#include "global.h"
|
||||
#if MD == 2
|
||||
#include <md2.h>
|
||||
#endif
|
||||
#if MD == 4
|
||||
#include <md4.h>
|
||||
#endif
|
||||
#if MD == 5
|
||||
#include <md5.h>
|
||||
#endif
|
||||
|
||||
/* Length of test block, number of test blocks.
|
||||
/*
|
||||
* 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]));
|
||||
|
||||
#if MD == 2
|
||||
#define MD_CTX MD2_CTX
|
||||
#define MDInit MD2Init
|
||||
#define MDUpdate MD2Update
|
||||
#define MDFinal MD2Final
|
||||
#endif
|
||||
#if MD == 4
|
||||
#define MD_CTX MD4_CTX
|
||||
#define MDInit MD4Init
|
||||
#define MDUpdate MD4Update
|
||||
#define MDFinal MD4Final
|
||||
#endif
|
||||
#if MD == 5
|
||||
#define MD_CTX MD5_CTX
|
||||
#define MDInit MD5Init
|
||||
#define MDUpdate MD5Update
|
||||
#define MDFinal MD5Final
|
||||
#endif
|
||||
static void MDString PROTO_LIST((char *));
|
||||
static void MDTimeTrial PROTO_LIST((void));
|
||||
static void MDTestSuite PROTO_LIST((void));
|
||||
static void MDFilter PROTO_LIST((void));
|
||||
|
||||
/* Main driver.
|
||||
|
||||
@ -74,158 +47,118 @@ Arguments (may be any combination):
|
||||
filename - digests file
|
||||
(none) - digests standard input
|
||||
*/
|
||||
int main (argc, argv)
|
||||
int argc;
|
||||
char *argv[];
|
||||
int
|
||||
main(argc, argv)
|
||||
int argc;
|
||||
char *argv[];
|
||||
{
|
||||
int i;
|
||||
int i;
|
||||
char *p;
|
||||
|
||||
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 ();
|
||||
else if (strcmp (argv[i], "-x") == 0)
|
||||
MDTestSuite ();
|
||||
else
|
||||
MDFile (argv[i]);
|
||||
else
|
||||
MDFilter ();
|
||||
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();
|
||||
else if (strcmp(argv[i], "-x") == 0)
|
||||
MDTestSuite();
|
||||
else {
|
||||
p = MD5File(argv[i]);
|
||||
if (!p)
|
||||
perror(argv[i]);
|
||||
else
|
||||
printf("MD5 (%s) = %s\n", argv[i], p);
|
||||
}
|
||||
else
|
||||
MDFilter();
|
||||
|
||||
return (0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* Digests a string and prints the result.
|
||||
/*
|
||||
* Digests a string and prints the result.
|
||||
*/
|
||||
static void MDString (string)
|
||||
char *string;
|
||||
static void
|
||||
MDString(string)
|
||||
char *string;
|
||||
{
|
||||
MD_CTX context;
|
||||
unsigned char digest[16];
|
||||
unsigned int len = strlen (string);
|
||||
unsigned int len = strlen(string);
|
||||
|
||||
MDInit (&context);
|
||||
MDUpdate (&context, string, len);
|
||||
MDFinal (digest, &context);
|
||||
|
||||
printf ("MD%d (\"%s\") = ", MD, string);
|
||||
MDPrint (digest);
|
||||
printf ("\n");
|
||||
printf("MD5 (\"%s\") = %s\n", string, MD5Data(string, len));
|
||||
}
|
||||
|
||||
/* Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte
|
||||
blocks.
|
||||
/*
|
||||
* Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte blocks.
|
||||
*/
|
||||
static void MDTimeTrial ()
|
||||
static void
|
||||
MDTimeTrial()
|
||||
{
|
||||
MD_CTX context;
|
||||
time_t endTime, startTime;
|
||||
unsigned char block[TEST_BLOCK_LEN], digest[16];
|
||||
unsigned int i;
|
||||
MD5_CTX context;
|
||||
time_t endTime, startTime;
|
||||
unsigned char block[TEST_BLOCK_LEN], digest[16];
|
||||
unsigned int i;
|
||||
char *p;
|
||||
|
||||
printf
|
||||
("MD%d time trial. Digesting %d %d-byte blocks ...", MD,
|
||||
TEST_BLOCK_LEN, TEST_BLOCK_COUNT);
|
||||
printf
|
||||
("MD5 time trial. Digesting %d %d-byte blocks ...",
|
||||
TEST_BLOCK_LEN, TEST_BLOCK_COUNT);
|
||||
|
||||
/* Initialize block */
|
||||
for (i = 0; i < TEST_BLOCK_LEN; i++)
|
||||
block[i] = (unsigned char)(i & 0xff);
|
||||
/* Initialize block */
|
||||
for (i = 0; i < TEST_BLOCK_LEN; i++)
|
||||
block[i] = (unsigned char) (i & 0xff);
|
||||
|
||||
/* Start timer */
|
||||
time (&startTime);
|
||||
/* Start timer */
|
||||
time(&startTime);
|
||||
|
||||
/* Digest blocks */
|
||||
MDInit (&context);
|
||||
for (i = 0; i < TEST_BLOCK_COUNT; i++)
|
||||
MDUpdate (&context, block, TEST_BLOCK_LEN);
|
||||
MDFinal (digest, &context);
|
||||
/* Digest blocks */
|
||||
MD5Init(&context);
|
||||
for (i = 0; i < TEST_BLOCK_COUNT; i++)
|
||||
MD5Update(&context, block, TEST_BLOCK_LEN);
|
||||
p = MD5End(&context);
|
||||
|
||||
/* Stop timer */
|
||||
time (&endTime);
|
||||
/* Stop timer */
|
||||
time(&endTime);
|
||||
|
||||
printf (" done\n");
|
||||
printf ("Digest = ");
|
||||
MDPrint (digest);
|
||||
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));
|
||||
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));
|
||||
}
|
||||
|
||||
/* Digests a reference suite of strings and prints the results.
|
||||
/*
|
||||
* Digests a reference suite of strings and prints the results.
|
||||
*/
|
||||
static void MDTestSuite ()
|
||||
static void
|
||||
MDTestSuite()
|
||||
{
|
||||
printf ("MD%d test suite:\n", MD);
|
||||
printf("MD5 test suite:\n");
|
||||
|
||||
MDString ("");
|
||||
MDString ("a");
|
||||
MDString ("abc");
|
||||
MDString ("message digest");
|
||||
MDString ("abcdefghijklmnopqrstuvwxyz");
|
||||
MDString
|
||||
("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
|
||||
MDString
|
||||
("1234567890123456789012345678901234567890\
|
||||
MDString("");
|
||||
MDString("a");
|
||||
MDString("abc");
|
||||
MDString("message digest");
|
||||
MDString("abcdefghijklmnopqrstuvwxyz");
|
||||
MDString
|
||||
("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
|
||||
MDString
|
||||
("1234567890123456789012345678901234567890\
|
||||
1234567890123456789012345678901234567890");
|
||||
}
|
||||
|
||||
/* Digests a file and prints the result.
|
||||
/*
|
||||
* Digests the standard input and prints the result.
|
||||
*/
|
||||
static void MDFile (filename)
|
||||
char *filename;
|
||||
static void
|
||||
MDFilter()
|
||||
{
|
||||
FILE *file;
|
||||
MD_CTX context;
|
||||
int len;
|
||||
unsigned char buffer[1024], digest[16];
|
||||
MD5_CTX context;
|
||||
int len;
|
||||
unsigned char buffer[BUFSIZ], digest[16];
|
||||
|
||||
if ((file = fopen (filename, "rb")) == NULL)
|
||||
printf ("%s can't be opened\n", filename);
|
||||
|
||||
else {
|
||||
MDInit (&context);
|
||||
while (len = fread (buffer, 1, 1024, file))
|
||||
MDUpdate (&context, buffer, len);
|
||||
MDFinal (digest, &context);
|
||||
|
||||
fclose (file);
|
||||
|
||||
printf ("MD%d (%s) = ", MD, filename);
|
||||
MDPrint (digest);
|
||||
printf ("\n");
|
||||
}
|
||||
}
|
||||
|
||||
/* Digests the standard input and prints the result.
|
||||
*/
|
||||
static void MDFilter ()
|
||||
{
|
||||
MD_CTX context;
|
||||
int len;
|
||||
unsigned char buffer[16], digest[16];
|
||||
|
||||
MDInit (&context);
|
||||
while (len = fread (buffer, 1, 16, stdin))
|
||||
MDUpdate (&context, buffer, len);
|
||||
MDFinal (digest, &context);
|
||||
|
||||
MDPrint (digest);
|
||||
printf ("\n");
|
||||
}
|
||||
|
||||
/* Prints a message digest in hexadecimal.
|
||||
*/
|
||||
static void MDPrint (digest)
|
||||
unsigned char digest[16];
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < 16; i++)
|
||||
printf ("%02x", digest[i]);
|
||||
MD5Init(&context);
|
||||
while (len = fread(buffer, 1, BUFSIZ, stdin))
|
||||
MD5Update(&context, buffer, len);
|
||||
printf("%s\n", MD5End(&context));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user