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";
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,
91 static const char digest_key_HMAC_MD5_2
[] =
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,
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
,
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,
122 static const char *digest_in_HMAC_MD5
[] =
125 "what do ya want for nothing?",
126 (const char*)&digest_in_HMAC_MD5_3
,
130 static const char *digest_out_HMAC_MD5
[] =
132 "9294727a3638bb1c13f48ef8158bfc9d",
133 "750c783e6ab0b503eaa86e310a5db738",
134 "56be34521d144c88dbb8c733f0e8b3f6",
137 static const char *digest_out_HMAC_SHA1
[] =
139 /* unofficial, i.e. not verified */
140 "675b0b3a1b4ddf4e124872da6c2f632bfed957e9",
141 "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
142 "d730594d167e35d5956fd8003d0db3d3f46dc7bb",
146 static char *pt (unsigned char *md
, int length
)
151 for (i
=0; i
<length
; i
++)
152 sprintf(&(buf
[i
*2]),"%02x",md
[i
]);
157 int SAL_CALL
main (void)
159 const char **P
,**R
, **Q
;
163 unsigned char md
[80];
164 unsigned char buffer
[1000];
173 rtl_digest_MD2 (*P
, strlen(*P
), md
, sizeof(md
));
175 p
=pt (md
, RTL_DIGEST_LENGTH_MD2
);
178 printf("error calculating MD2 on '%s'\n",*P
);
179 printf("got %s instead of %s\n",p
,*R
);
183 printf("test (MD2) %d ok\n",i
);
194 rtl_digest_MD5 (*P
, strlen(*P
), md
, sizeof(md
));
196 p
=pt (md
, RTL_DIGEST_LENGTH_MD5
);
199 printf("error calculating MD5 on '%s'\n",*P
);
200 printf("got %s instead of %s\n",p
,*R
);
204 printf("test (MD5) %d ok\n",i
);
215 rtl_digest_SHA (*P
, strlen(*P
), md
, sizeof(md
));
217 p
=pt (md
, RTL_DIGEST_LENGTH_SHA
);
220 printf("error calculating SHA-0 on '%s'\n",*P
);
221 printf("got %s instead of %s\n",p
,*R
);
225 printf("test (SHA-0) %d ok\n",i
);
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
);
244 printf("error calculating SHA-0 on '%s'\n",p
);
245 printf("got %s instead of %s\n",p
,*R
);
249 printf("test (SHA-0) n ok\n");
256 rtl_digest_SHA1 (*P
, strlen(*P
), md
, sizeof(md
));
258 p
=pt (md
, RTL_DIGEST_LENGTH_SHA1
);
261 printf("error calculating SHA-1 on '%s'\n",*P
);
262 printf("got %s instead of %s\n",p
,*R
);
266 printf("test (SHA-1) %d ok\n",i
);
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
);
285 printf("error calculating SHA-1 on '%s'\n",p
);
286 printf("got %s instead of %s\n",p
,*R
);
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();
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
);
306 printf("error calculating HMAC-MD5 on '%s'\n",*P
);
307 printf("got %s instead of %s\n",p
,*R
);
311 printf("test (HMAC-MD5) %d ok\n",i
);
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();
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
);
333 printf("error calculating HMAC-SHA-1 on '%s'\n",*P
);
334 printf("got %s instead of %s\n",p
,*R
);
338 printf("test (HMAC-SHA-1) %d ok\n",i
);
344 rtl_digest_destroyHMAC_SHA1 (Digest
);
346 P
=digest_in_HMAC_MD5
;
347 Q
=digest_key_HMAC_MD5
;
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]);
361 printf("test (PBKDF2) %d ok\n", 1);
366 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */