39 #include "blocxx/BLOCXX_config.h"
47 #pragma warning (push)
48 #pragma warning (disable: 4355)
51 namespace BLOCXX_NAMESPACE
74 reinterpret_cast<const unsigned char*>(s), num);
79 :
MD5OStreamBase(this), std::ostream(&_buf), m_ctx(), m_finished(false)
93 :
MD5OStreamBase(this), std::ostream(&_buf), m_ctx(), m_finished(false)
135 j = (sBin[
i] >> 4) & 0xf;
138 Hex[i*2] = (j +
'0');
142 Hex[i*2] = (j +
'a' - 10);
147 Hex[i*2+1] = (j +
'0');
151 Hex[i*2+1] = (j +
'a' - 10);
178 typedef unsigned char *
POINTER;
197 static void MD5Transform(UInt32*,
const unsigned char*);
198 static void Encode(
unsigned char *, UInt32 *, UInt32);
199 static void Decode(UInt32 *,
const unsigned char *, UInt32);
203 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
204 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
205 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
209 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
210 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
211 #define H(x, y, z) ((x) ^ (y) ^ (z))
212 #define I(x, y, z) ((y) ^ ((x) | (~z)))
215 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
219 #define FF(a, b, c, d, x, s, ac) { \
220 (a) += F ((b), (c), (d)) + (x) + static_cast<UInt32>(ac); \
221 (a) = ROTATE_LEFT ((a), (s)); \
224 #define GG(a, b, c, d, x, s, ac) { \
225 (a) += G ((b), (c), (d)) + (x) + static_cast<UInt32>(ac); \
226 (a) = ROTATE_LEFT ((a), (s)); \
229 #define HH(a, b, c, d, x, s, ac) { \
230 (a) += H ((b), (c), (d)) + (x) + static_cast<UInt32>(ac); \
231 (a) = ROTATE_LEFT ((a), (s)); \
234 #define II(a, b, c, d, x, s, ac) { \
235 (a) += I ((b), (c), (d)) + (x) + static_cast<UInt32>(ac); \
236 (a) = ROTATE_LEFT ((a), (s)); \
247 context->
state[0] = 0x67452301;
248 context->
state[1] = 0xefcdab89;
249 context->
state[2] = 0x98badcfe;
250 context->
state[3] = 0x10325476;
260 UInt32
i, index, partLen;
262 index = ((context->
count[0] >> 3) & 0x3F);
264 if ((context->
count[0] += (inputLen << 3)) < (inputLen << 3))
268 context->
count[1] += (inputLen >> 29);
269 partLen = 64 - index;
272 if (inputLen >= partLen)
274 memcpy(static_cast<POINTER>(&context->
buffer[index]),
275 static_cast<const unsigned char*>(input), partLen);
277 for (i = partLen; i + 63 < inputLen; i += 64)
285 (static_cast<POINTER>(&context->
buffer[index]),
286 static_cast<const unsigned char*>(&input[i]),
295 unsigned char bits[8];
296 UInt32 index, padLen;
301 index = ((context->
count[0] >> 3) & 0x3f);
302 padLen = (index < 56) ? (56 - index) : (120 - index);
310 memset (reinterpret_cast<POINTER>(context), 0,
sizeof (*context));
316 UInt32 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
319 FF (a, b, c, d, x[ 0],
S11, 0xd76aa478);
320 FF (d, a, b, c, x[ 1],
S12, 0xe8c7b756);
321 FF (c, d, a, b, x[ 2],
S13, 0x242070db);
322 FF (b, c, d, a, x[ 3],
S14, 0xc1bdceee);
323 FF (a, b, c, d, x[ 4],
S11, 0xf57c0faf);
324 FF (d, a, b, c, x[ 5],
S12, 0x4787c62a);
325 FF (c, d, a, b, x[ 6],
S13, 0xa8304613);
326 FF (b, c, d, a, x[ 7],
S14, 0xfd469501);
327 FF (a, b, c, d, x[ 8],
S11, 0x698098d8);
328 FF (d, a, b, c, x[ 9],
S12, 0x8b44f7af);
329 FF (c, d, a, b, x[10],
S13, 0xffff5bb1);
330 FF (b, c, d, a, x[11],
S14, 0x895cd7be);
331 FF (a, b, c, d, x[12],
S11, 0x6b901122);
332 FF (d, a, b, c, x[13],
S12, 0xfd987193);
333 FF (c, d, a, b, x[14],
S13, 0xa679438e);
334 FF (b, c, d, a, x[15],
S14, 0x49b40821);
336 GG (a, b, c, d, x[ 1],
S21, 0xf61e2562);
337 GG (d, a, b, c, x[ 6],
S22, 0xc040b340);
338 GG (c, d, a, b, x[11],
S23, 0x265e5a51);
339 GG (b, c, d, a, x[ 0],
S24, 0xe9b6c7aa);
340 GG (a, b, c, d, x[ 5],
S21, 0xd62f105d);
341 GG (d, a, b, c, x[10],
S22, 0x2441453);
342 GG (c, d, a, b, x[15],
S23, 0xd8a1e681);
343 GG (b, c, d, a, x[ 4],
S24, 0xe7d3fbc8);
344 GG (a, b, c, d, x[ 9],
S21, 0x21e1cde6);
345 GG (d, a, b, c, x[14],
S22, 0xc33707d6);
346 GG (c, d, a, b, x[ 3],
S23, 0xf4d50d87);
347 GG (b, c, d, a, x[ 8],
S24, 0x455a14ed);
348 GG (a, b, c, d, x[13],
S21, 0xa9e3e905);
349 GG (d, a, b, c, x[ 2],
S22, 0xfcefa3f8);
350 GG (c, d, a, b, x[ 7],
S23, 0x676f02d9);
351 GG (b, c, d, a, x[12],
S24, 0x8d2a4c8a);
353 HH (a, b, c, d, x[ 5],
S31, 0xfffa3942);
354 HH (d, a, b, c, x[ 8],
S32, 0x8771f681);
355 HH (c, d, a, b, x[11],
S33, 0x6d9d6122);
356 HH (b, c, d, a, x[14],
S34, 0xfde5380c);
357 HH (a, b, c, d, x[ 1],
S31, 0xa4beea44);
358 HH (d, a, b, c, x[ 4],
S32, 0x4bdecfa9);
359 HH (c, d, a, b, x[ 7],
S33, 0xf6bb4b60);
360 HH (b, c, d, a, x[10],
S34, 0xbebfbc70);
361 HH (a, b, c, d, x[13],
S31, 0x289b7ec6);
362 HH (d, a, b, c, x[ 0],
S32, 0xeaa127fa);
363 HH (c, d, a, b, x[ 3],
S33, 0xd4ef3085);
364 HH (b, c, d, a, x[ 6],
S34, 0x4881d05);
365 HH (a, b, c, d, x[ 9],
S31, 0xd9d4d039);
366 HH (d, a, b, c, x[12],
S32, 0xe6db99e5);
367 HH (c, d, a, b, x[15],
S33, 0x1fa27cf8);
368 HH (b, c, d, a, x[ 2],
S34, 0xc4ac5665);
370 II (a, b, c, d, x[ 0],
S41, 0xf4292244);
371 II (d, a, b, c, x[ 7],
S42, 0x432aff97);
372 II (c, d, a, b, x[14],
S43, 0xab9423a7);
373 II (b, c, d, a, x[ 5],
S44, 0xfc93a039);
374 II (a, b, c, d, x[12],
S41, 0x655b59c3);
375 II (d, a, b, c, x[ 3],
S42, 0x8f0ccc92);
376 II (c, d, a, b, x[10],
S43, 0xffeff47d);
377 II (b, c, d, a, x[ 1],
S44, 0x85845dd1);
378 II (a, b, c, d, x[ 8],
S41, 0x6fa87e4f);
379 II (d, a, b, c, x[15],
S42, 0xfe2ce6e0);
380 II (c, d, a, b, x[ 6],
S43, 0xa3014314);
381 II (b, c, d, a, x[13],
S44, 0x4e0811a1);
382 II (a, b, c, d, x[ 4],
S41, 0xf7537e82);
383 II (d, a, b, c, x[11],
S42, 0xbd3af235);
384 II (c, d, a, b, x[ 2],
S43, 0x2ad7d2bb);
385 II (b, c, d, a, x[ 9],
S44, 0xeb86d391);
391 memset (reinterpret_cast<POINTER>(x), 0,
sizeof (x));
396 static void Encode (
unsigned char* output, UInt32* input, UInt32 len)
399 for (i = 0, j = 0; j < len; i++, j += 4)
401 output[j] = (input[
i] & 0xff);
402 output[j+1] = ((input[
i] >> 8) & 0xff);
403 output[j+2] = ((input[
i] >> 16) & 0xff);
404 output[j+3] = ((input[
i] >> 24) & 0xff);
410 static void Decode (UInt32* output,
const unsigned char* input, UInt32 len)
413 for (i = 0, j = 0; j < len; i++, j += 4)
414 output[i] = (static_cast<UInt32>(input[j])) | ((
static_cast<UInt32
>(input[j+1])) << 8) |
415 ((static_cast<UInt32>(input[j+2])) << 16) | ((
static_cast<UInt32
>(input[j+3])) << 24);
437 #pragma warning (pop)