1 /* Copyright 2001-2005 The Apache Software Foundation or its licensors, as
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 /* This is derived from material copyright RSA Data Security, Inc.
17 * Their notice is reproduced below in its entirety.
19 * Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
22 * RSA Data Security, Inc. makes no representations concerning either
23 * the merchantability of this software or the suitability of this
24 * software for any particular purpose. It is provided "as is"
25 * without express or implied warranty of any kind.
27 * These notices must be retained in any copies of any part of this
28 * documentation and/or software.
33 #include "apr_general.h"
34 #include "apr_file_io.h"
43 * This is a MD4 test program based on the code published in RFC 1320.
44 * When run as ./testmd4 -x it should produce the following output:
47 MD4 ("") = 31d6cfe0d16ae931b73c59d7e0c089c0
48 MD4 ("a") = bde52cb31de33e46245e05fbdbd6fb24
49 MD4 ("abc") = a448017aaf21d8525fc10ae87aa6729d
50 MD4 ("message digest") = d9130a8164549fe818874806e1c7014b
51 MD4 ("abcdefghijklmnopqrstuvwxyz") = d79e1c308aa5bbcdeea8ed63df412da9
52 MD4 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") = 043f8582f241db351ce627e153e7f0e4
53 MD4 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890") = e33b4ddc9c38f2199c3e7b164fcc0536
57 /* Length of test block, number of test blocks.
59 #define TEST_BLOCK_LEN 1000
60 #define TEST_BLOCK_COUNT 1000
62 apr_pool_t *local_pool;
63 apr_file_t *in, *out, *err;
65 /* Prints a message digest in hexadecimal.
67 static void MDPrint (unsigned char digest[APR_MD4_DIGESTSIZE])
71 for (i = 0; i < APR_MD4_DIGESTSIZE; i++)
72 apr_file_printf(out, "%02x", digest[i]);
75 /* Digests a string and prints the result.
77 static void MDString(char *string)
79 apr_md4_ctx_t context;
80 unsigned char digest[APR_MD4_DIGESTSIZE];
81 unsigned int len = strlen(string);
83 apr_md4_init(&context);
84 apr_md4_update(&context, (unsigned char *)string, len);
85 apr_md4_final(digest, &context);
87 apr_file_printf (out, "MD4 (\"%s\") = ", string);
89 apr_file_printf (out, "\n");
92 /* Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte
95 static void MDTimeTrial(void)
97 apr_md4_ctx_t context;
98 apr_time_t endTime, startTime;
99 apr_interval_time_t timeTaken;
100 unsigned char block[TEST_BLOCK_LEN], digest[APR_MD4_DIGESTSIZE];
103 apr_file_printf(out, "MD4 time trial. Digesting %d %d-byte blocks ...",
104 TEST_BLOCK_LEN, TEST_BLOCK_COUNT);
106 /* Initialize block */
107 for (i = 0; i < TEST_BLOCK_LEN; i++)
108 block[i] = (unsigned char)(i & 0xff);
111 startTime = apr_time_now();
114 apr_md4_init(&context);
115 for (i = 0; i < TEST_BLOCK_COUNT; i++)
116 apr_md4_update(&context, block, TEST_BLOCK_LEN);
118 apr_md4_final(digest, &context);
121 endTime = apr_time_now();
122 timeTaken = endTime - startTime;
124 apr_file_printf(out, " done\n");
125 apr_file_printf(out, "Digest = ");
128 apr_file_printf(out, "\nTime = %" APR_TIME_T_FMT " seconds\n", timeTaken);
129 apr_file_printf(out, "Speed = % " APR_TIME_T_FMT " bytes/second\n",
130 TEST_BLOCK_LEN * TEST_BLOCK_COUNT/timeTaken);
133 /* Digests a reference suite of strings and prints the results.
135 static void MDTestSuite(void)
137 apr_file_printf(out, "MD4 test suite:\n");
142 MDString("message digest");
143 MDString("abcdefghijklmnopqrstuvwxyz");
144 MDString("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
145 MDString("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
148 /* Digests a file and prints the result.
150 static void MDFile(char *filename)
153 apr_md4_ctx_t context;
154 apr_size_t len = 1024;
155 unsigned char buffer[1024], digest[APR_MD4_DIGESTSIZE];
157 if (apr_file_open(&file, filename, APR_READ, APR_OS_DEFAULT, local_pool)
159 apr_file_printf(err, "%s can't be opened\n", filename);
161 apr_md4_init(&context);
162 while (apr_file_read(file, buffer, &len) != APR_SUCCESS)
164 apr_md4_update(&context, buffer, len);
167 apr_md4_final(digest, &context);
169 apr_file_close(file);
171 apr_file_printf(out, "MD4 (%s) = ", filename);
173 apr_file_printf(out, "\n");
177 /* Digests the standard input and prints the result.
179 static void MDFilter(void)
181 apr_md4_ctx_t context;
183 unsigned char buffer[16], digest[16];
185 apr_md4_init(&context);
186 while (apr_file_read(in, buffer, &len) != APR_SUCCESS)
188 apr_md4_update(&context, buffer, len);
191 apr_md4_update(&context, buffer, len);
192 apr_md4_final(digest, &context);
195 apr_file_printf(out, "\n");
201 Arguments (may be any combination):
202 -sstring - digests string
204 -x - runs test script
205 filename - digests file
206 (none) - digests standard input
208 int main (int argc, char **argv)
213 atexit(apr_terminate);
215 if (apr_pool_create(&local_pool, NULL) != APR_SUCCESS)
218 apr_file_open_stdin(&in, local_pool);
219 apr_file_open_stdout(&out, local_pool);
220 apr_file_open_stderr(&err, local_pool);
224 for (i = 1; i < argc; i++)
225 if (argv[i][0] == '-' && argv[i][1] == 's')
226 MDString(argv[i] + 2);
227 else if (strcmp(argv[i], "-t") == 0)
229 else if (strcmp (argv[i], "-x") == 0)