beta-0.89.2
[luatex.git] / source / texk / web2c / libmd5 / md5main.c
blob625a6198e6d6e6fe95ecf16ce227ae080d17a57a
1 /*
2 Copyright (C) 2002 Aladdin Enterprises. All rights reserved.
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
20 L. Peter Deutsch
21 ghost@aladdin.com
24 /* $Id: md5main.c,v 1.1 2002/04/13 19:20:28 lpd Exp $ */
26 Independent implementation of MD5 (RFC 1321).
28 This code implements the MD5 Algorithm defined in RFC 1321, whose
29 text is available at
30 http://www.ietf.org/rfc/rfc1321.txt
31 The code is derived from the text of the RFC, including the test suite
32 (section A.5) but excluding the rest of Appendix A. It does not include
33 any code or documentation that is identified in the RFC as being
34 copyrighted.
36 The original and principal author of md5.c is L. Peter Deutsch
37 <ghost@aladdin.com>. Other authors are noted in the change history
38 that follows (in reverse chronological order):
40 2002-04-13 lpd Splits off main program into a separate file, md5main.c.
43 #include "md5.h"
44 #include <math.h>
45 #include <stdio.h>
46 #include <string.h>
49 * This file builds an executable that performs various functions related
50 * to the MD5 library. Typical compilation:
51 * gcc -o md5main -lm md5main.c md5.c
53 static const char *const usage = "\
54 Usage:\n\
55 md5main --test # run the self-test (A.5 of RFC 1321)\n\
56 md5main --t-values # print the T values for the library\n\
57 md5main --version # print the version of the package\n\
59 static const char *const version = "2002-04-13";
61 /* Run the self-test. */
62 static int
63 do_test(void)
65 static const char *const test[7*2] = {
66 "", "d41d8cd98f00b204e9800998ecf8427e",
67 "a", "0cc175b9c0f1b6a831c399e269772661",
68 "abc", "900150983cd24fb0d6963f7d28e17f72",
69 "message digest", "f96b697d7cb7938d525a2f31aaf161d0",
70 "abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b",
71 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
72 "d174ab98d277d9f5a5611c2c9f419d9f",
73 "12345678901234567890123456789012345678901234567890123456789012345678901234567890", "57edf4a22be3c955ac49da2e2107b67a"
75 int i;
76 int status = 0;
78 for (i = 0; i < 7*2; i += 2) {
79 md5_state_t state;
80 md5_byte_t digest[16];
81 char hex_output[16*2 + 1];
82 int di;
84 md5_init(&state);
85 md5_append(&state, (const md5_byte_t *)test[i], strlen(test[i]));
86 md5_finish(&state, digest);
87 for (di = 0; di < 16; ++di)
88 sprintf(hex_output + di * 2, "%02x", digest[di]);
89 if (strcmp(hex_output, test[i + 1])) {
90 printf("MD5 (\"%s\") = ", test[i]);
91 puts(hex_output);
92 printf("**** ERROR, should be: %s\n", test[i + 1]);
93 status = 1;
96 if (status == 0)
97 puts("md5 self-test completed successfully.");
98 return status;
101 /* Print the T values. */
102 static int
103 do_t_values(void)
105 int i;
106 for (i = 1; i <= 64; ++i) {
107 unsigned long v = (unsigned long)(4294967296.0 * fabs(sin((double)i)));
110 * The following nonsense is only to avoid compiler warnings about
111 * "integer constant is unsigned in ANSI C, signed with -traditional".
113 if (v >> 31) {
114 printf("#define T%d /* 0x%08lx */ (T_MASK ^ 0x%08lx)\n", i,
115 v, (unsigned long)(unsigned int)(~v));
116 } else {
117 printf("#define T%d 0x%08lx\n", i, v);
120 return 0;
123 /* Main program */
125 main(int argc, char *argv[])
127 if (argc == 2) {
128 if (!strcmp(argv[1], "--test"))
129 return do_test();
130 if (!strcmp(argv[1], "--t-values"))
131 return do_t_values();
132 if (!strcmp(argv[1], "--version")) {
133 puts(version);
134 return 0;
137 puts(usage);
138 return 0;