Branch data Line data Source code
1 : : /*
2 : : * Copyright (C) 2005, 2010-2012 Free Software Foundation, Inc.
3 : : * Written by Simon Josefsson
4 : : *
5 : : * This program is free software; you can redistribute it and/or modify
6 : : * it under the terms of the GNU General Public License as published by
7 : : * the Free Software Foundation; either version 3, or (at your option)
8 : : * any later version.
9 : : *
10 : : * This program is distributed in the hope that it will be useful,
11 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 : : * GNU General Public License for more details.
14 : : *
15 : : * You should have received a copy of the GNU General Public License
16 : : * along with this program; if not, see <http://www.gnu.org/licenses/>. */
17 : :
18 : : #include <config.h>
19 : :
20 : : #include <stdio.h>
21 : : #include <string.h>
22 : : #include "gc.h"
23 : :
24 : : int
25 : 1 : main (int argc, char *argv[])
26 : : {
27 : : Gc_rc rc;
28 : : gc_hash_handle h;
29 : :
30 : 1 : rc = gc_init ();
31 [ - + ]: 1 : if (rc != GC_OK)
32 : : {
33 : 0 : printf ("gc_init() failed\n");
34 : 0 : return 1;
35 : : }
36 : :
37 : : /* Test vectors from RFC 1321. */
38 : :
39 : : {
40 : 1 : char *in = "abcdefghijklmnopqrstuvwxyz";
41 : 1 : size_t inlen = strlen (in);
42 : 1 : char *expect =
43 : : "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1\x3b";
44 : : char out[16];
45 : : const char *p;
46 : :
47 : : /* MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b */
48 : :
49 [ - + ]: 1 : if (gc_md5 (in, inlen, out) != 0)
50 : : {
51 : 0 : printf ("gc_md5 call failed\n");
52 : 0 : return 1;
53 : : }
54 : :
55 [ - + ]: 1 : if (memcmp (out, expect, 16) != 0)
56 : : {
57 : : size_t i;
58 : 0 : printf ("md5 1 mismatch. expected:\n");
59 [ # # ]: 0 : for (i = 0; i < 16; i++)
60 : 0 : printf ("%02x ", expect[i] & 0xFF);
61 : 0 : printf ("\ncomputed:\n");
62 [ # # ]: 0 : for (i = 0; i < 16; i++)
63 : 0 : printf ("%02x ", out[i] & 0xFF);
64 : 0 : printf ("\n");
65 : 0 : return 1;
66 : : }
67 : :
68 [ - + ]: 1 : if (gc_hash_buffer (GC_MD5, in, inlen, out) != 0)
69 : : {
70 : 0 : printf ("gc_hash_buffer(MD5) call failed\n");
71 : 0 : return 1;
72 : : }
73 : :
74 [ - + ]: 1 : if (memcmp (out, expect, 16) != 0)
75 : : {
76 : : size_t i;
77 : 0 : printf ("md5 2 mismatch. expected:\n");
78 [ # # ]: 0 : for (i = 0; i < 16; i++)
79 : 0 : printf ("%02x ", expect[i] & 0xFF);
80 : 0 : printf ("\ncomputed:\n");
81 [ # # ]: 0 : for (i = 0; i < 16; i++)
82 : 0 : printf ("%02x ", out[i] & 0xFF);
83 : 0 : printf ("\n");
84 : 0 : return 1;
85 : : }
86 : :
87 [ - + ]: 1 : if (gc_hash_digest_length (GC_MD5) != 16)
88 : : {
89 : 0 : printf ("gc_hash_digest_length (GC_MD5) failed\n");
90 : 0 : return 1;
91 : : }
92 : :
93 [ - + ]: 1 : if ((rc = gc_hash_open (GC_MD5, 0, &h)) != GC_OK)
94 : : {
95 : 0 : printf ("gc_hash_open(GC_MD5) failed (%d)\n", rc);
96 : 0 : return 1;
97 : : }
98 : :
99 : 1 : gc_hash_write (h, inlen, in);
100 : :
101 : 1 : p = gc_hash_read (h);
102 : :
103 [ - + ]: 1 : if (!p)
104 : : {
105 : 0 : printf ("gc_hash_read failed\n");
106 : 0 : return 1;
107 : : }
108 : :
109 [ - + ]: 1 : if (memcmp (p, expect, 16) != 0)
110 : : {
111 : : size_t i;
112 : 0 : printf ("md5 3 mismatch. expected:\n");
113 [ # # ]: 0 : for (i = 0; i < 16; i++)
114 : 0 : printf ("%02x ", expect[i] & 0xFF);
115 : 0 : printf ("\ncomputed:\n");
116 [ # # ]: 0 : for (i = 0; i < 16; i++)
117 : 0 : printf ("%02x ", p[i] & 0xFF);
118 : 0 : printf ("\n");
119 : 0 : return 1;
120 : : }
121 : :
122 : 1 : gc_hash_close (h);
123 : : }
124 : :
125 : 1 : gc_done ();
126 : :
127 : 1 : return 0;
128 : : }
|