Bump for 3.6-28
[LibreOffice.git] / sal / workben / t_digest.c
blob7d947ba253e7f5ce03cdeca69fc871d37df763cd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
33 #include <rtl/digest.h>
35 static const char *digest_in_MD[] =
37 "",
38 "a",
39 "abc",
40 "message digest",
41 "abcdefghijklmnopqrstuvwxyz",
42 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
43 "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
44 NULL,
47 static const char *digest_out_MD2[]=
49 "8350e5a3e24c153df2275c9f80692773",
50 "32ec01ec4a6dac72c0ab96fb34c0b5d1",
51 "da853b0d3f88d99b30283a69e6ded6bb",
52 "ab4f496bfb2a530b219ff33031fe06b0",
53 "4e8ddff3650292ab5a4108c3aa47940b",
54 "da33def2a42df13975352846c30338cd",
55 "d5976f79d83d3a0dc9806c3c66f3efd8",
58 static const char *digest_out_MD5[]=
60 "d41d8cd98f00b204e9800998ecf8427e",
61 "0cc175b9c0f1b6a831c399e269772661",
62 "900150983cd24fb0d6963f7d28e17f72",
63 "f96b697d7cb7938d525a2f31aaf161d0",
64 "c3fcd3d76192e4007dfb496cca67e13b",
65 "d174ab98d277d9f5a5611c2c9f419d9f",
66 "57edf4a22be3c955ac49da2e2107b67a",
69 static const char *digest_in_SHA[]=
71 "abc",
72 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
73 NULL,
76 static const char *digest_out_SHA_0[]=
78 "0164b8a914cd2a5e74c4f7ff082c4d97f1edf880",
79 "d2516ee1acfa5baf33dfc1c471e438449ef134c8",
82 static const char *digest_out_SHA_1[]=
84 "a9993e364706816aba3e25717850c26c9cd0d89d",
85 "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
88 static const char *digest_bigout_SHA_0=
89 "3232affa48628a26653b5aaa44541fd90d690603";
91 static const char *digest_bigout_SHA_1=
92 "34aa973cd4c4daa4f61eeb2bdbad27316534016f";
95 static const char digest_key_HMAC_MD5_1[] =
97 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
98 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
99 0x00
101 static const char digest_key_HMAC_MD5_2[] =
103 /* "Jefe" */
104 'J', 'e', 'f', 'e',
105 0x00
107 static const unsigned char digest_key_HMAC_MD5_3[] =
109 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
110 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
111 0x00
113 static const char *digest_key_HMAC_MD5[] =
115 (const char*)&digest_key_HMAC_MD5_1,
116 (const char*)&digest_key_HMAC_MD5_2, /* "Jefe", */
117 (const char*)&digest_key_HMAC_MD5_3,
118 NULL
121 static const unsigned char digest_in_HMAC_MD5_3[] =
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, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
129 0xDD, 0xDD,
130 0x00
132 static const char *digest_in_HMAC_MD5[] =
134 "Hi There",
135 "what do ya want for nothing?",
136 (const char*)&digest_in_HMAC_MD5_3,
137 NULL
140 static const char *digest_out_HMAC_MD5[] =
142 "9294727a3638bb1c13f48ef8158bfc9d",
143 "750c783e6ab0b503eaa86e310a5db738",
144 "56be34521d144c88dbb8c733f0e8b3f6",
145 NULL
147 static const char *digest_out_HMAC_SHA1[] =
149 /* unofficial, i.e. not verified */
150 "675b0b3a1b4ddf4e124872da6c2f632bfed957e9",
151 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
152 "d730594d167e35d5956fd8003d0db3d3f46dc7bb",
153 NULL
157 static char *pt (unsigned char *md, int length)
159 int i;
160 static char buf[80];
162 for (i=0; i<length; i++)
163 sprintf(&(buf[i*2]),"%02x",md[i]);
165 return(buf);
168 int SAL_CALL main (void)
170 const char **P,**R, **Q;
171 char *p;
172 int i=1, err=0;
174 unsigned char md[80];
175 unsigned char buffer[1000];
177 rtlDigest Digest;
179 P=digest_in_MD;
180 R=digest_out_MD2;
181 i = 1;
182 while (*P)
184 rtl_digest_MD2 (*P, strlen(*P), md, sizeof(md));
186 p=pt (md, RTL_DIGEST_LENGTH_MD2);
187 if (strcmp (p, *R))
189 printf("error calculating MD2 on '%s'\n",*P);
190 printf("got %s instead of %s\n",p,*R);
191 err++;
193 else
194 printf("test (MD2) %d ok\n",i);
195 i++;
196 R++;
197 P++;
200 P=digest_in_MD;
201 R=digest_out_MD5;
202 i=1;
203 while (*P)
205 rtl_digest_MD5 (*P, strlen(*P), md, sizeof(md));
207 p=pt (md, RTL_DIGEST_LENGTH_MD5);
208 if (strcmp (p, *R))
210 printf("error calculating MD5 on '%s'\n",*P);
211 printf("got %s instead of %s\n",p,*R);
212 err++;
214 else
215 printf("test (MD5) %d ok\n",i);
216 i++;
217 R++;
218 P++;
221 P=digest_in_SHA;
222 R=digest_out_SHA_0;
223 i=1;
224 while (*P)
226 rtl_digest_SHA (*P, strlen(*P), md, sizeof(md));
228 p=pt (md, RTL_DIGEST_LENGTH_SHA);
229 if (strcmp (p, *R))
231 printf("error calculating SHA-0 on '%s'\n",*P);
232 printf("got %s instead of %s\n",p,*R);
233 err++;
235 else
236 printf("test (SHA-0) %d ok\n",i);
237 i++;
238 R++;
239 P++;
242 memset (buffer, 'a', sizeof(buffer));
243 R = &digest_bigout_SHA_0;
245 Digest = rtl_digest_createSHA();
246 for (i=0; i<1000; i++)
247 rtl_digest_updateSHA (Digest, buffer, sizeof(buffer));
249 rtl_digest_getSHA (Digest, md, sizeof(md));
250 rtl_digest_destroySHA (Digest);
252 p=pt (md, RTL_DIGEST_LENGTH_SHA);
253 if (strcmp (p, *R))
255 printf("error calculating SHA-0 on '%s'\n",p);
256 printf("got %s instead of %s\n",p,*R);
257 err++;
259 else
260 printf("test (SHA-0) n ok\n");
262 P=digest_in_SHA;
263 R=digest_out_SHA_1;
264 i=1;
265 while (*P)
267 rtl_digest_SHA1 (*P, strlen(*P), md, sizeof(md));
269 p=pt (md, RTL_DIGEST_LENGTH_SHA1);
270 if (strcmp (p, *R))
272 printf("error calculating SHA-1 on '%s'\n",*P);
273 printf("got %s instead of %s\n",p,*R);
274 err++;
276 else
277 printf("test (SHA-1) %d ok\n",i);
278 i++;
279 R++;
280 P++;
283 memset (buffer, 'a', sizeof(buffer));
284 R = &digest_bigout_SHA_1;
286 Digest = rtl_digest_createSHA1();
287 for (i=0; i<1000; i++)
288 rtl_digest_updateSHA1 (Digest, buffer, sizeof(buffer));
290 rtl_digest_getSHA1 (Digest, md, sizeof(md));
291 rtl_digest_destroySHA1 (Digest);
293 p=pt (md, RTL_DIGEST_LENGTH_SHA1);
294 if (strcmp (p, *R))
296 printf("error calculating SHA-1 on '%s'\n",p);
297 printf("got %s instead of %s\n",p,*R);
298 err++;
300 else
301 printf("test (SHA-1) n ok\n");
304 P=digest_in_HMAC_MD5;
305 Q=digest_key_HMAC_MD5;
306 R=digest_out_HMAC_MD5;
307 Digest = rtl_digest_createHMAC_MD5();
308 i = 1;
309 while (*P)
311 rtl_digest_initHMAC_MD5 (Digest, (const sal_uInt8*)(*Q), strlen(*Q));
312 rtl_digest_updateHMAC_MD5 (Digest, *P, strlen(*P));
313 rtl_digest_getHMAC_MD5 (Digest, md, sizeof(md));
315 p=pt (md, RTL_DIGEST_LENGTH_HMAC_MD5);
316 if (strcmp (p, *R))
318 printf("error calculating HMAC-MD5 on '%s'\n",*P);
319 printf("got %s instead of %s\n",p,*R);
320 err++;
322 else
323 printf("test (HMAC-MD5) %d ok\n",i);
324 i++;
325 R++;
326 P++;
327 Q++;
329 rtl_digest_destroyHMAC_MD5 (Digest);
332 P=digest_in_HMAC_MD5;
333 Q=digest_key_HMAC_MD5;
334 R=digest_out_HMAC_SHA1;
335 Digest = rtl_digest_createHMAC_SHA1();
336 i = 1;
337 while (*P)
339 rtl_digest_initHMAC_SHA1 (Digest, (const sal_uInt8*)(*Q), strlen(*Q));
340 rtl_digest_updateHMAC_SHA1 (Digest, (const sal_uInt8*)(*P), strlen(*P));
341 rtl_digest_getHMAC_SHA1 (Digest, md, sizeof(md));
343 p=pt (md, RTL_DIGEST_LENGTH_HMAC_SHA1);
344 if (strcmp (p, *R))
346 printf("error calculating HMAC-SHA-1 on '%s'\n",*P);
347 printf("got %s instead of %s\n",p,*R);
348 err++;
350 else
351 printf("test (HMAC-SHA-1) %d ok\n",i);
352 i++;
353 P++;
354 Q++;
355 R++;
357 rtl_digest_destroyHMAC_SHA1 (Digest);
360 P=digest_in_HMAC_MD5;
361 Q=digest_key_HMAC_MD5;
362 rtl_digest_PBKDF2 (
363 md, RTL_DIGEST_LENGTH_MD5, /* [out] derived key */
364 (const sal_uInt8*)(Q[1]), strlen(Q[1]), /* [in] password */
365 (const sal_uInt8*)(P[1]), strlen(P[1]), /* [in] salt */
366 1000); /* [in] iteration count */
368 p=pt (md, RTL_DIGEST_LENGTH_MD5);
369 if (strcmp (p, "6349e09cb6b8c1485cfa9780ee3264df"))
371 printf("error calculating PBKDF2 on '%s'\n", P[1]);
372 err++;
374 else
375 printf("test (PBKDF2) %d ok\n", 1);
377 return (err);
380 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */