home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 February / CMCD0205.ISO / Linux / gimp-2.2.0.tar.gz / gimp-2.2.0.tar / gimp-2.2.0 / libgimpmath / test-md5.c < prev   
C/C++ Source or Header  |  2004-07-22  |  2KB  |  67 lines

  1. /* A small test program to check our MD5 implementation.
  2.  * The test program computes the MD5 digest of some strings
  3.  * as given in section A.5 of RFC 1321, reproduced below.
  4.  */
  5.  
  6. #include "config.h"
  7.  
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. #include <glib.h>
  12.  
  13. #include "gimpmd5.h"
  14.  
  15.  
  16. static const gchar * test[7][2] =
  17. {
  18.   { "", "d41d8cd98f00b204e9800998ecf8427e" },
  19.   { "a", "0cc175b9c0f1b6a831c399e269772661" },
  20.   { "abc", "900150983cd24fb0d6963f7d28e17f72" },
  21.   { "message digest", "f96b697d7cb7938d525a2f31aaf161d0" },
  22.   { "abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b" },
  23.   { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "d174ab98d277d9f5a5611c2c9f419d9f" },
  24.   { "12345678901234567890123456789012345678901234567890123456789012345678901234567890", "57edf4a22be3c955ac49da2e2107b67a" }
  25. };
  26.  
  27. int
  28. main (void)
  29. {
  30.   gboolean correct = TRUE;
  31.   guchar   digest[16];
  32.   gint     i, j;
  33.  
  34.   g_print ("Testing the GIMP MD5 implementation ...\n\n");
  35.  
  36.   for (i = 0; i < 7; ++i)
  37.     {
  38.       gimp_md5_get_digest (test[i][0], strlen (test[i][0]), digest);
  39.  
  40.       g_print ("MD5 (\"%s\") = ", test[i][0]);
  41.  
  42.       for (j = 0; j < 16; j++)
  43.         {
  44.           guchar buf[4];
  45.  
  46.           g_snprintf (buf, 3, "%02x", digest[j]);
  47.           g_print (buf);
  48.  
  49.           if (strncmp (buf, test[i][1] + j*2, 2))
  50.             correct = FALSE;
  51.         }
  52.       g_print ("\n");
  53.  
  54.       if (!correct)
  55.         {
  56.           g_print ("\nWRONG digest!! "
  57.                    "Please report to http://bugzilla.gnome.org/\n");
  58.           return EXIT_FAILURE;
  59.         }
  60.     }
  61.  
  62.   g_print ("\nLooks good.\n\n");
  63.  
  64.   return EXIT_SUCCESS;
  65. }
  66.  
  67.