merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / workben / t_digest.c
blobab6acf11572b5b18855e24befd9ba1936810473d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
32 #include <rtl/digest.h>
34 static const char *digest_in_MD[] =
36 "",
37 "a",
38 "abc",
39 "message digest",
40 "abcdefghijklmnopqrstuvwxyz",
41 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
42 "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
43 NULL,
46 static const char *digest_out_MD2[]=
48 "8350e5a3e24c153df2275c9f80692773",
49 "32ec01ec4a6dac72c0ab96fb34c0b5d1",
50 "da853b0d3f88d99b30283a69e6ded6bb",
51 "ab4f496bfb2a530b219ff33031fe06b0",
52 "4e8ddff3650292ab5a4108c3aa47940b",
53 "da33def2a42df13975352846c30338cd",
54 "d5976f79d83d3a0dc9806c3c66f3efd8",
57 static const char *digest_out_MD5[]=
59 "d41d8cd98f00b204e9800998ecf8427e",
60 "0cc175b9c0f1b6a831c399e269772661",
61 "900150983cd24fb0d6963f7d28e17f72",
62 "f96b697d7cb7938d525a2f31aaf161d0",
63 "c3fcd3d76192e4007dfb496cca67e13b",
64 "d174ab98d277d9f5a5611c2c9f419d9f",
65 "57edf4a22be3c955ac49da2e2107b67a",
68 static const char *digest_in_SHA[]=
70 "abc",
71 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
72 NULL,
75 static const char *digest_out_SHA_0[]=
77 "0164b8a914cd2a5e74c4f7ff082c4d97f1edf880",
78 "d2516ee1acfa5baf33dfc1c471e438449ef134c8",
81 static const char *digest_out_SHA_1[]=
83 "a9993e364706816aba3e25717850c26c9cd0d89d",
84 "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
87 static const char *digest_bigout_SHA_0=
88 "3232affa48628a26653b5aaa44541fd90d690603";
90 static const char *digest_bigout_SHA_1=
91 "34aa973cd4c4daa4f61eeb2bdbad27316534016f";
94 static const char digest_key_HMAC_MD5_1[] =
96 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
97 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
98 0x00
100 static const char digest_key_HMAC_MD5_2[] =
102 /* "Jefe" */
103 'J', 'e', 'f', 'e',
104 0x00
106 static const unsigned char digest_key_HMAC_MD5_3[] =
108 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
109 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
110 0x00
112 static const char *digest_key_HMAC_MD5[] =
114 (const char*)&digest_key_HMAC_MD5_1,
115 (const char*)&digest_key_HMAC_MD5_2, /* "Jefe", */
116 (const char*)&digest_key_HMAC_MD5_3,
117 NULL
120 static const unsigned char digest_in_HMAC_MD5_3[] =
122 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
123 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
124 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
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,
129 0x00
131 static const char *digest_in_HMAC_MD5[] =
133 "Hi There",
134 "what do ya want for nothing?",
135 (const char*)&digest_in_HMAC_MD5_3,
136 NULL
139 static const char *digest_out_HMAC_MD5[] =
141 "9294727a3638bb1c13f48ef8158bfc9d",
142 "750c783e6ab0b503eaa86e310a5db738",
143 "56be34521d144c88dbb8c733f0e8b3f6",
144 NULL
146 static const char *digest_out_HMAC_SHA1[] =
148 /* unofficial, i.e. not verified */
149 "675b0b3a1b4ddf4e124872da6c2f632bfed957e9",
150 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
151 "d730594d167e35d5956fd8003d0db3d3f46dc7bb",
152 NULL
156 static char *pt (unsigned char *md, int length)
158 int i;
159 static char buf[80];
161 for (i=0; i<length; i++)
162 sprintf(&(buf[i*2]),"%02x",md[i]);
164 return(buf);
167 int SAL_CALL main (int argc, char **argv)
169 const char **P,**R, **Q;
170 char *p;
171 int i=1, err=0;
173 unsigned char md[80];
174 unsigned char buffer[1000];
176 rtlDigest Digest;
178 P=digest_in_MD;
179 R=digest_out_MD2;
180 i = 1;
181 while (*P)
183 rtl_digest_MD2 (*P, strlen(*P), md, sizeof(md));
185 p=pt (md, RTL_DIGEST_LENGTH_MD2);
186 if (strcmp (p, *R))
188 printf("error calculating MD2 on '%s'\n",*P);
189 printf("got %s instead of %s\n",p,*R);
190 err++;
192 else
193 printf("test (MD2) %d ok\n",i);
194 i++;
195 R++;
196 P++;
199 P=digest_in_MD;
200 R=digest_out_MD5;
201 i=1;
202 while (*P)
204 rtl_digest_MD5 (*P, strlen(*P), md, sizeof(md));
206 p=pt (md, RTL_DIGEST_LENGTH_MD5);
207 if (strcmp (p, *R))
209 printf("error calculating MD5 on '%s'\n",*P);
210 printf("got %s instead of %s\n",p,*R);
211 err++;
213 else
214 printf("test (MD5) %d ok\n",i);
215 i++;
216 R++;
217 P++;
220 P=digest_in_SHA;
221 R=digest_out_SHA_0;
222 i=1;
223 while (*P)
225 rtl_digest_SHA (*P, strlen(*P), md, sizeof(md));
227 p=pt (md, RTL_DIGEST_LENGTH_SHA);
228 if (strcmp (p, *R))
230 printf("error calculating SHA-0 on '%s'\n",*P);
231 printf("got %s instead of %s\n",p,*R);
232 err++;
234 else
235 printf("test (SHA-0) %d ok\n",i);
236 i++;
237 R++;
238 P++;
241 memset (buffer, 'a', sizeof(buffer));
242 R = &digest_bigout_SHA_0;
244 Digest = rtl_digest_createSHA();
245 for (i=0; i<1000; i++)
246 rtl_digest_updateSHA (Digest, buffer, sizeof(buffer));
248 rtl_digest_getSHA (Digest, md, sizeof(md));
249 rtl_digest_destroySHA (Digest);
251 p=pt (md, RTL_DIGEST_LENGTH_SHA);
252 if (strcmp (p, *R))
254 printf("error calculating SHA-0 on '%s'\n",p);
255 printf("got %s instead of %s\n",p,*R);
256 err++;
258 else
259 printf("test (SHA-0) n ok\n");
261 P=digest_in_SHA;
262 R=digest_out_SHA_1;
263 i=1;
264 while (*P)
266 rtl_digest_SHA1 (*P, strlen(*P), md, sizeof(md));
268 p=pt (md, RTL_DIGEST_LENGTH_SHA1);
269 if (strcmp (p, *R))
271 printf("error calculating SHA-1 on '%s'\n",*P);
272 printf("got %s instead of %s\n",p,*R);
273 err++;
275 else
276 printf("test (SHA-1) %d ok\n",i);
277 i++;
278 R++;
279 P++;
282 memset (buffer, 'a', sizeof(buffer));
283 R = &digest_bigout_SHA_1;
285 Digest = rtl_digest_createSHA1();
286 for (i=0; i<1000; i++)
287 rtl_digest_updateSHA1 (Digest, buffer, sizeof(buffer));
289 rtl_digest_getSHA1 (Digest, md, sizeof(md));
290 rtl_digest_destroySHA1 (Digest);
292 p=pt (md, RTL_DIGEST_LENGTH_SHA1);
293 if (strcmp (p, *R))
295 printf("error calculating SHA-1 on '%s'\n",p);
296 printf("got %s instead of %s\n",p,*R);
297 err++;
299 else
300 printf("test (SHA-1) n ok\n");
303 P=digest_in_HMAC_MD5;
304 Q=digest_key_HMAC_MD5;
305 R=digest_out_HMAC_MD5;
306 Digest = rtl_digest_createHMAC_MD5();
307 i = 1;
308 while (*P)
310 rtl_digest_initHMAC_MD5 (Digest, *Q, strlen(*Q));
311 rtl_digest_updateHMAC_MD5 (Digest, *P, strlen(*P));
312 rtl_digest_getHMAC_MD5 (Digest, md, sizeof(md));
314 p=pt (md, RTL_DIGEST_LENGTH_HMAC_MD5);
315 if (strcmp (p, *R))
317 printf("error calculating HMAC-MD5 on '%s'\n",*P);
318 printf("got %s instead of %s\n",p,*R);
319 err++;
321 else
322 printf("test (HMAC-MD5) %d ok\n",i);
323 i++;
324 R++;
325 P++;
326 Q++;
328 rtl_digest_destroyHMAC_MD5 (Digest);
331 P=digest_in_HMAC_MD5;
332 Q=digest_key_HMAC_MD5;
333 R=digest_out_HMAC_SHA1;
334 Digest = rtl_digest_createHMAC_SHA1();
335 i = 1;
336 while (*P)
338 rtl_digest_initHMAC_SHA1 (Digest, *Q, strlen(*Q));
339 rtl_digest_updateHMAC_SHA1 (Digest, *P, strlen(*P));
340 rtl_digest_getHMAC_SHA1 (Digest, md, sizeof(md));
342 p=pt (md, RTL_DIGEST_LENGTH_HMAC_SHA1);
343 if (strcmp (p, *R))
345 printf("error calculating HMAC-SHA-1 on '%s'\n",*P);
346 printf("got %s instead of %s\n",p,*R);
347 err++;
349 else
350 printf("test (HMAC-SHA-1) %d ok\n",i);
351 i++;
352 P++;
353 Q++;
354 R++;
356 rtl_digest_destroyHMAC_SHA1 (Digest);
359 P=digest_in_HMAC_MD5;
360 Q=digest_key_HMAC_MD5;
361 rtl_digest_PBKDF2 (
362 md, RTL_DIGEST_LENGTH_MD5, /* [out] derived key */
363 Q[1], strlen(Q[1]), /* [in] password */
364 P[1], strlen(P[1]), /* [in] salt */
365 1000); /* [in] iteration count */
367 p=pt (md, RTL_DIGEST_LENGTH_MD5);
368 if (strcmp (p, "6349e09cb6b8c1485cfa9780ee3264df"))
370 printf("error calculating PBKDF2 on '%s'\n", P[1]);
371 err++;
373 else
374 printf("test (PBKDF2) %d ok\n", 1);
376 return (err);