1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
32 #include <rtl/digest.h>
34 static const char *digest_in_MD
[] =
40 "abcdefghijklmnopqrstuvwxyz",
41 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
42 "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
46 static const char *digest_out_MD2
[]=
48 "8350e5a3e24c153df2275c9f80692773",
49 "32ec01ec4a6dac72c0ab96fb34c0b5d1",
50 "da853b0d3f88d99b30283a69e6ded6bb",
51 "ab4f496bfb2a530b219ff33031fe06b0",
52 "4e8ddff3650292ab5a4108c3aa47940b",
53 "da33def2a42df13975352846c30338cd",
54 "d5976f79d83d3a0dc9806c3c66f3efd8",
57 static const char *digest_out_MD5
[]=
59 "d41d8cd98f00b204e9800998ecf8427e",
60 "0cc175b9c0f1b6a831c399e269772661",
61 "900150983cd24fb0d6963f7d28e17f72",
62 "f96b697d7cb7938d525a2f31aaf161d0",
63 "c3fcd3d76192e4007dfb496cca67e13b",
64 "d174ab98d277d9f5a5611c2c9f419d9f",
65 "57edf4a22be3c955ac49da2e2107b67a",
68 static const char *digest_in_SHA
[]=
71 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
75 static const char *digest_out_SHA_0
[]=
77 "0164b8a914cd2a5e74c4f7ff082c4d97f1edf880",
78 "d2516ee1acfa5baf33dfc1c471e438449ef134c8",
81 static const char *digest_out_SHA_1
[]=
83 "a9993e364706816aba3e25717850c26c9cd0d89d",
84 "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
87 static const char *digest_bigout_SHA_0
=
88 "3232affa48628a26653b5aaa44541fd90d690603";
90 static const char *digest_bigout_SHA_1
=
91 "34aa973cd4c4daa4f61eeb2bdbad27316534016f";
94 static const char digest_key_HMAC_MD5_1
[] =
96 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
97 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
100 static const char digest_key_HMAC_MD5_2
[] =
106 static const unsigned char digest_key_HMAC_MD5_3
[] =
108 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
109 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
112 static const char *digest_key_HMAC_MD5
[] =
114 (const char*)&digest_key_HMAC_MD5_1
,
115 (const char*)&digest_key_HMAC_MD5_2
, /* "Jefe", */
116 (const char*)&digest_key_HMAC_MD5_3
,
120 static const unsigned char digest_in_HMAC_MD5_3
[] =
122 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
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,
131 static const char *digest_in_HMAC_MD5
[] =
134 "what do ya want for nothing?",
135 (const char*)&digest_in_HMAC_MD5_3
,
139 static const char *digest_out_HMAC_MD5
[] =
141 "9294727a3638bb1c13f48ef8158bfc9d",
142 "750c783e6ab0b503eaa86e310a5db738",
143 "56be34521d144c88dbb8c733f0e8b3f6",
146 static const char *digest_out_HMAC_SHA1
[] =
148 /* unofficial, i.e. not verified */
149 "675b0b3a1b4ddf4e124872da6c2f632bfed957e9",
150 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
151 "d730594d167e35d5956fd8003d0db3d3f46dc7bb",
156 static char *pt (unsigned char *md
, int length
)
161 for (i
=0; i
<length
; i
++)
162 sprintf(&(buf
[i
*2]),"%02x",md
[i
]);
167 int SAL_CALL
main (int argc
, char **argv
)
169 const char **P
,**R
, **Q
;
173 unsigned char md
[80];
174 unsigned char buffer
[1000];
183 rtl_digest_MD2 (*P
, strlen(*P
), md
, sizeof(md
));
185 p
=pt (md
, RTL_DIGEST_LENGTH_MD2
);
188 printf("error calculating MD2 on '%s'\n",*P
);
189 printf("got %s instead of %s\n",p
,*R
);
193 printf("test (MD2) %d ok\n",i
);
204 rtl_digest_MD5 (*P
, strlen(*P
), md
, sizeof(md
));
206 p
=pt (md
, RTL_DIGEST_LENGTH_MD5
);
209 printf("error calculating MD5 on '%s'\n",*P
);
210 printf("got %s instead of %s\n",p
,*R
);
214 printf("test (MD5) %d ok\n",i
);
225 rtl_digest_SHA (*P
, strlen(*P
), md
, sizeof(md
));
227 p
=pt (md
, RTL_DIGEST_LENGTH_SHA
);
230 printf("error calculating SHA-0 on '%s'\n",*P
);
231 printf("got %s instead of %s\n",p
,*R
);
235 printf("test (SHA-0) %d ok\n",i
);
241 memset (buffer
, 'a', sizeof(buffer
));
242 R
= &digest_bigout_SHA_0
;
244 Digest
= rtl_digest_createSHA();
245 for (i
=0; i
<1000; i
++)
246 rtl_digest_updateSHA (Digest
, buffer
, sizeof(buffer
));
248 rtl_digest_getSHA (Digest
, md
, sizeof(md
));
249 rtl_digest_destroySHA (Digest
);
251 p
=pt (md
, RTL_DIGEST_LENGTH_SHA
);
254 printf("error calculating SHA-0 on '%s'\n",p
);
255 printf("got %s instead of %s\n",p
,*R
);
259 printf("test (SHA-0) n ok\n");
266 rtl_digest_SHA1 (*P
, strlen(*P
), md
, sizeof(md
));
268 p
=pt (md
, RTL_DIGEST_LENGTH_SHA1
);
271 printf("error calculating SHA-1 on '%s'\n",*P
);
272 printf("got %s instead of %s\n",p
,*R
);
276 printf("test (SHA-1) %d ok\n",i
);
282 memset (buffer
, 'a', sizeof(buffer
));
283 R
= &digest_bigout_SHA_1
;
285 Digest
= rtl_digest_createSHA1();
286 for (i
=0; i
<1000; i
++)
287 rtl_digest_updateSHA1 (Digest
, buffer
, sizeof(buffer
));
289 rtl_digest_getSHA1 (Digest
, md
, sizeof(md
));
290 rtl_digest_destroySHA1 (Digest
);
292 p
=pt (md
, RTL_DIGEST_LENGTH_SHA1
);
295 printf("error calculating SHA-1 on '%s'\n",p
);
296 printf("got %s instead of %s\n",p
,*R
);
300 printf("test (SHA-1) n ok\n");
303 P
=digest_in_HMAC_MD5
;
304 Q
=digest_key_HMAC_MD5
;
305 R
=digest_out_HMAC_MD5
;
306 Digest
= rtl_digest_createHMAC_MD5();
310 rtl_digest_initHMAC_MD5 (Digest
, *Q
, strlen(*Q
));
311 rtl_digest_updateHMAC_MD5 (Digest
, *P
, strlen(*P
));
312 rtl_digest_getHMAC_MD5 (Digest
, md
, sizeof(md
));
314 p
=pt (md
, RTL_DIGEST_LENGTH_HMAC_MD5
);
317 printf("error calculating HMAC-MD5 on '%s'\n",*P
);
318 printf("got %s instead of %s\n",p
,*R
);
322 printf("test (HMAC-MD5) %d ok\n",i
);
328 rtl_digest_destroyHMAC_MD5 (Digest
);
331 P
=digest_in_HMAC_MD5
;
332 Q
=digest_key_HMAC_MD5
;
333 R
=digest_out_HMAC_SHA1
;
334 Digest
= rtl_digest_createHMAC_SHA1();
338 rtl_digest_initHMAC_SHA1 (Digest
, *Q
, strlen(*Q
));
339 rtl_digest_updateHMAC_SHA1 (Digest
, *P
, strlen(*P
));
340 rtl_digest_getHMAC_SHA1 (Digest
, md
, sizeof(md
));
342 p
=pt (md
, RTL_DIGEST_LENGTH_HMAC_SHA1
);
345 printf("error calculating HMAC-SHA-1 on '%s'\n",*P
);
346 printf("got %s instead of %s\n",p
,*R
);
350 printf("test (HMAC-SHA-1) %d ok\n",i
);
356 rtl_digest_destroyHMAC_SHA1 (Digest
);
359 P
=digest_in_HMAC_MD5
;
360 Q
=digest_key_HMAC_MD5
;
362 md
, RTL_DIGEST_LENGTH_MD5
, /* [out] derived key */
363 Q
[1], strlen(Q
[1]), /* [in] password */
364 P
[1], strlen(P
[1]), /* [in] salt */
365 1000); /* [in] iteration count */
367 p
=pt (md
, RTL_DIGEST_LENGTH_MD5
);
368 if (strcmp (p
, "6349e09cb6b8c1485cfa9780ee3264df"))
370 printf("error calculating PBKDF2 on '%s'\n", P
[1]);
374 printf("test (PBKDF2) %d ok\n", 1);