10#if __DCL_HAVE_ALLOC_DEBUG
11#undef __DCL_ALLOC_LEVEL
12#define __DCL_ALLOC_LEVEL __DCL_ALLOC_INTERNAL
29typedef unsigned short int UINT2;
47static void MD5Update(
MD5_CTX *,
const unsigned char *,
size_t);
48static void MD5Final(
unsigned char [16],
MD5_CTX *);
50#if __DCL_HAVE_THIS_FILE__
59 __context = malloc(
sizeof(
MD5_CTX));
72 MD5Update((
MD5_CTX*)__context, (
const unsigned char*)_p, _n);
77 MD5Final(digest, (
MD5_CTX*)__context);
85 return String::toHexString((
const char*) digest,
sizeof(digest), (
size_t)-1,
false);
91 return MD5::final(_s.data(), _s.length() *
sizeof(
wchar_t));
96 return MD5::final(_s.data(), _s.length() *
sizeof(
char));
155static void MD5Transform(
UINT4 state[4],
const unsigned char block[64]);
156static void Encode(
unsigned char* output,
UINT4* input,
size_t len);
157static void Decode(
UINT4* output,
const unsigned char* input,
size_t len);
164static unsigned char PADDING[64] = {
165 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
166 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
167 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
172#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
173#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
174#define H(x, y, z) ((x) ^ (y) ^ (z))
175#define I(x, y, z) ((y) ^ ((x) | (~z)))
178#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
182#define FF(a, b, c, d, x, s, ac) { \
183 (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
184 (a) = ROTATE_LEFT ((a), (s)); \
187#define GG(a, b, c, d, x, s, ac) { \
188 (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
189 (a) = ROTATE_LEFT ((a), (s)); \
192#define HH(a, b, c, d, x, s, ac) { \
193 (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
194 (a) = ROTATE_LEFT ((a), (s)); \
197#define II(a, b, c, d, x, s, ac) { \
198 (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
199 (a) = ROTATE_LEFT ((a), (s)); \
204static void MD5Init(
MD5_CTX *context)
208 context->
state[0] = 0x67452301;
209 context->
state[1] = 0xefcdab89;
210 context->
state[2] = 0x98badcfe;
211 context->
state[3] = 0x10325476;
218static void MD5Update(
MD5_CTX* context,
const unsigned char* input,
size_t inputLen)
224 size_t i, index, partLen;
227 index = (size_t)((context->
count[0] >> 3) & 0x3F);
230 if ((context->
count[0] += ((
UINT4)inputLen << 3)) < ((
UINT4)inputLen << 3))
234 partLen = 64 - index;
237 if (inputLen >= partLen)
244 for (i = partLen; i + 63 < inputLen; i += 64)
245 MD5Transform (context->
state, &input[i]);
264static void MD5Final(
unsigned char digest[16],
MD5_CTX* context)
266 unsigned char bits[8];
267 size_t index, padLen;
270 Encode (bits, context->
count, 8);
273 index = (size_t)((context->
count[0] >> 3) & 0x3f);
274 padLen = (index < 56) ? (56 - index) : (120 - index);
275 MD5Update (context, PADDING, padLen);
278 MD5Update (context, bits, 8);
281 Encode (digest, context->
state, 16);
285 memset((
POINTER)context, 0,
sizeof (*context));
293static void MD5Transform(
UINT4 state[4],
const unsigned char block[64])
295 UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
297 Decode (x, block, 64);
300 FF (a, b, c, d, x[ 0],
S11, 0xd76aa478);
301 FF (d, a, b, c, x[ 1],
S12, 0xe8c7b756);
302 FF (c, d, a, b, x[ 2],
S13, 0x242070db);
303 FF (b, c, d, a, x[ 3],
S14, 0xc1bdceee);
304 FF (a, b, c, d, x[ 4],
S11, 0xf57c0faf);
305 FF (d, a, b, c, x[ 5],
S12, 0x4787c62a);
306 FF (c, d, a, b, x[ 6],
S13, 0xa8304613);
307 FF (b, c, d, a, x[ 7],
S14, 0xfd469501);
308 FF (a, b, c, d, x[ 8],
S11, 0x698098d8);
309 FF (d, a, b, c, x[ 9],
S12, 0x8b44f7af);
310 FF (c, d, a, b, x[10],
S13, 0xffff5bb1);
311 FF (b, c, d, a, x[11],
S14, 0x895cd7be);
312 FF (a, b, c, d, x[12],
S11, 0x6b901122);
313 FF (d, a, b, c, x[13],
S12, 0xfd987193);
314 FF (c, d, a, b, x[14],
S13, 0xa679438e);
315 FF (b, c, d, a, x[15],
S14, 0x49b40821);
318 GG (a, b, c, d, x[ 1],
S21, 0xf61e2562);
319 GG (d, a, b, c, x[ 6],
S22, 0xc040b340);
320 GG (c, d, a, b, x[11],
S23, 0x265e5a51);
321 GG (b, c, d, a, x[ 0],
S24, 0xe9b6c7aa);
322 GG (a, b, c, d, x[ 5],
S21, 0xd62f105d);
323 GG (d, a, b, c, x[10],
S22, 0x2441453);
324 GG (c, d, a, b, x[15],
S23, 0xd8a1e681);
325 GG (b, c, d, a, x[ 4],
S24, 0xe7d3fbc8);
326 GG (a, b, c, d, x[ 9],
S21, 0x21e1cde6);
327 GG (d, a, b, c, x[14],
S22, 0xc33707d6);
328 GG (c, d, a, b, x[ 3],
S23, 0xf4d50d87);
330 GG (b, c, d, a, x[ 8],
S24, 0x455a14ed);
331 GG (a, b, c, d, x[13],
S21, 0xa9e3e905);
332 GG (d, a, b, c, x[ 2],
S22, 0xfcefa3f8);
333 GG (c, d, a, b, x[ 7],
S23, 0x676f02d9);
334 GG (b, c, d, a, x[12],
S24, 0x8d2a4c8a);
337 HH (a, b, c, d, x[ 5],
S31, 0xfffa3942);
338 HH (d, a, b, c, x[ 8],
S32, 0x8771f681);
339 HH (c, d, a, b, x[11],
S33, 0x6d9d6122);
340 HH (b, c, d, a, x[14],
S34, 0xfde5380c);
341 HH (a, b, c, d, x[ 1],
S31, 0xa4beea44);
342 HH (d, a, b, c, x[ 4],
S32, 0x4bdecfa9);
343 HH (c, d, a, b, x[ 7],
S33, 0xf6bb4b60);
344 HH (b, c, d, a, x[10],
S34, 0xbebfbc70);
345 HH (a, b, c, d, x[13],
S31, 0x289b7ec6);
346 HH (d, a, b, c, x[ 0],
S32, 0xeaa127fa);
347 HH (c, d, a, b, x[ 3],
S33, 0xd4ef3085);
348 HH (b, c, d, a, x[ 6],
S34, 0x4881d05);
349 HH (a, b, c, d, x[ 9],
S31, 0xd9d4d039);
350 HH (d, a, b, c, x[12],
S32, 0xe6db99e5);
351 HH (c, d, a, b, x[15],
S33, 0x1fa27cf8);
352 HH (b, c, d, a, x[ 2],
S34, 0xc4ac5665);
355 II (a, b, c, d, x[ 0],
S41, 0xf4292244);
356 II (d, a, b, c, x[ 7],
S42, 0x432aff97);
357 II (c, d, a, b, x[14],
S43, 0xab9423a7);
358 II (b, c, d, a, x[ 5],
S44, 0xfc93a039);
359 II (a, b, c, d, x[12],
S41, 0x655b59c3);
360 II (d, a, b, c, x[ 3],
S42, 0x8f0ccc92);
361 II (c, d, a, b, x[10],
S43, 0xffeff47d);
362 II (b, c, d, a, x[ 1],
S44, 0x85845dd1);
363 II (a, b, c, d, x[ 8],
S41, 0x6fa87e4f);
364 II (d, a, b, c, x[15],
S42, 0xfe2ce6e0);
365 II (c, d, a, b, x[ 6],
S43, 0xa3014314);
366 II (b, c, d, a, x[13],
S44, 0x4e0811a1);
367 II (a, b, c, d, x[ 4],
S41, 0xf7537e82);
368 II (d, a, b, c, x[11],
S42, 0xbd3af235);
369 II (c, d, a, b, x[ 2],
S43, 0x2ad7d2bb);
370 II (b, c, d, a, x[ 9],
S44, 0xeb86d391);
379 memset((
POINTER)x, 0,
sizeof (x));
388static void Encode(
unsigned char* output,
UINT4* input,
size_t len)
392 for (i = 0, j = 0; j < len; i++, j += 4)
394 output[j] = (
unsigned char)(input[i] & 0xff);
395 output[j+1] = (
unsigned char)((input[i] >> 8) & 0xff);
396 output[j+2] = (
unsigned char)((input[i] >> 16) & 0xff);
397 output[j+3] = (
unsigned char)((input[i] >> 24) & 0xff);
407static void Decode(
UINT4* output,
const unsigned char* input,
size_t len)
411 for (i = 0, j = 0; j < len; i++, j += 4)
412 output[i] = ((
UINT4)input[j]) | (((
UINT4)input[j+1]) << 8) |
413 (((
UINT4)input[j+2]) << 16) | (((
UINT4)input[j+3]) << 24);
#define FF(a, b, c, d, x, s, ac)
#define GG(a, b, c, d, x, s, ac)
#define HH(a, b, c, d, x, s, ac)
#define II(a, b, c, d, x, s, ac)
#define __DCL_ASSERT(expr)
void update(const void *_p, size_t _n)
void final(byte_t digest[16])