63 unsigned char buffer[64];
67 static void MD5Update(
MD5_CTX *,
unsigned char *,
unsigned int);
68 static void MD5Final(
unsigned char [16],
MD5_CTX *);
116 typedef unsigned char *POINTER;
118 static void MD5Transform(uint32_t [4],
unsigned char [64]);
119 static void Encode(
unsigned char *, uint32_t *,
unsigned int);
120 static void Decode(uint32_t *,
unsigned char *,
unsigned int);
121 static void MD5_memcpy(POINTER, POINTER,
unsigned int);
122 static void MD5_memset(POINTER,
int,
unsigned int);
124 static unsigned char PADDING[64] = {
125 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
126 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
127 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
132 #define F(x, y, z) (((x) & (y)) | ((~x) & (z))) 133 #define G(x, y, z) (((x) & (z)) | ((y) & (~z))) 134 #define H(x, y, z) ((x) ^ (y) ^ (z)) 135 #define I(x, y, z) ((y) ^ ((x) | (~z))) 139 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) 144 #define FF(a, b, c, d, x, s, ac) { \ 145 (a) += F ((b), (c), (d)) + (x) + (uint32_t)(ac); \ 146 (a) = ROTATE_LEFT ((a), (s)); \ 149 #define GG(a, b, c, d, x, s, ac) { \ 150 (a) += G ((b), (c), (d)) + (x) + (uint32_t)(ac); \ 151 (a) = ROTATE_LEFT ((a), (s)); \ 154 #define HH(a, b, c, d, x, s, ac) { \ 155 (a) += H ((b), (c), (d)) + (x) + (uint32_t)(ac); \ 156 (a) = ROTATE_LEFT ((a), (s)); \ 159 #define II(a, b, c, d, x, s, ac) { \ 160 (a) += I ((b), (c), (d)) + (x) + (uint32_t)(ac); \ 161 (a) = ROTATE_LEFT ((a), (s)); \ 167 static void MD5Init (
MD5_CTX *context)
169 context->count[0] = context->count[1] = 0;
172 context->state[0] = 0x67452301;
173 context->state[1] = 0xefcdab89;
174 context->state[2] = 0x98badcfe;
175 context->state[3] = 0x10325476;
182 static void MD5Update (
184 unsigned char *input,
185 unsigned int inputLen)
187 unsigned int i, index, partLen;
190 index = (
unsigned int)((context->count[0] >> 3) & 0x3F);
193 if ((context->count[0] += ((uint32_t)inputLen << 3))
194 < ((uint32_t)inputLen << 3))
196 context->count[1] += ((uint32_t)inputLen >> 29);
198 partLen = 64 - index;
202 if (inputLen >= partLen) {
204 ((POINTER)&context->buffer[index], (POINTER)input, partLen);
205 MD5Transform (context->state, context->buffer);
207 for (i = partLen; i + 63 < inputLen; i += 64)
208 MD5Transform (context->state, &input[i]);
217 ((POINTER)&context->buffer[index], (POINTER)&input[i],
224 static void MD5Final (
225 unsigned char digest[16],
228 unsigned char bits[8];
229 unsigned int index, padLen;
232 Encode (bits, context->count, 8);
236 index = (
unsigned int)((context->count[0] >> 3) & 0x3f);
237 padLen = (index < 56) ? (56 - index) : (120 - index);
238 MD5Update (context, PADDING, padLen);
241 MD5Update (context, bits, 8);
244 Encode (digest, context->state, 16);
248 MD5_memset ((POINTER)context, 0,
sizeof (*context));
253 static void MD5Transform (
255 unsigned char block[64])
257 uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
259 Decode (x, block, 64);
262 FF (a, b, c, d, x[ 0], S11, 0xd76aa478);
263 FF (d, a, b, c, x[ 1], S12, 0xe8c7b756);
264 FF (c, d, a, b, x[ 2], S13, 0x242070db);
265 FF (b, c, d, a, x[ 3], S14, 0xc1bdceee);
266 FF (a, b, c, d, x[ 4], S11, 0xf57c0faf);
267 FF (d, a, b, c, x[ 5], S12, 0x4787c62a);
268 FF (c, d, a, b, x[ 6], S13, 0xa8304613);
269 FF (b, c, d, a, x[ 7], S14, 0xfd469501);
270 FF (a, b, c, d, x[ 8], S11, 0x698098d8);
271 FF (d, a, b, c, x[ 9], S12, 0x8b44f7af);
272 FF (c, d, a, b, x[10], S13, 0xffff5bb1);
273 FF (b, c, d, a, x[11], S14, 0x895cd7be);
274 FF (a, b, c, d, x[12], S11, 0x6b901122);
275 FF (d, a, b, c, x[13], S12, 0xfd987193);
276 FF (c, d, a, b, x[14], S13, 0xa679438e);
277 FF (b, c, d, a, x[15], S14, 0x49b40821);
280 GG (a, b, c, d, x[ 1], S21, 0xf61e2562);
281 GG (d, a, b, c, x[ 6], S22, 0xc040b340);
282 GG (c, d, a, b, x[11], S23, 0x265e5a51);
283 GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa);
284 GG (a, b, c, d, x[ 5], S21, 0xd62f105d);
285 GG (d, a, b, c, x[10], S22, 0x2441453);
286 GG (c, d, a, b, x[15], S23, 0xd8a1e681);
287 GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8);
288 GG (a, b, c, d, x[ 9], S21, 0x21e1cde6);
289 GG (d, a, b, c, x[14], S22, 0xc33707d6);
290 GG (c, d, a, b, x[ 3], S23, 0xf4d50d87);
291 GG (b, c, d, a, x[ 8], S24, 0x455a14ed);
292 GG (a, b, c, d, x[13], S21, 0xa9e3e905);
293 GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8);
294 GG (c, d, a, b, x[ 7], S23, 0x676f02d9);
295 GG (b, c, d, a, x[12], S24, 0x8d2a4c8a);
298 HH (a, b, c, d, x[ 5], S31, 0xfffa3942);
299 HH (d, a, b, c, x[ 8], S32, 0x8771f681);
300 HH (c, d, a, b, x[11], S33, 0x6d9d6122);
301 HH (b, c, d, a, x[14], S34, 0xfde5380c);
302 HH (a, b, c, d, x[ 1], S31, 0xa4beea44);
303 HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9);
304 HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60);
305 HH (b, c, d, a, x[10], S34, 0xbebfbc70);
306 HH (a, b, c, d, x[13], S31, 0x289b7ec6);
307 HH (d, a, b, c, x[ 0], S32, 0xeaa127fa);
308 HH (c, d, a, b, x[ 3], S33, 0xd4ef3085);
309 HH (b, c, d, a, x[ 6], S34, 0x4881d05);
310 HH (a, b, c, d, x[ 9], S31, 0xd9d4d039);
311 HH (d, a, b, c, x[12], S32, 0xe6db99e5);
312 HH (c, d, a, b, x[15], S33, 0x1fa27cf8);
313 HH (b, c, d, a, x[ 2], S34, 0xc4ac5665);
316 II (a, b, c, d, x[ 0], S41, 0xf4292244);
317 II (d, a, b, c, x[ 7], S42, 0x432aff97);
318 II (c, d, a, b, x[14], S43, 0xab9423a7);
319 II (b, c, d, a, x[ 5], S44, 0xfc93a039);
320 II (a, b, c, d, x[12], S41, 0x655b59c3);
321 II (d, a, b, c, x[ 3], S42, 0x8f0ccc92);
322 II (c, d, a, b, x[10], S43, 0xffeff47d);
323 II (b, c, d, a, x[ 1], S44, 0x85845dd1);
324 II (a, b, c, d, x[ 8], S41, 0x6fa87e4f);
325 II (d, a, b, c, x[15], S42, 0xfe2ce6e0);
326 II (c, d, a, b, x[ 6], S43, 0xa3014314);
327 II (b, c, d, a, x[13], S44, 0x4e0811a1);
328 II (a, b, c, d, x[ 4], S41, 0xf7537e82);
329 II (d, a, b, c, x[11], S42, 0xbd3af235);
330 II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb);
331 II (b, c, d, a, x[ 9], S44, 0xeb86d391);
340 MD5_memset ((POINTER)x, 0,
sizeof (x));
347 unsigned char *output,
353 for (i = 0, j = 0; j < len; i++, j += 4) {
354 output[j] = (
unsigned char)(input[i] & 0xff);
355 output[j+1] = (
unsigned char)((input[i] >> 8) & 0xff);
356 output[j+2] = (
unsigned char)((input[i] >> 16) & 0xff);
357 output[j+3] = (
unsigned char)((input[i] >> 24) & 0xff);
366 unsigned char *input,
371 for (i = 0, j = 0; j < len; i++, j += 4)
372 output[i] = ((uint32_t)input[j]) | (((uint32_t)input[j+1]) << 8) |
373 (((uint32_t)input[j+2]) << 16) | (((uint32_t)input[j+3]) << 24);
379 static void MD5_memcpy (
386 for (i = 0; i < len; i++)
387 output[i] = input[i];
392 static void MD5_memset (
399 for (i = 0; i < len; i++)
400 ((
char *)output)[i] = (char)value;