home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / md5.lzh / MD5 / mddriver.c < prev    next >
C/C++ Source or Header  |  1996-01-12  |  6KB  |  257 lines

  1. /* MDDRIVER.C - test driver for MD2, MD4 and MD5
  2.  */
  3.  
  4. /* Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
  5. rights reserved.
  6.  
  7. RSA Data Security, Inc. makes no representations concerning either
  8. the merchantability of this software or the suitability of this
  9. software for any particular purpose. It is provided "as is"
  10. without express or implied warranty of any kind.
  11.  
  12. These notices must be retained in any copies of any part of this
  13. documentation and/or software.
  14.  */
  15.  
  16. /* The following makes MD default to MD5 if it has not already been
  17.   defined with C compiler flags.
  18.  */
  19. #ifndef MD
  20. #ifndef OSK
  21. #define MD MD5
  22. #else
  23. #define MD 5
  24. #endif
  25. #endif
  26.  
  27. #include <stdio.h>
  28. #ifdef OSK
  29. #include "/dd/blarsdefs/time.h"
  30. #include "/dd/blarsdefs/string.h"
  31. #else
  32. #include <time.h>
  33. #include <string.h>
  34. #endif
  35.  
  36. #include "global.h"
  37.  
  38. #ifdef MD2
  39. #include "md2.h"
  40. #endif
  41. #ifdef MD4
  42. #include "md4.h"
  43. #endif
  44. #ifdef MD5
  45. #include "md5.h"
  46. #endif
  47.  
  48. /* Length of test block, number of test blocks.
  49.  */
  50. #define TEST_BLOCK_LEN 1000
  51. #define TEST_BLOCK_COUNT 1000
  52.  
  53. static void MDString();
  54. static void MDTimeTrial();
  55. static void MDTestSuite();
  56. static void MDFile();
  57. static void MDFilter();
  58. static void MDPrint();
  59.  
  60. #ifdef MD2
  61. #define MD_CTX MD2_CTX
  62. #define MDInit MD2Init
  63. #define MDUpdate MD2Update
  64. #define MDFinal MD2Final
  65. #endif
  66. #ifdef MD4
  67. #define MD_CTX MD4_CTX
  68. #define MDInit MD4Init
  69. #define MDUpdate MD4Update
  70. #define MDFinal MD4Final
  71. #endif
  72. #ifdef MD5
  73. #define MD_CTX MD5_CTX
  74. #define MDInit MD5Init
  75. #define MDUpdate MD5Update
  76. #define MDFinal MD5Final
  77. #endif
  78.  
  79. /* Main driver.
  80.  
  81. Arguments (may be any combination):
  82.   -sstring - digests string
  83.   -t       - runs time trial
  84.   -x       - runs test script
  85.   filename - digests file
  86.   (none)   - digests standard input
  87.  */
  88. int main(argc, argv)
  89. int argc;
  90. char *argv[];
  91. {
  92.   int i;
  93.  
  94.   if(argc > 1)
  95.  for(i = 1; i < argc; i++)
  96.    if(argv[i][0] == '-' && argv[i][1] == 's')
  97.      MDString(argv[i] + 2);
  98.    else if(strcmp(argv[i], "-t") == 0)
  99.      MDTimeTrial();
  100.    else if(strcmp(argv[i], "-x") == 0)
  101.      MDTestSuite();
  102. #ifdef OSK
  103.    else if(strcmp(argv[i],"-h") == 0){
  104.    fprintf(stderr,"\nMD%d - Calculate a Message Digest Fingerprint (Checksum)", MD);
  105.    fprintf(stderr,"\n   Usage: MD%d <-opts> <filename>\n", MD);
  106.    fprintf(stderr,"    Opts:  -sstring - digests string\n");
  107.    fprintf(stderr,"           -t       - runs time trial\n");
  108.    fprintf(stderr,"           -x       - runs test script\n");
  109.    fprintf(stderr,"           -h       - this help file\n");
  110.    fprintf(stderr," filename: digests file\n");
  111.    fprintf(stderr,"  (none) : digests standard input\n");}
  112. #endif
  113.    else
  114.      MDFile(argv[i]);
  115.   else
  116.  MDFilter();
  117.  
  118.   return(0);
  119. }
  120.  
  121. /* Digests a string and prints the result.
  122.  */
  123. static void MDString(string)
  124. char *string;
  125. {
  126.   MD_CTX context;
  127.   unsigned char digest[16];
  128.   unsigned int len = strlen(string);
  129.  
  130.   MDInit(&context);
  131.   MDUpdate(&context, string, len);
  132.   MDFinal(digest, &context);
  133.  
  134.   printf("MD%d (\"%s\") = ", MD, string);
  135.   MDPrint(digest);
  136.   printf("\n");
  137. }
  138.  
  139. /* Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte
  140.   blocks.
  141.  */
  142. static void MDTimeTrial()
  143. {
  144.   MD_CTX context;
  145.   time_t endTime, startTime;
  146.   unsigned char block[TEST_BLOCK_LEN], digest[16];
  147.   unsigned int i;
  148.  
  149.   printf("\nMD%d time trial. Digesting %d %d-byte blocks ...", MD, TEST_BLOCK_LEN, TEST_BLOCK_COUNT);
  150.  
  151. #ifdef OSK
  152. fflush(stdout);
  153. #endif
  154.  
  155.   /* Initialize block */
  156.   for(i = 0; i < TEST_BLOCK_LEN; i++)
  157.  block[i] = (unsigned char)(i & 0xff);
  158.  
  159.   /* Start timer */
  160.   time(&startTime);
  161.  
  162.   /* Digest blocks */
  163.   MDInit(&context);
  164.   for(i = 0; i < TEST_BLOCK_COUNT; i++)
  165.  MDUpdate(&context, block, TEST_BLOCK_LEN);
  166.   MDFinal(digest, &context);
  167.  
  168.   /* Stop timer */
  169.   time(&endTime);
  170.  
  171.   printf(" done\n");
  172.   printf("Digest = ");
  173.   MDPrint(digest);
  174.   printf("\nTime = %ld seconds\n", (long)(endTime-startTime));
  175.   /*
  176.    * Be careful that endTime-startTime is not zero.
  177.    * (Bug fix from Ric Anderson, ric@Artisoft.COM.)
  178.    */
  179.   printf("Speed = %ld bytes/second\n",\
  180.   (long)TEST_BLOCK_LEN * (long)TEST_BLOCK_COUNT/((endTime-startTime) != 0 ? (endTime-startTime):1));
  181. }
  182.  
  183. /* Digests a reference suite of strings and prints the results.
  184.  */
  185. static void MDTestSuite()
  186. {
  187.   printf("MD%d test suite:\n", MD);
  188.  
  189.   MDString("");
  190.   MDString("a");
  191.   MDString("abc");
  192.   MDString("message digest");
  193.   MDString("abcdefghijklmnopqrstuvwxyz");
  194.   MDString("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
  195.   MDString("1234567890123456789012345678901234567890\
  196. 1234567890123456789012345678901234567890");
  197. }
  198.  
  199. /* Digests a file and prints the result.
  200.  */
  201. static void MDFile(filename)
  202. char *filename;
  203. {
  204.   FILE *file;
  205.   MD_CTX context;
  206.   int len;
  207.   unsigned char buffer[1024], digest[16];
  208.  
  209. #ifndef OSK
  210.   if((file = fopen(filename, "rb")) == NULL)
  211.  printf("%s can't be opened\n", filename);
  212. #else
  213.   if((file = fopen(filename, "r")) == NULL)
  214.  printf("%s can't be opened\n", filename);
  215. #endif
  216.   else {
  217.  MDInit(&context);
  218.  while(len = fread (buffer, 1, 1024, file))
  219.    MDUpdate(&context, buffer, len);
  220.  MDFinal(digest, &context);
  221.  
  222.  fclose(file);
  223.  
  224.  printf("MD%d (%s) = ", MD, filename);
  225.  MDPrint(digest);
  226.  printf("\n");
  227.   }
  228. }
  229.  
  230. /* Digests the standard input and prints the result.
  231.  */
  232. static void MDFilter()
  233. {
  234.   MD_CTX context;
  235.   int len;
  236.   unsigned char buffer[16], digest[16];
  237.  
  238.   MDInit(&context);
  239.   while(len = fread (buffer, 1, 16, stdin))
  240.  MDUpdate(&context, buffer, len);
  241.   MDFinal(digest, &context);
  242.  
  243.   MDPrint(digest);
  244.   printf("\n");
  245. }
  246.  
  247. /* Prints a message digest in hexadecimal.
  248.  */
  249. static void MDPrint(digest)
  250. unsigned char digest[16];
  251. {
  252.   unsigned int i;
  253.  
  254.   for(i = 0; i < 16; i++)
  255.  printf("%02x", digest[i]);
  256. }
  257.