update dev300-m57
[ooovba.git] / sal / workben / t_digest.c
blob7d95ae1f7df2a93622d21bc77e36cf0fac92dd31
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: t_digest.c,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
35 #include <rtl/digest.h>
37 static const char *digest_in_MD[] =
39 "",
40 "a",
41 "abc",
42 "message digest",
43 "abcdefghijklmnopqrstuvwxyz",
44 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
45 "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
46 NULL,
49 static const char *digest_out_MD2[]=
51 "8350e5a3e24c153df2275c9f80692773",
52 "32ec01ec4a6dac72c0ab96fb34c0b5d1",
53 "da853b0d3f88d99b30283a69e6ded6bb",
54 "ab4f496bfb2a530b219ff33031fe06b0",
55 "4e8ddff3650292ab5a4108c3aa47940b",
56 "da33def2a42df13975352846c30338cd",
57 "d5976f79d83d3a0dc9806c3c66f3efd8",
60 static const char *digest_out_MD5[]=
62 "d41d8cd98f00b204e9800998ecf8427e",
63 "0cc175b9c0f1b6a831c399e269772661",
64 "900150983cd24fb0d6963f7d28e17f72",
65 "f96b697d7cb7938d525a2f31aaf161d0",
66 "c3fcd3d76192e4007dfb496cca67e13b",
67 "d174ab98d277d9f5a5611c2c9f419d9f",
68 "57edf4a22be3c955ac49da2e2107b67a",
71 static const char *digest_in_SHA[]=
73 "abc",
74 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
75 NULL,
78 static const char *digest_out_SHA_0[]=
80 "0164b8a914cd2a5e74c4f7ff082c4d97f1edf880",
81 "d2516ee1acfa5baf33dfc1c471e438449ef134c8",
84 static const char *digest_out_SHA_1[]=
86 "a9993e364706816aba3e25717850c26c9cd0d89d",
87 "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
90 static const char *digest_bigout_SHA_0=
91 "3232affa48628a26653b5aaa44541fd90d690603";
93 static const char *digest_bigout_SHA_1=
94 "34aa973cd4c4daa4f61eeb2bdbad27316534016f";
97 static const char digest_key_HMAC_MD5_1[] =
99 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
100 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
101 0x00
103 static const char digest_key_HMAC_MD5_2[] =
105 /* "Jefe" */
106 'J', 'e', 'f', 'e',
107 0x00
109 static const unsigned char digest_key_HMAC_MD5_3[] =
111 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
112 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
113 0x00
115 static const char *digest_key_HMAC_MD5[] =
117 (const char*)&digest_key_HMAC_MD5_1,
118 (const char*)&digest_key_HMAC_MD5_2, /* "Jefe", */
119 (const char*)&digest_key_HMAC_MD5_3,
120 NULL
123 static const unsigned char digest_in_HMAC_MD5_3[] =
125 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
126 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
127 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
128 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
129 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
130 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
131 0xDD, 0xDD,
132 0x00
134 static const char *digest_in_HMAC_MD5[] =
136 "Hi There",
137 "what do ya want for nothing?",
138 (const char*)&digest_in_HMAC_MD5_3,
139 NULL
142 static const char *digest_out_HMAC_MD5[] =
144 "9294727a3638bb1c13f48ef8158bfc9d",
145 "750c783e6ab0b503eaa86e310a5db738",
146 "56be34521d144c88dbb8c733f0e8b3f6",
147 NULL
149 static const char *digest_out_HMAC_SHA1[] =
151 /* unofficial, i.e. not verified */
152 "675b0b3a1b4ddf4e124872da6c2f632bfed957e9",
153 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
154 "d730594d167e35d5956fd8003d0db3d3f46dc7bb",
155 NULL
159 static char *pt (unsigned char *md, int length)
161 int i;
162 static char buf[80];
164 for (i=0; i<length; i++)
165 sprintf(&(buf[i*2]),"%02x",md[i]);
167 return(buf);
170 int SAL_CALL main (int argc, char **argv)
172 const char **P,**R, **Q;
173 char *p;
174 int i=1, err=0;
176 unsigned char md[80];
177 unsigned char buffer[1000];
179 rtlDigest Digest;
181 P=digest_in_MD;
182 R=digest_out_MD2;
183 i = 1;
184 while (*P)
186 rtl_digest_MD2 (*P, strlen(*P), md, sizeof(md));
188 p=pt (md, RTL_DIGEST_LENGTH_MD2);
189 if (strcmp (p, *R))
191 printf("error calculating MD2 on '%s'\n",*P);
192 printf("got %s instead of %s\n",p,*R);
193 err++;
195 else
196 printf("test (MD2) %d ok\n",i);
197 i++;
198 R++;
199 P++;
202 P=digest_in_MD;
203 R=digest_out_MD5;
204 i=1;
205 while (*P)
207 rtl_digest_MD5 (*P, strlen(*P), md, sizeof(md));
209 p=pt (md, RTL_DIGEST_LENGTH_MD5);
210 if (strcmp (p, *R))
212 printf("error calculating MD5 on '%s'\n",*P);
213 printf("got %s instead of %s\n",p,*R);
214 err++;
216 else
217 printf("test (MD5) %d ok\n",i);
218 i++;
219 R++;
220 P++;
223 P=digest_in_SHA;
224 R=digest_out_SHA_0;
225 i=1;
226 while (*P)
228 rtl_digest_SHA (*P, strlen(*P), md, sizeof(md));
230 p=pt (md, RTL_DIGEST_LENGTH_SHA);
231 if (strcmp (p, *R))
233 printf("error calculating SHA-0 on '%s'\n",*P);
234 printf("got %s instead of %s\n",p,*R);
235 err++;
237 else
238 printf("test (SHA-0) %d ok\n",i);
239 i++;
240 R++;
241 P++;
244 memset (buffer, 'a', sizeof(buffer));
245 R = &digest_bigout_SHA_0;
247 Digest = rtl_digest_createSHA();
248 for (i=0; i<1000; i++)
249 rtl_digest_updateSHA (Digest, buffer, sizeof(buffer));
251 rtl_digest_getSHA (Digest, md, sizeof(md));
252 rtl_digest_destroySHA (Digest);
254 p=pt (md, RTL_DIGEST_LENGTH_SHA);
255 if (strcmp (p, *R))
257 printf("error calculating SHA-0 on '%s'\n",p);
258 printf("got %s instead of %s\n",p,*R);
259 err++;
261 else
262 printf("test (SHA-0) n ok\n");
264 P=digest_in_SHA;
265 R=digest_out_SHA_1;
266 i=1;
267 while (*P)
269 rtl_digest_SHA1 (*P, strlen(*P), md, sizeof(md));
271 p=pt (md, RTL_DIGEST_LENGTH_SHA1);
272 if (strcmp (p, *R))
274 printf("error calculating SHA-1 on '%s'\n",*P);
275 printf("got %s instead of %s\n",p,*R);
276 err++;
278 else
279 printf("test (SHA-1) %d ok\n",i);
280 i++;
281 R++;
282 P++;
285 memset (buffer, 'a', sizeof(buffer));
286 R = &digest_bigout_SHA_1;
288 Digest = rtl_digest_createSHA1();
289 for (i=0; i<1000; i++)
290 rtl_digest_updateSHA1 (Digest, buffer, sizeof(buffer));
292 rtl_digest_getSHA1 (Digest, md, sizeof(md));
293 rtl_digest_destroySHA1 (Digest);
295 p=pt (md, RTL_DIGEST_LENGTH_SHA1);
296 if (strcmp (p, *R))
298 printf("error calculating SHA-1 on '%s'\n",p);
299 printf("got %s instead of %s\n",p,*R);
300 err++;
302 else
303 printf("test (SHA-1) n ok\n");
306 P=digest_in_HMAC_MD5;
307 Q=digest_key_HMAC_MD5;
308 R=digest_out_HMAC_MD5;
309 Digest = rtl_digest_createHMAC_MD5();
310 i = 1;
311 while (*P)
313 rtl_digest_initHMAC_MD5 (Digest, *Q, strlen(*Q));
314 rtl_digest_updateHMAC_MD5 (Digest, *P, strlen(*P));
315 rtl_digest_getHMAC_MD5 (Digest, md, sizeof(md));
317 p=pt (md, RTL_DIGEST_LENGTH_HMAC_MD5);
318 if (strcmp (p, *R))
320 printf("error calculating HMAC-MD5 on '%s'\n",*P);
321 printf("got %s instead of %s\n",p,*R);
322 err++;
324 else
325 printf("test (HMAC-MD5) %d ok\n",i);
326 i++;
327 R++;
328 P++;
329 Q++;
331 rtl_digest_destroyHMAC_MD5 (Digest);
334 P=digest_in_HMAC_MD5;
335 Q=digest_key_HMAC_MD5;
336 R=digest_out_HMAC_SHA1;
337 Digest = rtl_digest_createHMAC_SHA1();
338 i = 1;
339 while (*P)
341 rtl_digest_initHMAC_SHA1 (Digest, *Q, strlen(*Q));
342 rtl_digest_updateHMAC_SHA1 (Digest, *P, strlen(*P));
343 rtl_digest_getHMAC_SHA1 (Digest, md, sizeof(md));
345 p=pt (md, RTL_DIGEST_LENGTH_HMAC_SHA1);
346 if (strcmp (p, *R))
348 printf("error calculating HMAC-SHA-1 on '%s'\n",*P);
349 printf("got %s instead of %s\n",p,*R);
350 err++;
352 else
353 printf("test (HMAC-SHA-1) %d ok\n",i);
354 i++;
355 P++;
356 Q++;
357 R++;
359 rtl_digest_destroyHMAC_SHA1 (Digest);
362 P=digest_in_HMAC_MD5;
363 Q=digest_key_HMAC_MD5;
364 rtl_digest_PBKDF2 (
365 md, RTL_DIGEST_LENGTH_MD5, /* [out] derived key */
366 Q[1], strlen(Q[1]), /* [in] password */
367 P[1], strlen(P[1]), /* [in] salt */
368 1000); /* [in] iteration count */
370 p=pt (md, RTL_DIGEST_LENGTH_MD5);
371 if (strcmp (p, "6349e09cb6b8c1485cfa9780ee3264df"))
373 printf("error calculating PBKDF2 on '%s'\n", P[1]);
374 err++;
376 else
377 printf("test (PBKDF2) %d ok\n", 1);
379 return (err);