Commit | Line | Data |
---|---|---|
2a905848 RG |
1 | |
2 | ||
3 | /* | |
4 | *********************************************************************** | |
5 | ** md5driver.c -- sample test routines ** | |
6 | ** RSA Data Security, Inc. MD5 Message-Digest Algorithm ** | |
7 | ** Created: 2/16/90 RLR ** | |
8 | ** Updated: 1/91 SRD ** | |
9 | *********************************************************************** | |
10 | */ | |
11 | ||
12 | /* | |
13 | *********************************************************************** | |
14 | ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. ** | |
15 | ** ** | |
16 | ** RSA Data Security, Inc. makes no representations concerning ** | |
17 | ** either the merchantability of this software or the suitability ** | |
18 | ** of this software for any particular purpose. It is provided "as ** | |
19 | ** is" without express or implied warranty of any kind. ** | |
20 | ** ** | |
21 | ** These notices must be retained in any copies of any part of this ** | |
22 | ** documentation and/or software. ** | |
23 | *********************************************************************** | |
24 | */ | |
25 | ||
26 | #include <stdio.h> | |
27 | #include <sys/types.h> | |
28 | #include <time.h> | |
29 | #include <string.h> | |
30 | #include "md5.h" | |
31 | ||
32 | /* Prints message digest buffer in mdContext as 32 hexadecimal digits. | |
33 | Order is from low-order byte to high-order byte of digest. | |
34 | Each byte is printed with high-order hexadecimal digit first. | |
35 | */ | |
36 | static void MDPrint (mdContext) | |
37 | MD5_CTX *mdContext; | |
38 | { | |
39 | int i; | |
40 | ||
41 | for (i = 0; i < 16; i++) | |
42 | printf ("%02x", mdContext->digest[i]); | |
43 | } | |
44 | ||
45 | /* size of test block */ | |
46 | #define TEST_BLOCK_SIZE 1000 | |
47 | ||
48 | /* number of blocks to process */ | |
49 | #define TEST_BLOCKS 10000 | |
50 | ||
51 | /* number of test bytes = TEST_BLOCK_SIZE * TEST_BLOCKS */ | |
52 | static long TEST_BYTES = (long)TEST_BLOCK_SIZE * (long)TEST_BLOCKS; | |
53 | ||
54 | /* A time trial routine, to measure the speed of MD5. | |
55 | Measures wall time required to digest TEST_BLOCKS * TEST_BLOCK_SIZE | |
56 | characters. | |
57 | */ | |
58 | static void MDTimeTrial () | |
59 | { | |
60 | MD5_CTX mdContext; | |
61 | time_t endTime, startTime; | |
62 | unsigned char data[TEST_BLOCK_SIZE]; | |
63 | unsigned int i; | |
64 | ||
65 | /* initialize test data */ | |
66 | for (i = 0; i < TEST_BLOCK_SIZE; i++) | |
67 | data[i] = (unsigned char)(i & 0xFF); | |
68 | ||
69 | /* start timer */ | |
70 | printf ("MD5 time trial. Processing %ld characters...\n", TEST_BYTES); | |
71 | time (&startTime); | |
72 | ||
73 | /* digest data in TEST_BLOCK_SIZE byte blocks */ | |
74 | MD5Init (&mdContext); | |
75 | for (i = TEST_BLOCKS; i > 0; i--) | |
76 | MD5Update (&mdContext, data, TEST_BLOCK_SIZE); | |
77 | MD5Final (&mdContext); | |
78 | ||
79 | /* stop timer, get time difference */ | |
80 | time (&endTime); | |
81 | MDPrint (&mdContext); | |
82 | printf (" is digest of test input.\n"); | |
83 | printf | |
84 | ("Seconds to process test input: %ld\n", (long)(endTime-startTime)); | |
85 | printf | |
86 | ("Characters processed per second: %ld\n", | |
87 | TEST_BYTES/(endTime-startTime)); | |
88 | } | |
89 | ||
90 | /* Computes the message digest for string inString. | |
91 | Prints out message digest, a space, the string (in quotes) and a | |
92 | carriage return. | |
93 | */ | |
94 | static void MDString (inString) | |
95 | char *inString; | |
96 | { | |
97 | MD5_CTX mdContext; | |
98 | unsigned int len = strlen (inString); | |
99 | ||
100 | MD5Init (&mdContext); | |
101 | MD5Update (&mdContext, inString, len); | |
102 | MD5Final (&mdContext); | |
103 | MDPrint (&mdContext); | |
104 | printf (" \"%s\"\n", inString); | |
105 | } | |
106 | ||
107 | static void MDFile (filename) | |
108 | char *filename; | |
109 | { | |
110 | FILE *inFile = fopen (filename, "rb"); | |
111 | MD5_CTX mdContext; | |
112 | int bytes; | |
113 | unsigned char data[1024]; | |
114 | ||
115 | if (inFile == NULL) { | |
116 | printf ("%s can't be opened.\n", filename); | |
117 | return; | |
118 | } | |
119 | ||
120 | MD5Init (&mdContext); | |
121 | while ((bytes = fread (data, 1, 1024, inFile)) != 0) | |
122 | MD5Update (&mdContext, data, bytes); | |
123 | MD5Final (&mdContext); | |
124 | MDPrint (&mdContext); | |
125 | printf (" %s\n", filename); | |
126 | fclose (inFile); | |
127 | } | |
128 | ||
129 | /* Writes the message digest of the data from stdin onto stdout, | |
130 | followed by a carriage return. | |
131 | */ | |
132 | static void MDFilter () | |
133 | { | |
134 | MD5_CTX mdContext; | |
135 | int bytes; | |
136 | unsigned char data[16]; | |
137 | ||
138 | MD5Init (&mdContext); | |
139 | while ((bytes = fread (data, 1, 16, stdin)) != 0) | |
140 | MD5Update (&mdContext, data, bytes); | |
141 | MD5Final (&mdContext); | |
142 | MDPrint (&mdContext); | |
143 | printf ("\n"); | |
144 | } | |
145 | ||
146 | /* Runs a standard suite of test data. | |
147 | */ | |
148 | static void MDTestSuite () | |
149 | { | |
150 | printf ("MD5 test suite results:\n"); | |
151 | MDString (""); | |
152 | MDString ("a"); | |
153 | MDString ("abc"); | |
154 | MDString ("message digest"); | |
155 | MDString ("abcdefghijklmnopqrstuvwxyz"); | |
156 | MDString | |
157 | ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); | |
158 | MDString | |
159 | ("1234567890123456789012345678901234567890\ | |
160 | 1234567890123456789012345678901234567890"); | |
161 | /* Contents of file foo are "abc" */ | |
162 | MDFile ("foo"); | |
163 | } | |
164 | ||
165 | void main (argc, argv) | |
166 | int argc; | |
167 | char *argv[]; | |
168 | { | |
169 | int i; | |
170 | ||
171 | /* For each command line argument in turn: | |
172 | ** filename -- prints message digest and name of file | |
173 | ** -sstring -- prints message digest and contents of string | |
174 | ** -t -- prints time trial statistics for 10M | |
175 | characters | |
176 | ** -x -- execute a standard suite of test data | |
177 | ** (no args) -- writes messages digest of stdin onto stdout | |
178 | */ | |
179 | if (argc == 1) | |
180 | MDFilter (); | |
181 | else | |
182 | for (i = 1; i < argc; i++) | |
183 | if (argv[i][0] == '-' && argv[i][1] == 's') | |
184 | MDString (argv[i] + 2); | |
185 | else if (strcmp (argv[i], "-t") == 0) | |
186 | MDTimeTrial (); | |
187 | else if (strcmp (argv[i], "-x") == 0) | |
188 | MDTestSuite (); | |
189 | else MDFile (argv[i]); | |
190 | } | |
191 | ||
192 | /* | |
193 | *********************************************************************** | |
194 | ** End of md5driver.c ** | |
195 | ******************************** (cut) ******************************** | |
196 | */ |