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 #ifdef HAVE_CONFIG_H
00021 # include <config.h>
00022 #endif
00023 
00024 #include <stdio.h>
00025 #include <string.h>
00026 #include "gc.h"
00027 
00028 int
00029 main (int argc, char *argv[])
00030 {
00031   Gc_rc rc;
00032   gc_hash_handle h;
00033 
00034   rc = gc_init ();
00035   if (rc != GC_OK)
00036     {
00037       printf ("gc_init() failed\n");
00038       return 1;
00039     }
00040 
00041   /* Test vectors from RFC 1321. */
00042 
00043   {
00044     char *in = "abcdefghijklmnopqrstuvwxyz";
00045     size_t inlen = strlen (in);
00046     char *expect =
00047       "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1\x3b";
00048     char out[16];
00049     const char *p;
00050 
00051     /* MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b */
00052 
00053     if (gc_md5 (in, inlen, out) != 0)
00054       {
00055         printf ("gc_md5 call failed\n");
00056         return 1;
00057       }
00058 
00059     if (memcmp (out, expect, 16) != 0)
00060       {
00061         size_t i;
00062         printf ("md5 1 mismatch. expected:\n");
00063         for (i = 0; i < 16; i++)
00064           printf ("%02x ", expect[i] & 0xFF);
00065         printf ("\ncomputed:\n");
00066         for (i = 0; i < 16; i++)
00067           printf ("%02x ", out[i] & 0xFF);
00068         printf ("\n");
00069         return 1;
00070       }
00071 
00072     if (gc_hash_buffer (GC_MD5, in, inlen, out) != 0)
00073       {
00074         printf ("gc_hash_buffer(MD5) call failed\n");
00075         return 1;
00076       }
00077 
00078     if (memcmp (out, expect, 16) != 0)
00079       {
00080         size_t i;
00081         printf ("md5 2 mismatch. expected:\n");
00082         for (i = 0; i < 16; i++)
00083           printf ("%02x ", expect[i] & 0xFF);
00084         printf ("\ncomputed:\n");
00085         for (i = 0; i < 16; i++)
00086           printf ("%02x ", out[i] & 0xFF);
00087         printf ("\n");
00088         return 1;
00089       }
00090 
00091     if (gc_hash_digest_length (GC_MD5) != 16)
00092       {
00093         printf ("gc_hash_digest_length (GC_MD5) failed\n");
00094         return 1;
00095       }
00096 
00097     if ((rc = gc_hash_open (GC_MD5, 0, &h)) != GC_OK)
00098       {
00099         printf ("gc_hash_open(GC_MD5) failed (%d)\n", rc);
00100         return 1;
00101       }
00102 
00103     gc_hash_write (h, inlen, in);
00104 
00105     p = gc_hash_read (h);
00106 
00107     if (!p)
00108       {
00109         printf ("gc_hash_read failed\n");
00110         return 1;
00111       }
00112 
00113     if (memcmp (p, expect, 16) != 0)
00114         {
00115         size_t i;
00116         printf ("md5 3 mismatch. expected:\n");
00117         for (i = 0; i < 16; i++)
00118           printf ("%02x ", expect[i] & 0xFF);
00119         printf ("\ncomputed:\n");
00120         for (i = 0; i < 16; i++)
00121           printf ("%02x ", p[i] & 0xFF);
00122         printf ("\n");
00123         return 1;
00124       }
00125 
00126     gc_hash_close (h);
00127   }
00128 
00129   gc_done ();
00130 
00131   return 0;
00132 }

Generated on Tue Aug 22 12:06:07 2006 for gsasl by  doxygen 1.4.7