1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: t_digest.c,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
35 #include <rtl/digest.h>
37 static const char *digest_in_MD
[] =
43 "abcdefghijklmnopqrstuvwxyz",
44 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
45 "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
49 static const char *digest_out_MD2
[]=
51 "8350e5a3e24c153df2275c9f80692773",
52 "32ec01ec4a6dac72c0ab96fb34c0b5d1",
53 "da853b0d3f88d99b30283a69e6ded6bb",
54 "ab4f496bfb2a530b219ff33031fe06b0",
55 "4e8ddff3650292ab5a4108c3aa47940b",
56 "da33def2a42df13975352846c30338cd",
57 "d5976f79d83d3a0dc9806c3c66f3efd8",
60 static const char *digest_out_MD5
[]=
62 "d41d8cd98f00b204e9800998ecf8427e",
63 "0cc175b9c0f1b6a831c399e269772661",
64 "900150983cd24fb0d6963f7d28e17f72",
65 "f96b697d7cb7938d525a2f31aaf161d0",
66 "c3fcd3d76192e4007dfb496cca67e13b",
67 "d174ab98d277d9f5a5611c2c9f419d9f",
68 "57edf4a22be3c955ac49da2e2107b67a",
71 static const char *digest_in_SHA
[]=
74 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
78 static const char *digest_out_SHA_0
[]=
80 "0164b8a914cd2a5e74c4f7ff082c4d97f1edf880",
81 "d2516ee1acfa5baf33dfc1c471e438449ef134c8",
84 static const char *digest_out_SHA_1
[]=
86 "a9993e364706816aba3e25717850c26c9cd0d89d",
87 "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
90 static const char *digest_bigout_SHA_0
=
91 "3232affa48628a26653b5aaa44541fd90d690603";
93 static const char *digest_bigout_SHA_1
=
94 "34aa973cd4c4daa4f61eeb2bdbad27316534016f";
97 static const char digest_key_HMAC_MD5_1
[] =
99 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
100 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
103 static const char digest_key_HMAC_MD5_2
[] =
109 static const unsigned char digest_key_HMAC_MD5_3
[] =
111 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
112 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
115 static const char *digest_key_HMAC_MD5
[] =
117 (const char*)&digest_key_HMAC_MD5_1
,
118 (const char*)&digest_key_HMAC_MD5_2
, /* "Jefe", */
119 (const char*)&digest_key_HMAC_MD5_3
,
123 static const unsigned char digest_in_HMAC_MD5_3
[] =
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,
129 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
130 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
134 static const char *digest_in_HMAC_MD5
[] =
137 "what do ya want for nothing?",
138 (const char*)&digest_in_HMAC_MD5_3
,
142 static const char *digest_out_HMAC_MD5
[] =
144 "9294727a3638bb1c13f48ef8158bfc9d",
145 "750c783e6ab0b503eaa86e310a5db738",
146 "56be34521d144c88dbb8c733f0e8b3f6",
149 static const char *digest_out_HMAC_SHA1
[] =
151 /* unofficial, i.e. not verified */
152 "675b0b3a1b4ddf4e124872da6c2f632bfed957e9",
153 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
154 "d730594d167e35d5956fd8003d0db3d3f46dc7bb",
159 static char *pt (unsigned char *md
, int length
)
164 for (i
=0; i
<length
; i
++)
165 sprintf(&(buf
[i
*2]),"%02x",md
[i
]);
170 int SAL_CALL
main (int argc
, char **argv
)
172 const char **P
,**R
, **Q
;
176 unsigned char md
[80];
177 unsigned char buffer
[1000];
186 rtl_digest_MD2 (*P
, strlen(*P
), md
, sizeof(md
));
188 p
=pt (md
, RTL_DIGEST_LENGTH_MD2
);
191 printf("error calculating MD2 on '%s'\n",*P
);
192 printf("got %s instead of %s\n",p
,*R
);
196 printf("test (MD2) %d ok\n",i
);
207 rtl_digest_MD5 (*P
, strlen(*P
), md
, sizeof(md
));
209 p
=pt (md
, RTL_DIGEST_LENGTH_MD5
);
212 printf("error calculating MD5 on '%s'\n",*P
);
213 printf("got %s instead of %s\n",p
,*R
);
217 printf("test (MD5) %d ok\n",i
);
228 rtl_digest_SHA (*P
, strlen(*P
), md
, sizeof(md
));
230 p
=pt (md
, RTL_DIGEST_LENGTH_SHA
);
233 printf("error calculating SHA-0 on '%s'\n",*P
);
234 printf("got %s instead of %s\n",p
,*R
);
238 printf("test (SHA-0) %d ok\n",i
);
244 memset (buffer
, 'a', sizeof(buffer
));
245 R
= &digest_bigout_SHA_0
;
247 Digest
= rtl_digest_createSHA();
248 for (i
=0; i
<1000; i
++)
249 rtl_digest_updateSHA (Digest
, buffer
, sizeof(buffer
));
251 rtl_digest_getSHA (Digest
, md
, sizeof(md
));
252 rtl_digest_destroySHA (Digest
);
254 p
=pt (md
, RTL_DIGEST_LENGTH_SHA
);
257 printf("error calculating SHA-0 on '%s'\n",p
);
258 printf("got %s instead of %s\n",p
,*R
);
262 printf("test (SHA-0) n ok\n");
269 rtl_digest_SHA1 (*P
, strlen(*P
), md
, sizeof(md
));
271 p
=pt (md
, RTL_DIGEST_LENGTH_SHA1
);
274 printf("error calculating SHA-1 on '%s'\n",*P
);
275 printf("got %s instead of %s\n",p
,*R
);
279 printf("test (SHA-1) %d ok\n",i
);
285 memset (buffer
, 'a', sizeof(buffer
));
286 R
= &digest_bigout_SHA_1
;
288 Digest
= rtl_digest_createSHA1();
289 for (i
=0; i
<1000; i
++)
290 rtl_digest_updateSHA1 (Digest
, buffer
, sizeof(buffer
));
292 rtl_digest_getSHA1 (Digest
, md
, sizeof(md
));
293 rtl_digest_destroySHA1 (Digest
);
295 p
=pt (md
, RTL_DIGEST_LENGTH_SHA1
);
298 printf("error calculating SHA-1 on '%s'\n",p
);
299 printf("got %s instead of %s\n",p
,*R
);
303 printf("test (SHA-1) n ok\n");
306 P
=digest_in_HMAC_MD5
;
307 Q
=digest_key_HMAC_MD5
;
308 R
=digest_out_HMAC_MD5
;
309 Digest
= rtl_digest_createHMAC_MD5();
313 rtl_digest_initHMAC_MD5 (Digest
, *Q
, strlen(*Q
));
314 rtl_digest_updateHMAC_MD5 (Digest
, *P
, strlen(*P
));
315 rtl_digest_getHMAC_MD5 (Digest
, md
, sizeof(md
));
317 p
=pt (md
, RTL_DIGEST_LENGTH_HMAC_MD5
);
320 printf("error calculating HMAC-MD5 on '%s'\n",*P
);
321 printf("got %s instead of %s\n",p
,*R
);
325 printf("test (HMAC-MD5) %d ok\n",i
);
331 rtl_digest_destroyHMAC_MD5 (Digest
);
334 P
=digest_in_HMAC_MD5
;
335 Q
=digest_key_HMAC_MD5
;
336 R
=digest_out_HMAC_SHA1
;
337 Digest
= rtl_digest_createHMAC_SHA1();
341 rtl_digest_initHMAC_SHA1 (Digest
, *Q
, strlen(*Q
));
342 rtl_digest_updateHMAC_SHA1 (Digest
, *P
, strlen(*P
));
343 rtl_digest_getHMAC_SHA1 (Digest
, md
, sizeof(md
));
345 p
=pt (md
, RTL_DIGEST_LENGTH_HMAC_SHA1
);
348 printf("error calculating HMAC-SHA-1 on '%s'\n",*P
);
349 printf("got %s instead of %s\n",p
,*R
);
353 printf("test (HMAC-SHA-1) %d ok\n",i
);
359 rtl_digest_destroyHMAC_SHA1 (Digest
);
362 P
=digest_in_HMAC_MD5
;
363 Q
=digest_key_HMAC_MD5
;
365 md
, RTL_DIGEST_LENGTH_MD5
, /* [out] derived key */
366 Q
[1], strlen(Q
[1]), /* [in] password */
367 P
[1], strlen(P
[1]), /* [in] salt */
368 1000); /* [in] iteration count */
370 p
=pt (md
, RTL_DIGEST_LENGTH_MD5
);
371 if (strcmp (p
, "6349e09cb6b8c1485cfa9780ee3264df"))
373 printf("error calculating PBKDF2 on '%s'\n", P
[1]);
377 printf("test (PBKDF2) %d ok\n", 1);