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 ************************************************************************/
33 #include <rtl/digest.h>
35 static const char *digest_in_MD
[] =
41 "abcdefghijklmnopqrstuvwxyz",
42 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
43 "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
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
[]=
72 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
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,
101 static const char digest_key_HMAC_MD5_2
[] =
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,
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
,
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,
132 static const char *digest_in_HMAC_MD5
[] =
135 "what do ya want for nothing?",
136 (const char*)&digest_in_HMAC_MD5_3
,
140 static const char *digest_out_HMAC_MD5
[] =
142 "9294727a3638bb1c13f48ef8158bfc9d",
143 "750c783e6ab0b503eaa86e310a5db738",
144 "56be34521d144c88dbb8c733f0e8b3f6",
147 static const char *digest_out_HMAC_SHA1
[] =
149 /* unofficial, i.e. not verified */
150 "675b0b3a1b4ddf4e124872da6c2f632bfed957e9",
151 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
152 "d730594d167e35d5956fd8003d0db3d3f46dc7bb",
157 static char *pt (unsigned char *md
, int length
)
162 for (i
=0; i
<length
; i
++)
163 sprintf(&(buf
[i
*2]),"%02x",md
[i
]);
168 int SAL_CALL
main (void)
170 const char **P
,**R
, **Q
;
174 unsigned char md
[80];
175 unsigned char buffer
[1000];
184 rtl_digest_MD2 (*P
, strlen(*P
), md
, sizeof(md
));
186 p
=pt (md
, RTL_DIGEST_LENGTH_MD2
);
189 printf("error calculating MD2 on '%s'\n",*P
);
190 printf("got %s instead of %s\n",p
,*R
);
194 printf("test (MD2) %d ok\n",i
);
205 rtl_digest_MD5 (*P
, strlen(*P
), md
, sizeof(md
));
207 p
=pt (md
, RTL_DIGEST_LENGTH_MD5
);
210 printf("error calculating MD5 on '%s'\n",*P
);
211 printf("got %s instead of %s\n",p
,*R
);
215 printf("test (MD5) %d ok\n",i
);
226 rtl_digest_SHA (*P
, strlen(*P
), md
, sizeof(md
));
228 p
=pt (md
, RTL_DIGEST_LENGTH_SHA
);
231 printf("error calculating SHA-0 on '%s'\n",*P
);
232 printf("got %s instead of %s\n",p
,*R
);
236 printf("test (SHA-0) %d ok\n",i
);
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
);
255 printf("error calculating SHA-0 on '%s'\n",p
);
256 printf("got %s instead of %s\n",p
,*R
);
260 printf("test (SHA-0) n ok\n");
267 rtl_digest_SHA1 (*P
, strlen(*P
), md
, sizeof(md
));
269 p
=pt (md
, RTL_DIGEST_LENGTH_SHA1
);
272 printf("error calculating SHA-1 on '%s'\n",*P
);
273 printf("got %s instead of %s\n",p
,*R
);
277 printf("test (SHA-1) %d ok\n",i
);
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
);
296 printf("error calculating SHA-1 on '%s'\n",p
);
297 printf("got %s instead of %s\n",p
,*R
);
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();
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
);
318 printf("error calculating HMAC-MD5 on '%s'\n",*P
);
319 printf("got %s instead of %s\n",p
,*R
);
323 printf("test (HMAC-MD5) %d ok\n",i
);
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();
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
);
346 printf("error calculating HMAC-SHA-1 on '%s'\n",*P
);
347 printf("got %s instead of %s\n",p
,*R
);
351 printf("test (HMAC-SHA-1) %d ok\n",i
);
357 rtl_digest_destroyHMAC_SHA1 (Digest
);
360 P
=digest_in_HMAC_MD5
;
361 Q
=digest_key_HMAC_MD5
;
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]);
375 printf("test (PBKDF2) %d ok\n", 1);
380 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */