update emoji autocorrect entries from po-files
[LibreOffice.git] / sal / workben / t_digest.c
blob2372206950d3e8a8d2e464d68fa5b04833f58ba5
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";
85 static const char digest_key_HMAC_MD5_1[] =
87 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
88 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
89 0x00
91 static const char digest_key_HMAC_MD5_2[] =
93 /* "Jefe" */
94 'J', 'e', 'f', 'e',
95 0x00
97 static const unsigned char digest_key_HMAC_MD5_3[] =
99 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
100 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
101 0x00
103 static const char *digest_key_HMAC_MD5[] =
105 (const char*)&digest_key_HMAC_MD5_1,
106 (const char*)&digest_key_HMAC_MD5_2, /* "Jefe", */
107 (const char*)&digest_key_HMAC_MD5_3,
108 NULL
111 static const unsigned char digest_in_HMAC_MD5_3[] =
113 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
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,
120 0x00
122 static const char *digest_in_HMAC_MD5[] =
124 "Hi There",
125 "what do ya want for nothing?",
126 (const char*)&digest_in_HMAC_MD5_3,
127 NULL
130 static const char *digest_out_HMAC_MD5[] =
132 "9294727a3638bb1c13f48ef8158bfc9d",
133 "750c783e6ab0b503eaa86e310a5db738",
134 "56be34521d144c88dbb8c733f0e8b3f6",
135 NULL
137 static const char *digest_out_HMAC_SHA1[] =
139 /* unofficial, i.e. not verified */
140 "675b0b3a1b4ddf4e124872da6c2f632bfed957e9",
141 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
142 "d730594d167e35d5956fd8003d0db3d3f46dc7bb",
143 NULL
146 static char *pt (unsigned char *md, int length)
148 int i;
149 static char buf[80];
151 for (i=0; i<length; i++)
152 sprintf(&(buf[i*2]),"%02x",md[i]);
154 return buf;
157 int SAL_CALL main (void)
159 const char **P,**R, **Q;
160 char *p;
161 int i=1, err=0;
163 unsigned char md[80];
164 unsigned char buffer[1000];
166 rtlDigest Digest;
168 P=digest_in_MD;
169 R=digest_out_MD2;
170 i = 1;
171 while (*P)
173 rtl_digest_MD2 (*P, strlen(*P), md, sizeof(md));
175 p=pt (md, RTL_DIGEST_LENGTH_MD2);
176 if (strcmp (p, *R))
178 printf("error calculating MD2 on '%s'\n",*P);
179 printf("got %s instead of %s\n",p,*R);
180 err++;
182 else
183 printf("test (MD2) %d ok\n",i);
184 i++;
185 R++;
186 P++;
189 P=digest_in_MD;
190 R=digest_out_MD5;
191 i=1;
192 while (*P)
194 rtl_digest_MD5 (*P, strlen(*P), md, sizeof(md));
196 p=pt (md, RTL_DIGEST_LENGTH_MD5);
197 if (strcmp (p, *R))
199 printf("error calculating MD5 on '%s'\n",*P);
200 printf("got %s instead of %s\n",p,*R);
201 err++;
203 else
204 printf("test (MD5) %d ok\n",i);
205 i++;
206 R++;
207 P++;
210 P=digest_in_SHA;
211 R=digest_out_SHA_0;
212 i=1;
213 while (*P)
215 rtl_digest_SHA (*P, strlen(*P), md, sizeof(md));
217 p=pt (md, RTL_DIGEST_LENGTH_SHA);
218 if (strcmp (p, *R))
220 printf("error calculating SHA-0 on '%s'\n",*P);
221 printf("got %s instead of %s\n",p,*R);
222 err++;
224 else
225 printf("test (SHA-0) %d ok\n",i);
226 i++;
227 R++;
228 P++;
231 memset (buffer, 'a', sizeof(buffer));
232 R = &digest_bigout_SHA_0;
234 Digest = rtl_digest_createSHA();
235 for (i=0; i<1000; i++)
236 rtl_digest_updateSHA (Digest, buffer, sizeof(buffer));
238 rtl_digest_getSHA (Digest, md, sizeof(md));
239 rtl_digest_destroySHA (Digest);
241 p=pt (md, RTL_DIGEST_LENGTH_SHA);
242 if (strcmp (p, *R))
244 printf("error calculating SHA-0 on '%s'\n",p);
245 printf("got %s instead of %s\n",p,*R);
246 err++;
248 else
249 printf("test (SHA-0) n ok\n");
251 P=digest_in_SHA;
252 R=digest_out_SHA_1;
253 i=1;
254 while (*P)
256 rtl_digest_SHA1 (*P, strlen(*P), md, sizeof(md));
258 p=pt (md, RTL_DIGEST_LENGTH_SHA1);
259 if (strcmp (p, *R))
261 printf("error calculating SHA-1 on '%s'\n",*P);
262 printf("got %s instead of %s\n",p,*R);
263 err++;
265 else
266 printf("test (SHA-1) %d ok\n",i);
267 i++;
268 R++;
269 P++;
272 memset (buffer, 'a', sizeof(buffer));
273 R = &digest_bigout_SHA_1;
275 Digest = rtl_digest_createSHA1();
276 for (i=0; i<1000; i++)
277 rtl_digest_updateSHA1 (Digest, buffer, sizeof(buffer));
279 rtl_digest_getSHA1 (Digest, md, sizeof(md));
280 rtl_digest_destroySHA1 (Digest);
282 p=pt (md, RTL_DIGEST_LENGTH_SHA1);
283 if (strcmp (p, *R))
285 printf("error calculating SHA-1 on '%s'\n",p);
286 printf("got %s instead of %s\n",p,*R);
287 err++;
289 else
290 printf("test (SHA-1) n ok\n");
292 P=digest_in_HMAC_MD5;
293 Q=digest_key_HMAC_MD5;
294 R=digest_out_HMAC_MD5;
295 Digest = rtl_digest_createHMAC_MD5();
296 i = 1;
297 while (*P)
299 rtl_digest_initHMAC_MD5 (Digest, (const sal_uInt8*)(*Q), strlen(*Q));
300 rtl_digest_updateHMAC_MD5 (Digest, *P, strlen(*P));
301 rtl_digest_getHMAC_MD5 (Digest, md, sizeof(md));
303 p=pt (md, RTL_DIGEST_LENGTH_HMAC_MD5);
304 if (strcmp (p, *R))
306 printf("error calculating HMAC-MD5 on '%s'\n",*P);
307 printf("got %s instead of %s\n",p,*R);
308 err++;
310 else
311 printf("test (HMAC-MD5) %d ok\n",i);
312 i++;
313 R++;
314 P++;
315 Q++;
317 rtl_digest_destroyHMAC_MD5 (Digest);
319 P=digest_in_HMAC_MD5;
320 Q=digest_key_HMAC_MD5;
321 R=digest_out_HMAC_SHA1;
322 Digest = rtl_digest_createHMAC_SHA1();
323 i = 1;
324 while (*P)
326 rtl_digest_initHMAC_SHA1 (Digest, (const sal_uInt8*)(*Q), strlen(*Q));
327 rtl_digest_updateHMAC_SHA1 (Digest, (const sal_uInt8*)(*P), strlen(*P));
328 rtl_digest_getHMAC_SHA1 (Digest, md, sizeof(md));
330 p=pt (md, RTL_DIGEST_LENGTH_HMAC_SHA1);
331 if (strcmp (p, *R))
333 printf("error calculating HMAC-SHA-1 on '%s'\n",*P);
334 printf("got %s instead of %s\n",p,*R);
335 err++;
337 else
338 printf("test (HMAC-SHA-1) %d ok\n",i);
339 i++;
340 P++;
341 Q++;
342 R++;
344 rtl_digest_destroyHMAC_SHA1 (Digest);
346 P=digest_in_HMAC_MD5;
347 Q=digest_key_HMAC_MD5;
348 rtl_digest_PBKDF2 (
349 md, RTL_DIGEST_LENGTH_MD5, /* [out] derived key */
350 (const sal_uInt8*)(Q[1]), strlen(Q[1]), /* [in] password */
351 (const sal_uInt8*)(P[1]), strlen(P[1]), /* [in] salt */
352 1000); /* [in] iteration count */
354 p=pt (md, RTL_DIGEST_LENGTH_MD5);
355 if (strcmp (p, "6349e09cb6b8c1485cfa9780ee3264df"))
357 printf("error calculating PBKDF2 on '%s'\n", P[1]);
358 err++;
360 else
361 printf("test (PBKDF2) %d ok\n", 1);
363 return err;
366 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */