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));
156static void MD5Transform(
UINT4 state[4],
const unsigned char block[64]);
157static void Encode(
unsigned char* output,
UINT4* input,
size_t len);
158static void Decode(
UINT4* output,
const unsigned char* input,
size_t len);
165static unsigned char PADDING[64] = {
166 0x80, 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, 0, 0, 0, 0,
168 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
173#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
174#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
175#define H(x, y, z) ((x) ^ (y) ^ (z))
176#define I(x, y, z) ((y) ^ ((x) | (~z)))
179#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
183#define FF(a, b, c, d, x, s, ac) { \
184 (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
185 (a) = ROTATE_LEFT ((a), (s)); \
188#define GG(a, b, c, d, x, s, ac) { \
189 (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
190 (a) = ROTATE_LEFT ((a), (s)); \
193#define HH(a, b, c, d, x, s, ac) { \
194 (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
195 (a) = ROTATE_LEFT ((a), (s)); \
198#define II(a, b, c, d, x, s, ac) { \
199 (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
200 (a) = ROTATE_LEFT ((a), (s)); \
205static void MD5Init(
MD5_CTX *context)
209 context->
state[0] = 0x67452301;
210 context->
state[1] = 0xefcdab89;
211 context->
state[2] = 0x98badcfe;
212 context->
state[3] = 0x10325476;
219static void MD5Update(
MD5_CTX* context,
const unsigned char* input,
size_t inputLen)
225 size_t i, index, partLen;
228 index = (size_t)((context->
count[0] >> 3) & 0x3F);
231 if ((context->
count[0] += ((
UINT4)inputLen << 3)) < ((
UINT4)inputLen << 3))
235 partLen = 64 - index;
238 if (inputLen >= partLen)
245 for (i = partLen; i + 63 < inputLen; i += 64)
246 MD5Transform (context->
state, &input[i]);
265static void MD5Final(
unsigned char digest[16],
MD5_CTX* context)
267 unsigned char bits[8];
268 size_t index, padLen;
271 Encode (bits, context->
count, 8);
274 index = (size_t)((context->
count[0] >> 3) & 0x3f);
275 padLen = (index < 56) ? (56 - index) : (120 - index);
276 MD5Update (context, PADDING, padLen);
279 MD5Update (context, bits, 8);
282 Encode (digest, context->
state, 16);
286 memset((
POINTER)context, 0,
sizeof (*context));
294static void MD5Transform(
UINT4 state[4],
const unsigned char block[64])
296 UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
298 Decode (x, block, 64);
301 FF (a, b, c, d, x[ 0],
S11, 0xd76aa478);
302 FF (d, a, b, c, x[ 1],
S12, 0xe8c7b756);
303 FF (c, d, a, b, x[ 2],
S13, 0x242070db);
304 FF (b, c, d, a, x[ 3],
S14, 0xc1bdceee);
305 FF (a, b, c, d, x[ 4],
S11, 0xf57c0faf);
306 FF (d, a, b, c, x[ 5],
S12, 0x4787c62a);
307 FF (c, d, a, b, x[ 6],
S13, 0xa8304613);
308 FF (b, c, d, a, x[ 7],
S14, 0xfd469501);
309 FF (a, b, c, d, x[ 8],
S11, 0x698098d8);
310 FF (d, a, b, c, x[ 9],
S12, 0x8b44f7af);
311 FF (c, d, a, b, x[10],
S13, 0xffff5bb1);
312 FF (b, c, d, a, x[11],
S14, 0x895cd7be);
313 FF (a, b, c, d, x[12],
S11, 0x6b901122);
314 FF (d, a, b, c, x[13],
S12, 0xfd987193);
315 FF (c, d, a, b, x[14],
S13, 0xa679438e);
316 FF (b, c, d, a, x[15],
S14, 0x49b40821);
319 GG (a, b, c, d, x[ 1],
S21, 0xf61e2562);
320 GG (d, a, b, c, x[ 6],
S22, 0xc040b340);
321 GG (c, d, a, b, x[11],
S23, 0x265e5a51);
322 GG (b, c, d, a, x[ 0],
S24, 0xe9b6c7aa);
323 GG (a, b, c, d, x[ 5],
S21, 0xd62f105d);
324 GG (d, a, b, c, x[10],
S22, 0x2441453);
325 GG (c, d, a, b, x[15],
S23, 0xd8a1e681);
326 GG (b, c, d, a, x[ 4],
S24, 0xe7d3fbc8);
327 GG (a, b, c, d, x[ 9],
S21, 0x21e1cde6);
328 GG (d, a, b, c, x[14],
S22, 0xc33707d6);
329 GG (c, d, a, b, x[ 3],
S23, 0xf4d50d87);
331 GG (b, c, d, a, x[ 8],
S24, 0x455a14ed);
332 GG (a, b, c, d, x[13],
S21, 0xa9e3e905);
333 GG (d, a, b, c, x[ 2],
S22, 0xfcefa3f8);
334 GG (c, d, a, b, x[ 7],
S23, 0x676f02d9);
335 GG (b, c, d, a, x[12],
S24, 0x8d2a4c8a);
338 HH (a, b, c, d, x[ 5],
S31, 0xfffa3942);
339 HH (d, a, b, c, x[ 8],
S32, 0x8771f681);
340 HH (c, d, a, b, x[11],
S33, 0x6d9d6122);
341 HH (b, c, d, a, x[14],
S34, 0xfde5380c);
342 HH (a, b, c, d, x[ 1],
S31, 0xa4beea44);
343 HH (d, a, b, c, x[ 4],
S32, 0x4bdecfa9);
344 HH (c, d, a, b, x[ 7],
S33, 0xf6bb4b60);
345 HH (b, c, d, a, x[10],
S34, 0xbebfbc70);
346 HH (a, b, c, d, x[13],
S31, 0x289b7ec6);
347 HH (d, a, b, c, x[ 0],
S32, 0xeaa127fa);
348 HH (c, d, a, b, x[ 3],
S33, 0xd4ef3085);
349 HH (b, c, d, a, x[ 6],
S34, 0x4881d05);
350 HH (a, b, c, d, x[ 9],
S31, 0xd9d4d039);
351 HH (d, a, b, c, x[12],
S32, 0xe6db99e5);
352 HH (c, d, a, b, x[15],
S33, 0x1fa27cf8);
353 HH (b, c, d, a, x[ 2],
S34, 0xc4ac5665);
356 II (a, b, c, d, x[ 0],
S41, 0xf4292244);
357 II (d, a, b, c, x[ 7],
S42, 0x432aff97);
358 II (c, d, a, b, x[14],
S43, 0xab9423a7);
359 II (b, c, d, a, x[ 5],
S44, 0xfc93a039);
360 II (a, b, c, d, x[12],
S41, 0x655b59c3);
361 II (d, a, b, c, x[ 3],
S42, 0x8f0ccc92);
362 II (c, d, a, b, x[10],
S43, 0xffeff47d);
363 II (b, c, d, a, x[ 1],
S44, 0x85845dd1);
364 II (a, b, c, d, x[ 8],
S41, 0x6fa87e4f);
365 II (d, a, b, c, x[15],
S42, 0xfe2ce6e0);
366 II (c, d, a, b, x[ 6],
S43, 0xa3014314);
367 II (b, c, d, a, x[13],
S44, 0x4e0811a1);
368 II (a, b, c, d, x[ 4],
S41, 0xf7537e82);
369 II (d, a, b, c, x[11],
S42, 0xbd3af235);
370 II (c, d, a, b, x[ 2],
S43, 0x2ad7d2bb);
371 II (b, c, d, a, x[ 9],
S44, 0xeb86d391);
380 memset((
POINTER)x, 0,
sizeof (x));
389static void Encode(
unsigned char* output,
UINT4* input,
size_t len)
393 for (i = 0, j = 0; j <
len; i++, j += 4)
395 output[j] = (
unsigned char)(input[i] & 0xff);
396 output[j+1] = (
unsigned char)((input[i] >> 8) & 0xff);
397 output[j+2] = (
unsigned char)((input[i] >> 16) & 0xff);
398 output[j+3] = (
unsigned char)((input[i] >> 24) & 0xff);
408static void Decode(
UINT4* output,
const unsigned char* input,
size_t len)
412 for (i = 0, j = 0; j <
len; i++, j += 4)
413 output[i] = ((
UINT4)input[j]) | (((
UINT4)input[j+1]) << 8) |
414 (((
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])