OpenSSL version

#include <stdio.h>
#include <openssl/md5.h>
#define BYTES_PER_READ 64

int dice(char* filename)
{
   MD5_CTX the_md5;
   FILE *the_file;
   int j;
   int done = 0;
   int bytes_read;
   char line_read[BYTES_PER_READ];
   unsigned char md5_dump[17];

   printf("%s : ", filename);

   the_file = fopen(filename, "rb");

   if (the_file == NULL)
   {  printf("(unopenable/missing)\n");
      return 1;
   }

   MD5_Init(&the_md5);

   while (!done)
   {  bytes_read = fread(line_read, 1, BYTES_PER_READ, the_file);
      MD5_Update(&the_md5, line_read, bytes_read);

      if (bytes_read < BYTES_PER_READ)
      {  done = 1;
   }  }

   MD5_Final(md5_dump, &the_md5);

   for (j = 0; j < 16; j++)
   {  printf("%02x", md5_dump[j]);
   }

   printf("\n");
   fclose(the_file);
   return 0;
}

int main(int argc, char** argv)
{
   int i;

   if (argc < 2)
   {  printf("usage: dicer [files to dice]\n");
      return 1;
   }

   else
   {  for (i = 1; i < argc; i++)
      {   dice(argv[i]);
}  }  }


Version that works with L. Peter Deutch's code

Virtually the same...

int dice(char* filename)
{
   FILE *the_file;

   md5_state_t state;
   md5_byte_t digest[16];

   int j;
   int done = 0;
   int bytes_read;
   char line_read[BYTES_PER_READ];
   unsigned char md5_dump[17];

   printf("%s : ", filename);

   the_file = fopen(filename, "rb");

   if (the_file == NULL)
   {  printf("(unopenable/missing)\n");
      return 1;
   }
   md5_init(&state);

   while (!done)
   {  bytes_read = fread(line_read, 1, BYTES_PER_READ, the_file);
      md5_append(&state, (const md5_byte_t *)line_read, bytes_read);

      if (bytes_read < BYTES_PER_READ)
      {  done = 1;
   }  }

   md5_finish(&state, digest);

   for (j = 0; j < 16; j++)
   {  printf("%02x", digest[j]);
   }

   printf("\n");
   fclose(the_file);
   return 0;
}

-- MattWalsh - 07 Dec 2005

Topic revision: r2 - 29 Jan 2006 - MattWalsh
 
This site is powered by the TWiki collaboration platformCopyright © 2008-2012 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback