home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xntp3.zip / authstuff / md5driver.c < prev    next >
C/C++ Source or Header  |  1992-08-04  |  6KB  |  197 lines

  1. /*
  2.  ***********************************************************************
  3.  ** md5driver.c -- sample test routines                               **
  4.  ** RSA Data Security, Inc. MD5 Message-Digest Algorithm              **
  5.  ** Created: 2/16/90 RLR                                              **
  6.  ** Updated: 1/91 SRD                                                 **
  7.  ** Updated: 7/91 SRD Removed file "foo" from test suite              **
  8.  ***********************************************************************
  9.  */
  10.  
  11. /*
  12.  ***********************************************************************
  13.  ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved.  **
  14.  **                                                                   **
  15.  ** RSA Data Security, Inc. makes no representations concerning       **
  16.  ** either the merchantability of this software or the suitability    **
  17.  ** of this software for any particular purpose.  It is provided "as  **
  18.  ** is" without express or implied warranty of any kind.              **
  19.  **                                                                   **
  20.  ** These notices must be retained in any copies of any part of this  **
  21.  ** documentation and/or software.                                    **
  22.  ***********************************************************************
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <sys/types.h>
  27. #include <time.h>
  28. #include <string.h>
  29. #include "md5.h"
  30.  
  31. /* Prints message digest buffer in mdContext as 32 hexadecimal digits.
  32.    Order is from low-order byte to high-order byte of digest.
  33.    Each byte is printed with high-order hexadecimal digit first.
  34.  */
  35. static void MDPrint (mdContext)
  36. MD5_CTX *mdContext;
  37. {
  38.   int i;
  39.  
  40.   for (i = 0; i < 16; i++)
  41.     printf ("%02x", mdContext->digest[i]);
  42. }
  43.  
  44. /* size of test block */
  45. #define TEST_BLOCK_SIZE 1000
  46.  
  47. /* number of blocks to process */
  48. #define TEST_BLOCKS 10000
  49.  
  50. /* number of test bytes = TEST_BLOCK_SIZE * TEST_BLOCKS */
  51. static long TEST_BYTES = (long)TEST_BLOCK_SIZE * (long)TEST_BLOCKS;
  52.  
  53. /* A time trial routine, to measure the speed of MD5.
  54.    Measures wall time required to digest TEST_BLOCKS * TEST_BLOCK_SIZE
  55.    characters.
  56.  */
  57. static void MDTimeTrial ()
  58. {
  59.   MD5_CTX mdContext;
  60.   time_t endTime, startTime;
  61.   unsigned char data[TEST_BLOCK_SIZE];
  62.   unsigned int i;
  63.  
  64.   /* initialize test data */
  65.   for (i = 0; i < TEST_BLOCK_SIZE; i++)
  66.     data[i] = (unsigned char)(i & 0xFF);
  67.  
  68.   /* start timer */
  69.   printf ("MD5 time trial. Processing %ld characters...\n", TEST_BYTES);
  70.   time (&startTime);
  71.  
  72.   /* digest data in TEST_BLOCK_SIZE byte blocks */
  73.   MD5Init (&mdContext);
  74.   for (i = TEST_BLOCKS; i > 0; i--)
  75.     MD5Update (&mdContext, data, TEST_BLOCK_SIZE);
  76.   MD5Final (&mdContext);
  77.  
  78.   /* stop timer, get time difference */
  79.   time (&endTime);
  80.   MDPrint (&mdContext);
  81.   printf (" is digest of test input.\n");
  82.   printf
  83.     ("Seconds to process test input: %ld\n", (long)(endTime-startTime));
  84.   printf
  85.     ("Characters processed per second: %ld\n",
  86.      TEST_BYTES/(endTime-startTime));
  87. }
  88.  
  89. /* Computes the message digest for string inString.
  90.    Prints out message digest, a space, the string (in quotes) and a
  91.    carriage return.
  92.  */
  93. static void MDString (inString)
  94. char *inString;
  95. {
  96.   MD5_CTX mdContext;
  97.   unsigned int len = strlen (inString);
  98.  
  99.   MD5Init (&mdContext);
  100.   MD5Update (&mdContext, inString, len);
  101.   MD5Final (&mdContext);
  102.   MDPrint (&mdContext);
  103.   printf (" \"%s\"\n", inString);
  104. }
  105.  
  106. /* Computes the message digest for a specified file.
  107.    Prints out message digest, a space, the file name, and a carriage
  108.    return.
  109.  */
  110. static void MDFile (filename)
  111. char *filename;
  112. {
  113.   FILE *inFile = fopen (filename, "rb");
  114.   MD5_CTX mdContext;
  115.   int bytes;
  116.   unsigned char data[1024];
  117.  
  118.   if (inFile == NULL) {
  119.     printf ("%s can't be opened.\n", filename);
  120.     return;
  121.   }
  122.  
  123.   MD5Init (&mdContext);
  124.   while ((bytes = fread (data, 1, 1024, inFile)) != 0)
  125.     MD5Update (&mdContext, data, bytes);
  126.   MD5Final (&mdContext);
  127.   MDPrint (&mdContext);
  128.   printf (" %s\n", filename);
  129.   fclose (inFile);
  130. }
  131.  
  132. /* Writes the message digest of the data from stdin onto stdout,
  133.    followed by a carriage return.
  134.  */
  135. static void MDFilter ()
  136. {
  137.   MD5_CTX mdContext;
  138.   int bytes;
  139.   unsigned char data[16];
  140.  
  141.   MD5Init (&mdContext);
  142.   while ((bytes = fread (data, 1, 16, stdin)) != 0)
  143.     MD5Update (&mdContext, data, bytes);
  144.   MD5Final (&mdContext);
  145.   MDPrint (&mdContext);
  146.   printf ("\n");
  147. }
  148.  
  149. /* Runs a standard suite of test data.
  150.  */
  151. static void MDTestSuite ()
  152. {
  153.   printf ("MD5 test suite results:\n");
  154.   MDString ("");
  155.   MDString ("a");
  156.   MDString ("abc");
  157.   MDString ("message digest");
  158.   MDString ("abcdefghijklmnopqrstuvwxyz");
  159.   MDString
  160.     ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
  161.   MDString
  162.     ("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
  163. }
  164.  
  165. void main (argc, argv)
  166. int argc;
  167. char *argv[];
  168. {
  169.   int i;
  170.  
  171.   /* For each command line argument in turn:
  172.   ** filename          -- prints message digest and name of file
  173.   ** -sstring          -- prints message digest and contents of string
  174.   ** -t                -- prints time trial statistics for 10M
  175.                           characters
  176.   ** -x                -- execute a standard suite of test data
  177.   ** (no args)         -- writes messages digest of stdin onto stdout
  178.   */
  179.   if (argc == 1)
  180.     MDFilter ();
  181.   else
  182.     for (i = 1; i < argc; i++)
  183.       if (argv[i][0] == '-' && argv[i][1] == 's')
  184.         MDString (argv[i] + 2);
  185.       else if (strcmp (argv[i], "-t") == 0)
  186.         MDTimeTrial ();
  187.       else if (strcmp (argv[i], "-x") == 0)
  188.         MDTestSuite ();
  189.       else MDFile (argv[i]);
  190. }
  191.  
  192. /*
  193.  ***********************************************************************
  194.  ** End of md5driver.c                                                **
  195.  ******************************** (cut) ********************************
  196.  */
  197.