{\scriptsize \begin{lstlisting}
     1	/**
     2	   @file Testing the md5 checksum generator.
     3	
     4	   @param If no paramater is gven this programm generates the
     5	   checksums if md5test[0-3].dat and compares these with the internal
     6	   stored sums. Otherwise the program generates the md5sums of the
     7	   given file(s) and print these on the screen.
     8	*/
     9	
    10	/*
    11	  Copyright (c) Andreas Hofmeier
    12	  (www.an-h.de, www.an-h.de.vu, www.lgut.uni-bremen.de/an-h/)
    13	  
    14	  This program is free software; you can redistribute it and/or modify
    15	  it under the terms of the GNU General Public License as published by
    16	  the Free Software Foundation; either version 2 of the License, or
    17	  (at your option) any later version.
    18	
    19	  This program is distributed in the hope that it will be useful, but
    20	  WITHOUT ANY WARRANTY; without even the implied warranty of
    21	  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    22	  General Public License for more details.
    23	
    24	  You should have received a copy of the GNU General Public License
    25	  along with this program; if not, write to the Free Software
    26	  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    27	*/
    28	
    29	
    30	#include <stdio.h>
    31	#include "../lib/md5.h"
    32	
    33	/** Buffersize for reading the file to generate a md5sum of it, for
    34	    calculate_md5sum(). */
    35	#define bufsize 2048
    36	
    37	// internal sored md5sums of the md5test[0-4].dat files.
    38	char md5sums[4][16] = {
    39	  { 0x9e, 0xb2, 0x86, 0x6e, 0x09, 0xcd, 0x47, 0xed, 0x53, 0x2d, 0x99, 0xb9, 0xc3, 0xcd, 0x4a, 0x86 }, // md5test0.dat
    40	  { 0xd2, 0xb6, 0x5a, 0xe7, 0x29, 0xe7, 0x93, 0x3c, 0x9c, 0xa9, 0x1c, 0x88, 0xdb, 0x7c, 0xd0, 0xfc }, // md5test1.dat
    41	  { 0x4d, 0x3c, 0x9e, 0xff, 0xe9, 0xb4, 0x97, 0x9a, 0x4b, 0xc7, 0xce, 0x2e, 0x45, 0xdf, 0x21, 0xd2 }, // md5test2.dat
    42	  { 0xf5, 0x5c, 0x17, 0x5c, 0x5f, 0x26, 0x98, 0xf9, 0xb2, 0xcc, 0xf7, 0xba, 0x72, 0x01, 0x4b, 0x14 }, // md5test3.dat
    43	};
    44	
    45	/**
    46	   Calculate the md5sum of a file
    47	
    48	   @param filename (char *) name of the file of which the md5sum has
    49	   to be generated
    50	
    51	   @return (char *) pointer to a static memory area in whish the
    52	   md5sum ist stored.
    53	*/
    54	char *calculate_md5sum(char *filename) {
    55	  FILE *f;
    56	  // Structure to store the sub-total md5sum
    57	  struct MD5Context context;
    58	  // memory area to store the md5sum
    59	  static unsigned char md5_prs[16];
    60	  // control variable for size of read block
    61	  int i;
    62	  // buffer for reading the file
    63	  char buf[bufsize];
    64	
    65	  // open file
    66	  if ((f = fopen(filename, "r")) == NULL) {
    67	    perror(filename);
    68	    return NULL;
    69	  }
    70	  // init sub-total structure
    71	  MD5Init(&context);
    72	  // read the whole file and calculate the md5sum over it
    73	  while (!feof(f)) {
    74	    i = fread(buf, 1, bufsize, f);
    75	    MD5Update(&context, buf, i);
    76	  }
    77	  fclose(f);
    78	  // build up the md5sum
    79	  MD5Final(md5_prs, &context);
    80	  // return it
    81	  return md5_prs;
    82	}
    83	
    84	
    85	main(int argc, char *argv[]) {
    86	  int i, j;
    87	  unsigned char *s;
    88	  char fn[128];
    89	
    90	  // calculate the md5sum of any file which is given as parameter
    91	  if (argc != 1) {
    92	    for (i = 1; i < argc; i++) {
    93	      if ((s = calculate_md5sum(argv[i])) != NULL) {
    94		for (j = 0; j < 16; j++){
    95		  printf("%02x", s[j]);
    96		}
    97		printf("  %s\n", argv[i]);
    98	      }
    99	    }
   100	    exit(0);
   101	  }
   102	
   103	  // runing normal test
   104	  printf("running %s...\n", argv[0]);
   105	
   106	  for (i = 0; i < 4; i++) {
   107	    sprintf(fn, "md5test%d.dat", i);
   108	    printf("  %s...", fn);
   109	    fflush(stdout);
   110	    s = calculate_md5sum(fn);
   111	    if ((strncmp(s, md5sums[i], 16)) != 0) {
   112	      fail("md5sums are not matching");
   113	      exit(1);
   114	    }
   115	    printf(" OK\n", fn);
   116	    fflush(stdout);
   117	  }
   118	  exit(0);
   119	}
\end{lstlisting}}
