Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / sal / workben / t_digest.c
blobf2bbf62fe6a20f0ed4d60330458611956fe38a7b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
24 #include <rtl/digest.h>
26 static const char *digest_in_MD[] =
28 "",
29 "a",
30 "abc",
31 "message digest",
32 "abcdefghijklmnopqrstuvwxyz",
33 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
34 "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
35 NULL,
38 static const char *digest_out_MD2[]=
40 "8350e5a3e24c153df2275c9f80692773",
41 "32ec01ec4a6dac72c0ab96fb34c0b5d1",
42 "da853b0d3f88d99b30283a69e6ded6bb",
43 "ab4f496bfb2a530b219ff33031fe06b0",
44 "4e8ddff3650292ab5a4108c3aa47940b",
45 "da33def2a42df13975352846c30338cd",
46 "d5976f79d83d3a0dc9806c3c66f3efd8",
49 static const char *digest_out_MD5[]=
51 "d41d8cd98f00b204e9800998ecf8427e",
52 "0cc175b9c0f1b6a831c399e269772661",
53 "900150983cd24fb0d6963f7d28e17f72",
54 "f96b697d7cb7938d525a2f31aaf161d0",
55 "c3fcd3d76192e4007dfb496cca67e13b",
56 "d174ab98d277d9f5a5611c2c9f419d9f",
57 "57edf4a22be3c955ac49da2e2107b67a",
60 static const char *digest_in_SHA[]=
62 "abc",
63 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
64 NULL,
67 static const char *digest_out_SHA_0[]=
69 "0164b8a914cd2a5e74c4f7ff082c4d97f1edf880",
70 "d2516ee1acfa5baf33dfc1c471e438449ef134c8",
73 static const char *digest_out_SHA_1[]=
75 "a9993e364706816aba3e25717850c26c9cd0d89d",
76 "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
79 static const char *digest_bigout_SHA_0=
80 "3232affa48628a26653b5aaa44541fd90d690603";
82 static const char *digest_bigout_SHA_1=
83 "34aa973cd4c4daa4f61eeb2bdbad27316534016f";
86 static const char digest_key_HMAC_MD5_1[] =
88 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
89 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
90 0x00
92 static const char digest_key_HMAC_MD5_2[] =
94 /* "Jefe" */
95 'J', 'e', 'f', 'e',
96 0x00
98 static const unsigned char digest_key_HMAC_MD5_3[] =
100 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
101 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
102 0x00
104 static const char *digest_key_HMAC_MD5[] =
106 (const char*)&digest_key_HMAC_MD5_1,
107 (const char*)&digest_key_HMAC_MD5_2, /* "Jefe", */
108 (const char*)&digest_key_HMAC_MD5_3,
109 NULL
112 static const unsigned char digest_in_HMAC_MD5_3[] =
114 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
115 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
116 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
117 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
118 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
119 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
120 0xDD, 0xDD,
121 0x00
123 static const char *digest_in_HMAC_MD5[] =
125 "Hi There",
126 "what do ya want for nothing?",
127 (const char*)&digest_in_HMAC_MD5_3,
128 NULL
131 static const char *digest_out_HMAC_MD5[] =
133 "9294727a3638bb1c13f48ef8158bfc9d",
134 "750c783e6ab0b503eaa86e310a5db738",
135 "56be34521d144c88dbb8c733f0e8b3f6",
136 NULL
138 static const char *digest_out_HMAC_SHA1[] =
140 /* unofficial, i.e. not verified */
141 "675b0b3a1b4ddf4e124872da6c2f632bfed957e9",
142 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
143 "d730594d167e35d5956fd8003d0db3d3f46dc7bb",
144 NULL
148 static char *pt (unsigned char *md, int length)
150 int i;
151 static char buf[80];
153 for (i=0; i<length; i++)
154 sprintf(&(buf[i*2]),"%02x",md[i]);
156 return(buf);
159 int SAL_CALL main (void)
161 const char **P,**R, **Q;
162 char *p;
163 int i=1, err=0;
165 unsigned char md[80];
166 unsigned char buffer[1000];
168 rtlDigest Digest;
170 P=digest_in_MD;
171 R=digest_out_MD2;
172 i = 1;
173 while (*P)
175 rtl_digest_MD2 (*P, strlen(*P), md, sizeof(md));
177 p=pt (md, RTL_DIGEST_LENGTH_MD2);
178 if (strcmp (p, *R))
180 printf("error calculating MD2 on '%s'\n",*P);
181 printf("got %s instead of %s\n",p,*R);
182 err++;
184 else
185 printf("test (MD2) %d ok\n",i);
186 i++;
187 R++;
188 P++;
191 P=digest_in_MD;
192 R=digest_out_MD5;
193 i=1;
194 while (*P)
196 rtl_digest_MD5 (*P, strlen(*P), md, sizeof(md));
198 p=pt (md, RTL_DIGEST_LENGTH_MD5);
199 if (strcmp (p, *R))
201 printf("error calculating MD5 on '%s'\n",*P);
202 printf("got %s instead of %s\n",p,*R);
203 err++;
205 else
206 printf("test (MD5) %d ok\n",i);
207 i++;
208 R++;
209 P++;
212 P=digest_in_SHA;
213 R=digest_out_SHA_0;
214 i=1;
215 while (*P)
217 rtl_digest_SHA (*P, strlen(*P), md, sizeof(md));
219 p=pt (md, RTL_DIGEST_LENGTH_SHA);
220 if (strcmp (p, *R))
222 printf("error calculating SHA-0 on '%s'\n",*P);
223 printf("got %s instead of %s\n",p,*R);
224 err++;
226 else
227 printf("test (SHA-0) %d ok\n",i);
228 i++;
229 R++;
230 P++;
233 memset (buffer, 'a', sizeof(buffer));
234 R = &digest_bigout_SHA_0;
236 Digest = rtl_digest_createSHA();
237 for (i=0; i<1000; i++)
238 rtl_digest_updateSHA (Digest, buffer, sizeof(buffer));
240 rtl_digest_getSHA (Digest, md, sizeof(md));
241 rtl_digest_destroySHA (Digest);
243 p=pt (md, RTL_DIGEST_LENGTH_SHA);
244 if (strcmp (p, *R))
246 printf("error calculating SHA-0 on '%s'\n",p);
247 printf("got %s instead of %s\n",p,*R);
248 err++;
250 else
251 printf("test (SHA-0) n ok\n");
253 P=digest_in_SHA;
254 R=digest_out_SHA_1;
255 i=1;
256 while (*P)
258 rtl_digest_SHA1 (*P, strlen(*P), md, sizeof(md));
260 p=pt (md, RTL_DIGEST_LENGTH_SHA1);
261 if (strcmp (p, *R))
263 printf("error calculating SHA-1 on '%s'\n",*P);
264 printf("got %s instead of %s\n",p,*R);
265 err++;
267 else
268 printf("test (SHA-1) %d ok\n",i);
269 i++;
270 R++;
271 P++;
274 memset (buffer, 'a', sizeof(buffer));
275 R = &digest_bigout_SHA_1;
277 Digest = rtl_digest_createSHA1();
278 for (i=0; i<1000; i++)
279 rtl_digest_updateSHA1 (Digest, buffer, sizeof(buffer));
281 rtl_digest_getSHA1 (Digest, md, sizeof(md));
282 rtl_digest_destroySHA1 (Digest);
284 p=pt (md, RTL_DIGEST_LENGTH_SHA1);
285 if (strcmp (p, *R))
287 printf("error calculating SHA-1 on '%s'\n",p);
288 printf("got %s instead of %s\n",p,*R);
289 err++;
291 else
292 printf("test (SHA-1) n ok\n");
295 P=digest_in_HMAC_MD5;
296 Q=digest_key_HMAC_MD5;
297 R=digest_out_HMAC_MD5;
298 Digest = rtl_digest_createHMAC_MD5();
299 i = 1;
300 while (*P)
302 rtl_digest_initHMAC_MD5 (Digest, (const sal_uInt8*)(*Q), strlen(*Q));
303 rtl_digest_updateHMAC_MD5 (Digest, *P, strlen(*P));
304 rtl_digest_getHMAC_MD5 (Digest, md, sizeof(md));
306 p=pt (md, RTL_DIGEST_LENGTH_HMAC_MD5);
307 if (strcmp (p, *R))
309 printf("error calculating HMAC-MD5 on '%s'\n",*P);
310 printf("got %s instead of %s\n",p,*R);
311 err++;
313 else
314 printf("test (HMAC-MD5) %d ok\n",i);
315 i++;
316 R++;
317 P++;
318 Q++;
320 rtl_digest_destroyHMAC_MD5 (Digest);
323 P=digest_in_HMAC_MD5;
324 Q=digest_key_HMAC_MD5;
325 R=digest_out_HMAC_SHA1;
326 Digest = rtl_digest_createHMAC_SHA1();
327 i = 1;
328 while (*P)
330 rtl_digest_initHMAC_SHA1 (Digest, (const sal_uInt8*)(*Q), strlen(*Q));
331 rtl_digest_updateHMAC_SHA1 (Digest, (const sal_uInt8*)(*P), strlen(*P));
332 rtl_digest_getHMAC_SHA1 (Digest, md, sizeof(md));
334 p=pt (md, RTL_DIGEST_LENGTH_HMAC_SHA1);
335 if (strcmp (p, *R))
337 printf("error calculating HMAC-SHA-1 on '%s'\n",*P);
338 printf("got %s instead of %s\n",p,*R);
339 err++;
341 else
342 printf("test (HMAC-SHA-1) %d ok\n",i);
343 i++;
344 P++;
345 Q++;
346 R++;
348 rtl_digest_destroyHMAC_SHA1 (Digest);
351 P=digest_in_HMAC_MD5;
352 Q=digest_key_HMAC_MD5;
353 rtl_digest_PBKDF2 (
354 md, RTL_DIGEST_LENGTH_MD5, /* [out] derived key */
355 (const sal_uInt8*)(Q[1]), strlen(Q[1]), /* [in] password */
356 (const sal_uInt8*)(P[1]), strlen(P[1]), /* [in] salt */
357 1000); /* [in] iteration count */
359 p=pt (md, RTL_DIGEST_LENGTH_MD5);
360 if (strcmp (p, "6349e09cb6b8c1485cfa9780ee3264df"))
362 printf("error calculating PBKDF2 on '%s'\n", P[1]);
363 err++;
365 else
366 printf("test (PBKDF2) %d ok\n", 1);
368 return (err);
371 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */