test-gc-md5.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2005 Free Software Foundation
00003  * Written by Simon Josefsson
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2, or (at your option)
00008  * any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00018  * 02110-1301, USA.  */
00019 
00020 #include <config.h>
00021 
00022 #include <stdio.h>
00023 #include <string.h>
00024 #include "gc.h"
00025 
00026 int
00027 main (int argc, char *argv[])
00028 {
00029   Gc_rc rc;
00030   gc_hash_handle h;
00031 
00032   rc = gc_init ();
00033   if (rc != GC_OK)
00034     {
00035       printf ("gc_init() failed\n");
00036       return 1;
00037     }
00038 
00039   /* Test vectors from RFC 1321. */
00040 
00041   {
00042     char *in = "abcdefghijklmnopqrstuvwxyz";
00043     size_t inlen = strlen (in);
00044     char *expect =
00045       "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1\x3b";
00046     char out[16];
00047     const char *p;
00048 
00049     /* MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b */
00050 
00051     if (gc_md5 (in, inlen, out) != 0)
00052       {
00053         printf ("gc_md5 call failed\n");
00054         return 1;
00055       }
00056 
00057     if (memcmp (out, expect, 16) != 0)
00058       {
00059         size_t i;
00060         printf ("md5 1 mismatch. expected:\n");
00061         for (i = 0; i < 16; i++)
00062           printf ("%02x ", expect[i] & 0xFF);
00063         printf ("\ncomputed:\n");
00064         for (i = 0; i < 16; i++)
00065           printf ("%02x ", out[i] & 0xFF);
00066         printf ("\n");
00067         return 1;
00068       }
00069 
00070     if (gc_hash_buffer (GC_MD5, in, inlen, out) != 0)
00071       {
00072         printf ("gc_hash_buffer(MD5) call failed\n");
00073         return 1;
00074       }
00075 
00076     if (memcmp (out, expect, 16) != 0)
00077       {
00078         size_t i;
00079         printf ("md5 2 mismatch. expected:\n");
00080         for (i = 0; i < 16; i++)
00081           printf ("%02x ", expect[i] & 0xFF);
00082         printf ("\ncomputed:\n");
00083         for (i = 0; i < 16; i++)
00084           printf ("%02x ", out[i] & 0xFF);
00085         printf ("\n");
00086         return 1;
00087       }
00088 
00089     if (gc_hash_digest_length (GC_MD5) != 16)
00090       {
00091         printf ("gc_hash_digest_length (GC_MD5) failed\n");
00092         return 1;
00093       }
00094 
00095     if ((rc = gc_hash_open (GC_MD5, 0, &h)) != GC_OK)
00096       {
00097         printf ("gc_hash_open(GC_MD5) failed (%d)\n", rc);
00098         return 1;
00099       }
00100 
00101     gc_hash_write (h, inlen, in);
00102 
00103     p = gc_hash_read (h);
00104 
00105     if (!p)
00106       {
00107         printf ("gc_hash_read failed\n");
00108         return 1;
00109       }
00110 
00111     if (memcmp (p, expect, 16) != 0)
00112         {
00113         size_t i;
00114         printf ("md5 3 mismatch. expected:\n");
00115         for (i = 0; i < 16; i++)
00116           printf ("%02x ", expect[i] & 0xFF);
00117         printf ("\ncomputed:\n");
00118         for (i = 0; i < 16; i++)
00119           printf ("%02x ", p[i] & 0xFF);
00120         printf ("\n");
00121         return 1;
00122       }
00123 
00124     gc_hash_close (h);
00125   }
00126 
00127   gc_done ();
00128 
00129   return 0;
00130 }

Generated on Tue Oct 21 18:28:19 2008 for gsasl by  doxygen 1.5.6