2 **********************************************************************
4 ** RSA Data Security, Inc. MD5 Message Digest Algorithm **
5 ** Created: 2/17/90 RLR **
6 ** Revised: 1/91 SRD,AJ,BSK,JT Reference C Version **
7 **********************************************************************
11 **********************************************************************
12 ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
14 ** License to copy and use this software is granted provided that **
15 ** it is identified as the "RSA Data Security, Inc. MD5 Message **
16 ** Digest Algorithm" in all material mentioning or referencing this **
17 ** software or this function. **
19 ** License is also granted to make and use derivative works **
20 ** provided that such works are identified as "derived from the RSA **
21 ** Data Security, Inc. MD5 Message Digest Algorithm" in all **
22 ** material mentioning or referencing the derived work. **
24 ** RSA Data Security, Inc. makes no representations concerning **
25 ** either the merchantability of this software or the suitability **
26 ** of this software for any particular purpose. It is provided "as **
27 ** is" without express or implied warranty of any kind. **
29 ** These notices must be retained in any copies of any part of this **
30 ** documentation and/or software. **
31 **********************************************************************
36 /* forward declaration */
37 static void Transform ();
39 static unsigned char PADDING[64] = {
40 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
42 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
44 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
50 /* F, G and H are basic MD5 functions: selection, majority, parity */
51 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
52 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
53 #define H(x, y, z) ((x) ^ (y) ^ (z))
54 #define I(x, y, z) ((y) ^ ((x) | (~z)))
56 /* ROTATE_LEFT rotates x left n bits */
57 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
59 /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
60 /* Rotation is separate from addition to prevent recomputation */
61 #define FF(a, b, c, d, x, s, ac) \
62 {(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
63 (a) = ROTATE_LEFT ((a), (s)); \
66 #define GG(a, b, c, d, x, s, ac) \
67 {(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
68 (a) = ROTATE_LEFT ((a), (s)); \
71 #define HH(a, b, c, d, x, s, ac) \
72 {(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
73 (a) = ROTATE_LEFT ((a), (s)); \
76 #define II(a, b, c, d, x, s, ac) \
77 {(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
78 (a) = ROTATE_LEFT ((a), (s)); \
82 void MD5Init (mdContext)
85 mdContext->i[0] = mdContext->i[1] = (UINT4)0;
87 /* Load magic initialization constants.
89 mdContext->buf[0] = (UINT4)0x67452301;
90 mdContext->buf[1] = (UINT4)0xefcdab89;
91 mdContext->buf[2] = (UINT4)0x98badcfe;
92 mdContext->buf[3] = (UINT4)0x10325476;
95 void MD5Update (mdContext, inBuf, inLen)
104 /* compute number of bytes mod 64 */
105 mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
107 /* update number of bits */
108 if ((mdContext->i[0] + ((UINT4)inLen << 3)) < mdContext->i[0])
110 mdContext->i[0] += ((UINT4)inLen << 3);
111 mdContext->i[1] += ((UINT4)inLen >> 29);
114 /* add new character to buffer, increment mdi */
115 mdContext->in[mdi++] = *inBuf++;
117 /* transform if necessary */
119 for (i = 0, ii = 0; i < 16; i++, ii += 4)
120 in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
121 (((UINT4)mdContext->in[ii+2]) << 16) |
122 (((UINT4)mdContext->in[ii+1]) << 8) |
123 ((UINT4)mdContext->in[ii]);
124 Transform (mdContext->buf, in);
130 void MD5Final (mdContext)
138 /* save number of bits */
139 in[14] = mdContext->i[0];
140 in[15] = mdContext->i[1];
142 /* compute number of bytes mod 64 */
143 mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
145 /* pad out to 56 mod 64 */
146 padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
147 MD5Update (mdContext, PADDING, padLen);
149 /* append length in bits and transform */
150 for (i = 0, ii = 0; i < 14; i++, ii += 4)
151 in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
152 (((UINT4)mdContext->in[ii+2]) << 16) |
153 (((UINT4)mdContext->in[ii+1]) << 8) |
154 ((UINT4)mdContext->in[ii]);
155 Transform (mdContext->buf, in);
157 /* store buffer in digest */
158 for (i = 0, ii = 0; i < 4; i++, ii += 4) {
159 mdContext->digest[ii] = (unsigned char)(mdContext->buf[i] & 0xFF);
160 mdContext->digest[ii+1] =
161 (unsigned char)((mdContext->buf[i] >> 8) & 0xFF);
162 mdContext->digest[ii+2] =
163 (unsigned char)((mdContext->buf[i] >> 16) & 0xFF);
164 mdContext->digest[ii+3] =
165 (unsigned char)((mdContext->buf[i] >> 24) & 0xFF);
169 /* Basic MD5 step. Transform buf based on in.
171 static void Transform (buf, in)
175 UINT4 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
182 FF ( a, b, c, d, in[ 0], S11, 3614090360u); /* 1 */
183 FF ( d, a, b, c, in[ 1], S12, 3905402710u); /* 2 */
184 FF ( c, d, a, b, in[ 2], S13, 606105819u); /* 3 */
185 FF ( b, c, d, a, in[ 3], S14, 3250441966u); /* 4 */
186 FF ( a, b, c, d, in[ 4], S11, 4118548399u); /* 5 */
187 FF ( d, a, b, c, in[ 5], S12, 1200080426u); /* 6 */
188 FF ( c, d, a, b, in[ 6], S13, 2821735955u); /* 7 */
189 FF ( b, c, d, a, in[ 7], S14, 4249261313u); /* 8 */
190 FF ( a, b, c, d, in[ 8], S11, 1770035416u); /* 9 */
191 FF ( d, a, b, c, in[ 9], S12, 2336552879u); /* 10 */
192 FF ( c, d, a, b, in[10], S13, 4294925233u); /* 11 */
193 FF ( b, c, d, a, in[11], S14, 2304563134u); /* 12 */
194 FF ( a, b, c, d, in[12], S11, 1804603682u); /* 13 */
195 FF ( d, a, b, c, in[13], S12, 4254626195u); /* 14 */
196 FF ( c, d, a, b, in[14], S13, 2792965006u); /* 15 */
197 FF ( b, c, d, a, in[15], S14, 1236535329u); /* 16 */
204 GG ( a, b, c, d, in[ 1], S21, 4129170786u); /* 17 */
205 GG ( d, a, b, c, in[ 6], S22, 3225465664u); /* 18 */
206 GG ( c, d, a, b, in[11], S23, 643717713u); /* 19 */
207 GG ( b, c, d, a, in[ 0], S24, 3921069994u); /* 20 */
208 GG ( a, b, c, d, in[ 5], S21, 3593408605u); /* 21 */
209 GG ( d, a, b, c, in[10], S22, 38016083u); /* 22 */
210 GG ( c, d, a, b, in[15], S23, 3634488961u); /* 23 */
211 GG ( b, c, d, a, in[ 4], S24, 3889429448u); /* 24 */
212 GG ( a, b, c, d, in[ 9], S21, 568446438u); /* 25 */
213 GG ( d, a, b, c, in[14], S22, 3275163606u); /* 26 */
214 GG ( c, d, a, b, in[ 3], S23, 4107603335u); /* 27 */
215 GG ( b, c, d, a, in[ 8], S24, 1163531501u); /* 28 */
216 GG ( a, b, c, d, in[13], S21, 2850285829u); /* 29 */
217 GG ( d, a, b, c, in[ 2], S22, 4243563512u); /* 30 */
218 GG ( c, d, a, b, in[ 7], S23, 1735328473u); /* 31 */
219 GG ( b, c, d, a, in[12], S24, 2368359562u); /* 32 */
226 HH ( a, b, c, d, in[ 5], S31, 4294588738u); /* 33 */
227 HH ( d, a, b, c, in[ 8], S32, 2272392833u); /* 34 */
228 HH ( c, d, a, b, in[11], S33, 1839030562u); /* 35 */
229 HH ( b, c, d, a, in[14], S34, 4259657740u); /* 36 */
230 HH ( a, b, c, d, in[ 1], S31, 2763975236u); /* 37 */
231 HH ( d, a, b, c, in[ 4], S32, 1272893353u); /* 38 */
232 HH ( c, d, a, b, in[ 7], S33, 4139469664u); /* 39 */
233 HH ( b, c, d, a, in[10], S34, 3200236656u); /* 40 */
234 HH ( a, b, c, d, in[13], S31, 681279174u); /* 41 */
235 HH ( d, a, b, c, in[ 0], S32, 3936430074u); /* 42 */
236 HH ( c, d, a, b, in[ 3], S33, 3572445317u); /* 43 */
237 HH ( b, c, d, a, in[ 6], S34, 76029189u); /* 44 */
238 HH ( a, b, c, d, in[ 9], S31, 3654602809u); /* 45 */
239 HH ( d, a, b, c, in[12], S32, 3873151461u); /* 46 */
240 HH ( c, d, a, b, in[15], S33, 530742520u); /* 47 */
241 HH ( b, c, d, a, in[ 2], S34, 3299628645u); /* 48 */
248 II ( a, b, c, d, in[ 0], S41, 4096336452u); /* 49 */
249 II ( d, a, b, c, in[ 7], S42, 1126891415u); /* 50 */
250 II ( c, d, a, b, in[14], S43, 2878612391u); /* 51 */
251 II ( b, c, d, a, in[ 5], S44, 4237533241u); /* 52 */
252 II ( a, b, c, d, in[12], S41, 1700485571u); /* 53 */
253 II ( d, a, b, c, in[ 3], S42, 2399980690u); /* 54 */
254 II ( c, d, a, b, in[10], S43, 4293915773u); /* 55 */
255 II ( b, c, d, a, in[ 1], S44, 2240044497u); /* 56 */
256 II ( a, b, c, d, in[ 8], S41, 1873313359u); /* 57 */
257 II ( d, a, b, c, in[15], S42, 4264355552u); /* 58 */
258 II ( c, d, a, b, in[ 6], S43, 2734768916u); /* 59 */
259 II ( b, c, d, a, in[13], S44, 1309151649u); /* 60 */
260 II ( a, b, c, d, in[ 4], S41, 4149444226u); /* 61 */
261 II ( d, a, b, c, in[11], S42, 3174756917u); /* 62 */
262 II ( c, d, a, b, in[ 2], S43, 718787259u); /* 63 */
263 II ( b, c, d, a, in[ 9], S44, 3951481745u); /* 64 */
271 /* lh3: the following code is added by me */
277 #define HEX_STR "0123456789abcdef"
279 static void md5_one(const char *fn)
281 unsigned char buf[4096];
286 fp = strcmp(fn, "-")? fopen(fn, "r") : stdin;
288 fprintf(stderr, "md5sum: %s: No such file or directory\n", fn);
292 while ((l = fread(buf, 1, 4096, fp)) > 0)
293 MD5Update(&md5, buf, l);
295 if (fp != stdin) fclose(fp);
296 for (l = 0; l < 16; ++l)
297 printf("%c%c", HEX_STR[md5.digest[l]>>4&0xf], HEX_STR[md5.digest[l]&0xf]);
300 int main(int argc, char *argv[])
303 if (argc == 1) md5_one("-");
304 else for (i = 1; i < argc; ++i) md5_one(argv[i]);