1 /* Copyright 2001-2004 The Apache Software Foundation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 /* This is derived from material copyright RSA Data Security, Inc.
16 * Their notice is reproduced below in its entirety.
18 * Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
21 * RSA Data Security, Inc. makes no representations concerning either
22 * the merchantability of this software or the suitability of this
23 * software for any particular purpose. It is provided "as is"
24 * without express or implied warranty of any kind.
26 * These notices must be retained in any copies of any part of this
27 * documentation and/or software.
32 #include "apr_general.h"
33 #include "apr_file_io.h"
42 * This is a MD4 test program based on the code published in RFC 1320.
43 * When run as ./testmd4 -x it should produce the following output:
46 MD4 ("") = 31d6cfe0d16ae931b73c59d7e0c089c0
47 MD4 ("a") = bde52cb31de33e46245e05fbdbd6fb24
48 MD4 ("abc") = a448017aaf21d8525fc10ae87aa6729d
49 MD4 ("message digest") = d9130a8164549fe818874806e1c7014b
50 MD4 ("abcdefghijklmnopqrstuvwxyz") = d79e1c308aa5bbcdeea8ed63df412da9
51 MD4 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") = 043f8582f241db351ce627e153e7f0e4
52 MD4 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890") = e33b4ddc9c38f2199c3e7b164fcc0536
56 /* Length of test block, number of test blocks.
58 #define TEST_BLOCK_LEN 1000
59 #define TEST_BLOCK_COUNT 1000
61 apr_pool_t
*local_pool
;
62 apr_file_t
*in
, *out
, *err
;
64 /* Prints a message digest in hexadecimal.
66 static void MDPrint (unsigned char digest
[APR_MD4_DIGESTSIZE
])
70 for (i
= 0; i
< APR_MD4_DIGESTSIZE
; i
++)
71 apr_file_printf(out
, "%02x", digest
[i
]);
74 /* Digests a string and prints the result.
76 static void MDString(char *string
)
78 apr_md4_ctx_t context
;
79 unsigned char digest
[APR_MD4_DIGESTSIZE
];
80 unsigned int len
= strlen(string
);
82 apr_md4_init(&context
);
83 apr_md4_update(&context
, (unsigned char *)string
, len
);
84 apr_md4_final(digest
, &context
);
86 apr_file_printf (out
, "MD4 (\"%s\") = ", string
);
88 apr_file_printf (out
, "\n");
91 /* Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte
94 static void MDTimeTrial(void)
96 apr_md4_ctx_t context
;
97 apr_time_t endTime
, startTime
;
98 apr_interval_time_t timeTaken
;
99 unsigned char block
[TEST_BLOCK_LEN
], digest
[APR_MD4_DIGESTSIZE
];
102 apr_file_printf(out
, "MD4 time trial. Digesting %d %d-byte blocks ...",
103 TEST_BLOCK_LEN
, TEST_BLOCK_COUNT
);
105 /* Initialize block */
106 for (i
= 0; i
< TEST_BLOCK_LEN
; i
++)
107 block
[i
] = (unsigned char)(i
& 0xff);
110 startTime
= apr_time_now();
113 apr_md4_init(&context
);
114 for (i
= 0; i
< TEST_BLOCK_COUNT
; i
++)
115 apr_md4_update(&context
, block
, TEST_BLOCK_LEN
);
117 apr_md4_final(digest
, &context
);
120 endTime
= apr_time_now();
121 timeTaken
= endTime
- startTime
;
123 apr_file_printf(out
, " done\n");
124 apr_file_printf(out
, "Digest = ");
127 apr_file_printf(out
, "\nTime = %" APR_TIME_T_FMT
" seconds\n", timeTaken
);
128 apr_file_printf(out
, "Speed = % " APR_TIME_T_FMT
" bytes/second\n",
129 TEST_BLOCK_LEN
* TEST_BLOCK_COUNT
/timeTaken
);
132 /* Digests a reference suite of strings and prints the results.
134 static void MDTestSuite(void)
136 apr_file_printf(out
, "MD4 test suite:\n");
141 MDString("message digest");
142 MDString("abcdefghijklmnopqrstuvwxyz");
143 MDString("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
144 MDString("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
147 /* Digests a file and prints the result.
149 static void MDFile(char *filename
)
152 apr_md4_ctx_t context
;
153 apr_size_t len
= 1024;
154 unsigned char buffer
[1024], digest
[APR_MD4_DIGESTSIZE
];
156 if (apr_file_open(&file
, filename
, APR_READ
, APR_OS_DEFAULT
, local_pool
)
158 apr_file_printf(err
, "%s can't be opened\n", filename
);
160 apr_md4_init(&context
);
161 while (apr_file_read(file
, buffer
, &len
) != APR_SUCCESS
)
163 apr_md4_update(&context
, buffer
, len
);
166 apr_md4_final(digest
, &context
);
168 apr_file_close(file
);
170 apr_file_printf(out
, "MD4 (%s) = ", filename
);
172 apr_file_printf(out
, "\n");
176 /* Digests the standard input and prints the result.
178 static void MDFilter(void)
180 apr_md4_ctx_t context
;
182 unsigned char buffer
[16], digest
[16];
184 apr_md4_init(&context
);
185 while (apr_file_read(in
, buffer
, &len
) != APR_SUCCESS
)
187 apr_md4_update(&context
, buffer
, len
);
190 apr_md4_update(&context
, buffer
, len
);
191 apr_md4_final(digest
, &context
);
194 apr_file_printf(out
, "\n");
200 Arguments (may be any combination):
201 -sstring - digests string
203 -x - runs test script
204 filename - digests file
205 (none) - digests standard input
207 int main (int argc
, char **argv
)
212 atexit(apr_terminate
);
214 if (apr_pool_create(&local_pool
, NULL
) != APR_SUCCESS
)
217 apr_file_open_stdin(&in
, local_pool
);
218 apr_file_open_stdout(&out
, local_pool
);
219 apr_file_open_stderr(&err
, local_pool
);
223 for (i
= 1; i
< argc
; i
++)
224 if (argv
[i
][0] == '-' && argv
[i
][1] == 's')
225 MDString(argv
[i
] + 2);
226 else if (strcmp(argv
[i
], "-t") == 0)
228 else if (strcmp (argv
[i
], "-x") == 0)