1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
24 #include <rtl/digest.h>
26 static const char *digest_in_MD
[] =
32 "abcdefghijklmnopqrstuvwxyz",
33 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
34 "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
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
[]=
63 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
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,
92 static const char digest_key_HMAC_MD5_2
[] =
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,
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
,
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,
123 static const char *digest_in_HMAC_MD5
[] =
126 "what do ya want for nothing?",
127 (const char*)&digest_in_HMAC_MD5_3
,
131 static const char *digest_out_HMAC_MD5
[] =
133 "9294727a3638bb1c13f48ef8158bfc9d",
134 "750c783e6ab0b503eaa86e310a5db738",
135 "56be34521d144c88dbb8c733f0e8b3f6",
138 static const char *digest_out_HMAC_SHA1
[] =
140 /* unofficial, i.e. not verified */
141 "675b0b3a1b4ddf4e124872da6c2f632bfed957e9",
142 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
143 "d730594d167e35d5956fd8003d0db3d3f46dc7bb",
148 static char *pt (unsigned char *md
, int length
)
153 for (i
=0; i
<length
; i
++)
154 sprintf(&(buf
[i
*2]),"%02x",md
[i
]);
159 int SAL_CALL
main (void)
161 const char **P
,**R
, **Q
;
165 unsigned char md
[80];
166 unsigned char buffer
[1000];
175 rtl_digest_MD2 (*P
, strlen(*P
), md
, sizeof(md
));
177 p
=pt (md
, RTL_DIGEST_LENGTH_MD2
);
180 printf("error calculating MD2 on '%s'\n",*P
);
181 printf("got %s instead of %s\n",p
,*R
);
185 printf("test (MD2) %d ok\n",i
);
196 rtl_digest_MD5 (*P
, strlen(*P
), md
, sizeof(md
));
198 p
=pt (md
, RTL_DIGEST_LENGTH_MD5
);
201 printf("error calculating MD5 on '%s'\n",*P
);
202 printf("got %s instead of %s\n",p
,*R
);
206 printf("test (MD5) %d ok\n",i
);
217 rtl_digest_SHA (*P
, strlen(*P
), md
, sizeof(md
));
219 p
=pt (md
, RTL_DIGEST_LENGTH_SHA
);
222 printf("error calculating SHA-0 on '%s'\n",*P
);
223 printf("got %s instead of %s\n",p
,*R
);
227 printf("test (SHA-0) %d ok\n",i
);
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
);
246 printf("error calculating SHA-0 on '%s'\n",p
);
247 printf("got %s instead of %s\n",p
,*R
);
251 printf("test (SHA-0) n ok\n");
258 rtl_digest_SHA1 (*P
, strlen(*P
), md
, sizeof(md
));
260 p
=pt (md
, RTL_DIGEST_LENGTH_SHA1
);
263 printf("error calculating SHA-1 on '%s'\n",*P
);
264 printf("got %s instead of %s\n",p
,*R
);
268 printf("test (SHA-1) %d ok\n",i
);
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
);
287 printf("error calculating SHA-1 on '%s'\n",p
);
288 printf("got %s instead of %s\n",p
,*R
);
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();
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
);
309 printf("error calculating HMAC-MD5 on '%s'\n",*P
);
310 printf("got %s instead of %s\n",p
,*R
);
314 printf("test (HMAC-MD5) %d ok\n",i
);
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();
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
);
337 printf("error calculating HMAC-SHA-1 on '%s'\n",*P
);
338 printf("got %s instead of %s\n",p
,*R
);
342 printf("test (HMAC-SHA-1) %d ok\n",i
);
348 rtl_digest_destroyHMAC_SHA1 (Digest
);
351 P
=digest_in_HMAC_MD5
;
352 Q
=digest_key_HMAC_MD5
;
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]);
366 printf("test (PBKDF2) %d ok\n", 1);
371 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */