1 // Md5.cpp: implementation of the CMd5 class.
3 //////////////////////////////////////////////////////////////////////
10 static char THIS_FILE
[]=__FILE__
;
15 #define FF(a, b, c, d, x, s, ac) { \
16 (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
17 (a) = ROTATE_LEFT ((a), (s)); \
21 #define GG(a, b, c, d, x, s, ac) { \
22 (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
23 (a) = ROTATE_LEFT ((a), (s)); \
27 #define HH(a, b, c, d, x, s, ac) { \
28 (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
29 (a) = ROTATE_LEFT ((a), (s)); \
33 #define II(a, b, c, d, x, s, ac) { \
34 (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
35 (a) = ROTATE_LEFT ((a), (s)); \
38 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
40 #define MD5_PAD_FIRST_BYTE 0x80 // The first byte of padding.
41 #define MD5_PAD_SIZE 56 // The padding function pads out to 56 bytes.
43 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
44 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
45 #define H(x, y, z) ((x) ^ (y) ^ (z))
46 #define I(x, y, z) ((y) ^ ((x) | (~z)))
77 #define T10 0x8b44f7af
78 #define T11 0xffff5bb1
79 #define T12 0x895cd7be
80 #define T13 0x6b901122
81 #define T14 0xfd987193
82 #define T15 0xa679438e
83 #define T16 0x49b40821
85 #define T17 0xf61e2562
86 #define T18 0xc040b340
87 #define T19 0x265e5a51
88 #define T20 0xe9b6c7aa
89 #define T21 0xd62f105d
90 #define T22 0x02441453
91 #define T23 0xd8a1e681
92 #define T24 0xe7d3fbc8
93 #define T25 0x21e1cde6
94 #define T26 0xc33707d6
95 #define T27 0xf4d50d87
96 #define T28 0x455a14ed
97 #define T29 0xa9e3e905
98 #define T30 0xfcefa3f8
99 #define T31 0x676f02d9
100 #define T32 0x8d2a4c8a
102 #define T33 0xfffa3942
103 #define T34 0x8771f681
104 #define T35 0x6d9d6122
105 #define T36 0xfde5380c
106 #define T37 0xa4beea44
107 #define T38 0x4bdecfa9
108 #define T39 0xf6bb4b60
109 #define T40 0xbebfbc70
110 #define T41 0x289b7ec6
111 #define T42 0xeaa127fa
112 #define T43 0xd4ef3085
113 #define T44 0x04881d05
114 #define T45 0xd9d4d039
115 #define T46 0xe6db99e5
116 #define T47 0x1fa27cf8
117 #define T48 0xc4ac5665
119 #define T49 0xf4292244
120 #define T50 0x432aff97
121 #define T51 0xab9423a7
122 #define T52 0xfc93a039
123 #define T53 0x655b59c3
124 #define T54 0x8f0ccc92
125 #define T55 0xffeff47d
126 #define T56 0x85845dd1
127 #define T57 0x6fa87e4f
128 #define T58 0xfe2ce6e0
129 #define T59 0xa3014314
130 #define T60 0x4e0811a1
131 #define T61 0xf7537e82
132 #define T62 0xbd3af235
133 #define T63 0x2ad7d2bb
134 #define T64 0xeb86d391
136 #define A_INIT 0x67452301
137 #define B_INIT 0xefcdab89
138 #define C_INIT 0x98badcfe
139 #define D_INIT 0x10325476
142 //////////////////////////////////////////////////////////////////////
143 // Construction/Destruction
144 //////////////////////////////////////////////////////////////////////
149 CMD5::CMD5(CFile
& InputFile
)
151 // Initialize the digest.
157 // Now init the bit length of the input.
161 // Now perform transformation on every block of input.
164 // Read a block from the input.
165 block_idx
= InputFile
.Read( manip_block
, MD5_BLOCK_SIZE
);
167 if (block_idx
== MD5_BLOCK_SIZE
)
169 // A full block is ready to be transformed. Do so.
170 MD5_IncCount( MD5_BLOCK_SIZE
);
175 MD5_IncCount( block_idx
);
177 // If the count of bytes read is not a full block,
178 // the end of the file has been found, and the ending should be prepared.
179 if (block_idx
< (MD5_PAD_SIZE
) )
181 // There is enough room in the block, to pad up to the correct size.
182 // Pad and then transform.
188 // The block is already up to or beyond the pad size. Pad to the
189 // end and of the current block, and then transform it.
193 // Now create another block that is empty and pad all the way.
199 } while (block_idx
== MD5_BLOCK_SIZE
);
202 void CMD5::SetContent(CString csContent
)
206 // Initialize the digest.
212 // Now init the bit length of the input.
216 // Now perform transformation on every block of input.
219 strcpy((char*)manip_block
, csContent
.Mid(iOffset
, MD5_BLOCK_SIZE
));
220 block_idx
= strlen((const char*)manip_block
);
222 if (block_idx
== MD5_BLOCK_SIZE
)
224 // A full block is ready to be transformed. Do so.
225 MD5_IncCount(MD5_BLOCK_SIZE
);
230 MD5_IncCount( block_idx
);
232 // If the count of bytes read is not a full block,
233 // the end of the file has been found, and the ending should be prepared.
234 if (block_idx
< (MD5_PAD_SIZE
) )
236 // There is enough room in the block, to pad up to the correct size.
237 // Pad and then transform.
243 // The block is already up to or beyond the pad size. Pad to the
244 // end and of the current block, and then transform it.
248 // Now create another block that is empty and pad all the way.
255 while (block_idx
== MD5_BLOCK_SIZE
);
258 void CMD5::MD5_Pad( MD5_PadMode PadMode
)
263 // Perform normal padding.
264 manip_block
[block_idx
++] = MD5_PAD_FIRST_BYTE
;
265 while (block_idx
< MD5_PAD_SIZE
)
266 manip_block
[block_idx
++] = 0;
268 // Now store count in this last block.
273 // Pad until the end of the block.
274 manip_block
[block_idx
++] = MD5_PAD_FIRST_BYTE
;
275 while (block_idx
< MD5_BLOCK_SIZE
)
276 manip_block
[block_idx
++] = 0;
281 while (block_idx
< MD5_PAD_SIZE
)
282 manip_block
[block_idx
++] = 0;
284 // Now store count in this last block.
290 void CMD5::MD5_Xform()
292 // Save orig values of a,b,c,d.
298 // Now decode the manip_block into xform_buf;
302 // Let [abcd k s i] denote the operation
303 // a = b + ((a + F(b,c,d) + xform_buf[k] + T[i]) <<< s).
304 // Do the following 16 operations.
305 // [ABCD 0 7 1] [DABC 1 12 2] [CDAB 2 17 3] [BCDA 3 22 4]
306 // [ABCD 4 7 5] [DABC 5 12 6] [CDAB 6 17 7] [BCDA 7 22 8]
307 // [ABCD 8 7 9] [DABC 9 12 10] [CDAB 10 17 11] [BCDA 11 22 12]
308 // [ABCD 12 7 13] [DABC 13 12 14] [CDAB 14 17 15] [BCDA 15 22 16]
310 FF (a
, b
, c
, d
, xform_buf
[ 0], S11
, T1
);
311 FF (d
, a
, b
, c
, xform_buf
[ 1], S12
, T2
);
312 FF (c
, d
, a
, b
, xform_buf
[ 2], S13
, T3
);
313 FF (b
, c
, d
, a
, xform_buf
[ 3], S14
, T4
);
314 FF (a
, b
, c
, d
, xform_buf
[ 4], S11
, T5
);
315 FF (d
, a
, b
, c
, xform_buf
[ 5], S12
, T6
);
316 FF (c
, d
, a
, b
, xform_buf
[ 6], S13
, T7
);
317 FF (b
, c
, d
, a
, xform_buf
[ 7], S14
, T8
);
318 FF (a
, b
, c
, d
, xform_buf
[ 8], S11
, T9
);
319 FF (d
, a
, b
, c
, xform_buf
[ 9], S12
, T10
);
320 FF (c
, d
, a
, b
, xform_buf
[10], S13
, T11
);
321 FF (b
, c
, d
, a
, xform_buf
[11], S14
, T12
);
322 FF (a
, b
, c
, d
, xform_buf
[12], S11
, T13
);
323 FF (d
, a
, b
, c
, xform_buf
[13], S12
, T14
);
324 FF (c
, d
, a
, b
, xform_buf
[14], S13
, T15
);
325 FF (b
, c
, d
, a
, xform_buf
[15], S14
, T16
);
328 // Let [abcd k s i] denote the operation
329 // a = b + ((a + G(b,c,d) + xform_buf[k] + T[i]) <<< s).
330 // Do the following 16 operations.
331 // [ABCD 1 5 17] [DABC 6 9 18] [CDAB 11 14 19] [BCDA 0 20 20]
332 // [ABCD 5 5 21] [DABC 10 9 22] [CDAB 15 14 23] [BCDA 4 20 24]
333 // [ABCD 9 5 25] [DABC 14 9 26] [CDAB 3 14 27] [BCDA 8 20 28]
334 // [ABCD 13 5 29] [DABC 2 9 30] [CDAB 7 14 31] [BCDA 12 20 32]
336 GG (a
, b
, c
, d
, xform_buf
[ 1], S21
, T17
);
337 GG (d
, a
, b
, c
, xform_buf
[ 6], S22
, T18
);
338 GG (c
, d
, a
, b
, xform_buf
[11], S23
, T19
);
339 GG (b
, c
, d
, a
, xform_buf
[ 0], S24
, T20
);
340 GG (a
, b
, c
, d
, xform_buf
[ 5], S21
, T21
);
341 GG (d
, a
, b
, c
, xform_buf
[10], S22
, T22
);
342 GG (c
, d
, a
, b
, xform_buf
[15], S23
, T23
);
343 GG (b
, c
, d
, a
, xform_buf
[ 4], S24
, T24
);
344 GG (a
, b
, c
, d
, xform_buf
[ 9], S21
, T25
);
345 GG (d
, a
, b
, c
, xform_buf
[14], S22
, T26
);
346 GG (c
, d
, a
, b
, xform_buf
[ 3], S23
, T27
);
347 GG (b
, c
, d
, a
, xform_buf
[ 8], S24
, T28
);
348 GG (a
, b
, c
, d
, xform_buf
[13], S21
, T29
);
349 GG (d
, a
, b
, c
, xform_buf
[ 2], S22
, T30
);
350 GG (c
, d
, a
, b
, xform_buf
[ 7], S23
, T31
);
351 GG (b
, c
, d
, a
, xform_buf
[12], S24
, T32
);
354 // Let [abcd k s t] denote the operation
355 // a = b + ((a + H(b,c,d) + xform_buf[k] + T[i]) <<< s).
356 // Do the following 16 operations.
357 // [ABCD 5 4 33] [DABC 8 11 34] [CDAB 11 16 35] [BCDA 14 23 36]
358 // [ABCD 1 4 37] [DABC 4 11 38] [CDAB 7 16 39] [BCDA 10 23 40]
359 // [ABCD 13 4 41] [DABC 0 11 42] [CDAB 3 16 43] [BCDA 6 23 44]
360 // [ABCD 9 4 45] [DABC 12 11 46] [CDAB 15 16 47] [BCDA 2 23 48]
362 HH (a
, b
, c
, d
, xform_buf
[ 5], S31
, T33
);
363 HH (d
, a
, b
, c
, xform_buf
[ 8], S32
, T34
);
364 HH (c
, d
, a
, b
, xform_buf
[11], S33
, T35
);
365 HH (b
, c
, d
, a
, xform_buf
[14], S34
, T36
);
366 HH (a
, b
, c
, d
, xform_buf
[ 1], S31
, T37
);
367 HH (d
, a
, b
, c
, xform_buf
[ 4], S32
, T38
);
368 HH (c
, d
, a
, b
, xform_buf
[ 7], S33
, T39
);
369 HH (b
, c
, d
, a
, xform_buf
[10], S34
, T40
);
370 HH (a
, b
, c
, d
, xform_buf
[13], S31
, T41
);
371 HH (d
, a
, b
, c
, xform_buf
[ 0], S32
, T42
);
372 HH (c
, d
, a
, b
, xform_buf
[ 3], S33
, T43
);
373 HH (b
, c
, d
, a
, xform_buf
[ 6], S34
, T44
);
374 HH (a
, b
, c
, d
, xform_buf
[ 9], S31
, T45
);
375 HH (d
, a
, b
, c
, xform_buf
[12], S32
, T46
);
376 HH (c
, d
, a
, b
, xform_buf
[15], S33
, T47
);
377 HH (b
, c
, d
, a
, xform_buf
[ 2], S34
, T48
);
380 // Let [abcd k s t] denote the operation
381 // a = b + ((a + I(b,c,d) + xform_buf[k] + T[i]) <<< s).
382 // Do the following 16 operations.
383 // [ABCD 0 6 49] [DABC 7 10 50] [CDAB 14 15 51] [BCDA 5 21 52]
384 // [ABCD 12 6 53] [DABC 3 10 54] [CDAB 10 15 55] [BCDA 1 21 56]
385 // [ABCD 8 6 57] [DABC 15 10 58] [CDAB 6 15 59] [BCDA 13 21 60]
386 // [ABCD 4 6 61] [DABC 11 10 62] [CDAB 2 15 63] [BCDA 9 21 64]
388 II (a
, b
, c
, d
, xform_buf
[ 0], S41
, T49
);
389 II (d
, a
, b
, c
, xform_buf
[ 7], S42
, T50
);
390 II (c
, d
, a
, b
, xform_buf
[14], S43
, T51
);
391 II (b
, c
, d
, a
, xform_buf
[ 5], S44
, T52
);
392 II (a
, b
, c
, d
, xform_buf
[12], S41
, T53
);
393 II (d
, a
, b
, c
, xform_buf
[ 3], S42
, T54
);
394 II (c
, d
, a
, b
, xform_buf
[10], S43
, T55
);
395 II (b
, c
, d
, a
, xform_buf
[ 1], S44
, T56
);
396 II (a
, b
, c
, d
, xform_buf
[ 8], S41
, T57
);
397 II (d
, a
, b
, c
, xform_buf
[15], S42
, T58
);
398 II (c
, d
, a
, b
, xform_buf
[ 6], S43
, T59
);
399 II (b
, c
, d
, a
, xform_buf
[13], S44
, T60
);
400 II (a
, b
, c
, d
, xform_buf
[ 4], S41
, T61
);
401 II (d
, a
, b
, c
, xform_buf
[11], S42
, T62
);
402 II (c
, d
, a
, b
, xform_buf
[ 2], S43
, T63
);
403 II (b
, c
, d
, a
, xform_buf
[ 9], S44
, T64
);
405 // Now perform the following additions. (That is increment each
406 // of the four registers by the value it had before this block
414 void CMD5::MD5_IncCount( int IncAmt
)
416 UINT4 old_cnt0
= count
[0];
418 count
[0] += IncAmt
*8;
419 if(count
[0] < old_cnt0
)
420 count
[1]++; // Carry detection.
423 void CMD5::MD5_Decode()
427 for (i
= 0, j
= 0; j
< MD5_BLOCK_SIZE
; i
++, j
+= sizeof(UINT4
))
428 xform_buf
[i
] = ((UINT4
)manip_block
[j
]) | (((UINT4
)manip_block
[j
+1]) << 8) |
429 (((UINT4
)manip_block
[j
+2]) << 16) | (((UINT4
)manip_block
[j
+3]) << 24);
432 void CMD5::MD5_StoreCnt()
434 manip_block
[MD5_PAD_SIZE
+0]= unsigned int( count
[0] & 0xff);
435 manip_block
[MD5_PAD_SIZE
+1]= unsigned int((count
[0] >> 8) & 0xff);
436 manip_block
[MD5_PAD_SIZE
+2]= unsigned int((count
[0] >> 16) & 0xff);
437 manip_block
[MD5_PAD_SIZE
+3]= unsigned int((count
[0] >> 24) & 0xff);
439 manip_block
[MD5_PAD_SIZE
+4]= unsigned int(count
[1] & 0xff);
440 manip_block
[MD5_PAD_SIZE
+5]= unsigned int((count
[1] >> 8) & 0xff);
441 manip_block
[MD5_PAD_SIZE
+6]= unsigned int((count
[1] >> 16) & 0xff);
442 manip_block
[MD5_PAD_SIZE
+7]= unsigned int((count
[1] >> 24) & 0xff);
445 void CMD5::GetDigest( unsigned char* Dest
)
447 Dest
[0] = unsigned int(a
& 0xff);
448 Dest
[1] = unsigned int((a
>> 8) & 0xff);
449 Dest
[2] = unsigned int((a
>> 16) & 0xff);
450 Dest
[3] = unsigned int((a
>> 24) & 0xff);
452 Dest
[4] = unsigned int(b
& 0xff);
453 Dest
[5] = unsigned int((b
>> 8) & 0xff);
454 Dest
[6] = unsigned int((b
>> 16) & 0xff);
455 Dest
[7] = unsigned int((b
>> 24) & 0xff);
457 Dest
[8] = unsigned int(c
& 0xff);
458 Dest
[9] = unsigned int((c
>> 8) & 0xff);
459 Dest
[10] = unsigned int((c
>> 16) & 0xff);
460 Dest
[11] = unsigned int((c
>> 24) & 0xff);
462 Dest
[12] = unsigned int(d
& 0xff);
463 Dest
[13] = unsigned int((d
>> 8) & 0xff);
464 Dest
[14] = unsigned int((d
>> 16) & 0xff);
465 Dest
[15] = unsigned int((d
>> 24) & 0xff);
468 CString
CMD5::ConvertToAscii(unsigned char* lpszBuff
)
471 CString csHex
= "0123456789abcdef";
474 unsigned long* plBuff
= (unsigned long*)lpszBuff
;
476 for(i
= 0; i
< 4; i
++)
479 for(j
= 0; j
< 4; j
++)
481 csRet
+= csHex
[int((num
>> (j
* 8 + 4)) & 0x0F)];
482 csRet
+= csHex
[int((num
>> (j
* 8)) & 0x0F)];