encrypt_md5.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include <string.h>
  2. #include "encrypt_md5.h"
  3. #define S11 7
  4. #define S12 12
  5. #define S13 17
  6. #define S14 22
  7. #define S21 5
  8. #define S22 9
  9. #define S23 14
  10. #define S24 20
  11. #define S31 4
  12. #define S32 11
  13. #define S33 16
  14. #define S34 23
  15. #define S41 6
  16. #define S42 10
  17. #define S43 15
  18. #define S44 21
  19. static unsigned char PADDING[64] = {
  20. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  21. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  22. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  23. };
  24. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  25. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  26. #define H(x, y, z) ((x) ^ (y) ^ (z))
  27. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  28. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  29. #define FF(a, b, c, d, x, s, ac) { (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); }
  30. #define GG(a, b, c, d, x, s, ac) { (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); }
  31. #define HH(a, b, c, d, x, s, ac) { (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); }
  32. #define II(a, b, c, d, x, s, ac) { (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); }
  33. static void md5_encode(unsigned char *output, UINT4 *input, unsigned int len)
  34. {
  35. unsigned int i, j;
  36. for (i = 0, j = 0; j < len; i++, j += 4)
  37. {
  38. output[j] = (unsigned char)(input[i] & 0xff);
  39. output[j + 1] = (unsigned char)((input[i] >> 8) & 0xff);
  40. output[j + 2] = (unsigned char)((input[i] >> 16) & 0xff);
  41. output[j + 3] = (unsigned char)((input[i] >> 24) & 0xff);
  42. }
  43. }
  44. static void md5_decode(UINT4 *output, unsigned char *input, unsigned int len)
  45. {
  46. unsigned int i, j;
  47. for (i = 0, j = 0; j < len; i++, j += 4) output[i] = ((UINT4)input[j]) | (((UINT4)input[j + 1]) << 8) |
  48. (((UINT4)input[j + 2]) << 16) | (((UINT4)input[j + 3]) << 24);
  49. }
  50. static void md5_transform(UINT4 state[4], unsigned char block[64])
  51. {
  52. UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  53. md5_decode(x, block, 64);
  54. FF(a, b, c, d, x[0], S11, 0xd76aa478);
  55. FF(d, a, b, c, x[1], S12, 0xe8c7b756);
  56. FF(c, d, a, b, x[2], S13, 0x242070db);
  57. FF(b, c, d, a, x[3], S14, 0xc1bdceee);
  58. FF(a, b, c, d, x[4], S11, 0xf57c0faf);
  59. FF(d, a, b, c, x[5], S12, 0x4787c62a);
  60. FF(c, d, a, b, x[6], S13, 0xa8304613);
  61. FF(b, c, d, a, x[7], S14, 0xfd469501);
  62. FF(a, b, c, d, x[8], S11, 0x698098d8);
  63. FF(d, a, b, c, x[9], S12, 0x8b44f7af);
  64. FF(c, d, a, b, x[10], S13, 0xffff5bb1);
  65. FF(b, c, d, a, x[11], S14, 0x895cd7be);
  66. FF(a, b, c, d, x[12], S11, 0x6b901122);
  67. FF(d, a, b, c, x[13], S12, 0xfd987193);
  68. FF(c, d, a, b, x[14], S13, 0xa679438e);
  69. FF(b, c, d, a, x[15], S14, 0x49b40821);
  70. GG(a, b, c, d, x[1], S21, 0xf61e2562);
  71. GG(d, a, b, c, x[6], S22, 0xc040b340);
  72. GG(c, d, a, b, x[11], S23, 0x265e5a51);
  73. GG(b, c, d, a, x[0], S24, 0xe9b6c7aa);
  74. GG(a, b, c, d, x[5], S21, 0xd62f105d);
  75. GG(d, a, b, c, x[10], S22, 0x2441453);
  76. GG(c, d, a, b, x[15], S23, 0xd8a1e681);
  77. GG(b, c, d, a, x[4], S24, 0xe7d3fbc8);
  78. GG(a, b, c, d, x[9], S21, 0x21e1cde6);
  79. GG(d, a, b, c, x[14], S22, 0xc33707d6);
  80. GG(c, d, a, b, x[3], S23, 0xf4d50d87);
  81. GG(b, c, d, a, x[8], S24, 0x455a14ed);
  82. GG(a, b, c, d, x[13], S21, 0xa9e3e905);
  83. GG(d, a, b, c, x[2], S22, 0xfcefa3f8);
  84. GG(c, d, a, b, x[7], S23, 0x676f02d9);
  85. GG(b, c, d, a, x[12], S24, 0x8d2a4c8a);
  86. HH(a, b, c, d, x[5], S31, 0xfffa3942);
  87. HH(d, a, b, c, x[8], S32, 0x8771f681);
  88. HH(c, d, a, b, x[11], S33, 0x6d9d6122);
  89. HH(b, c, d, a, x[14], S34, 0xfde5380c);
  90. HH(a, b, c, d, x[1], S31, 0xa4beea44);
  91. HH(d, a, b, c, x[4], S32, 0x4bdecfa9);
  92. HH(c, d, a, b, x[7], S33, 0xf6bb4b60);
  93. HH(b, c, d, a, x[10], S34, 0xbebfbc70);
  94. HH(a, b, c, d, x[13], S31, 0x289b7ec6);
  95. HH(d, a, b, c, x[0], S32, 0xeaa127fa);
  96. HH(c, d, a, b, x[3], S33, 0xd4ef3085);
  97. HH(b, c, d, a, x[6], S34, 0x4881d05);
  98. HH(a, b, c, d, x[9], S31, 0xd9d4d039);
  99. HH(d, a, b, c, x[12], S32, 0xe6db99e5);
  100. HH(c, d, a, b, x[15], S33, 0x1fa27cf8);
  101. HH(b, c, d, a, x[2], S34, 0xc4ac5665);
  102. II(a, b, c, d, x[0], S41, 0xf4292244);
  103. II(d, a, b, c, x[7], S42, 0x432aff97);
  104. II(c, d, a, b, x[14], S43, 0xab9423a7);
  105. II(b, c, d, a, x[5], S44, 0xfc93a039);
  106. II(a, b, c, d, x[12], S41, 0x655b59c3);
  107. II(d, a, b, c, x[3], S42, 0x8f0ccc92);
  108. II(c, d, a, b, x[10], S43, 0xffeff47d);
  109. II(b, c, d, a, x[1], S44, 0x85845dd1);
  110. II(a, b, c, d, x[8], S41, 0x6fa87e4f);
  111. II(d, a, b, c, x[15], S42, 0xfe2ce6e0);
  112. II(c, d, a, b, x[6], S43, 0xa3014314);
  113. II(b, c, d, a, x[13], S44, 0x4e0811a1);
  114. II(a, b, c, d, x[4], S41, 0xf7537e82);
  115. II(d, a, b, c, x[11], S42, 0xbd3af235);
  116. II(c, d, a, b, x[2], S43, 0x2ad7d2bb);
  117. II(b, c, d, a, x[9], S44, 0xeb86d391);
  118. state[0] += a;
  119. state[1] += b;
  120. state[2] += c;
  121. state[3] += d;
  122. memset((POINTER)x, 0, sizeof(x));
  123. }
  124. void md5_init(MD5_CTX *context)
  125. {
  126. context->count[0] = context->count[1] = 0;
  127. context->state[0] = 0x67452301;
  128. context->state[1] = 0xefcdab89;
  129. context->state[2] = 0x98badcfe;
  130. context->state[3] = 0x10325476;
  131. }
  132. void md5_update(MD5_CTX *context, unsigned char *input, unsigned int inputLen)
  133. {
  134. unsigned int i, index, partLen;
  135. index = (unsigned int)((context->count[0] >> 3) & 0x3F);
  136. if ((context->count[0] += ((UINT4)inputLen << 3))
  137. < ((UINT4)inputLen << 3)) context->count[1]++;
  138. context->count[1] += ((UINT4)inputLen >> 29);
  139. partLen = 64 - index;
  140. if (inputLen >= partLen)
  141. {
  142. memcpy((POINTER)&context->buffer[index], (POINTER)input, partLen);
  143. md5_transform(context->state, context->buffer);
  144. for (i = partLen; i + 63 < inputLen; i += 64) md5_transform(context->state, &input[i]);
  145. index = 0;
  146. } else i = 0;
  147. memcpy((POINTER)&context->buffer[index], (POINTER)&input[i], inputLen - i);
  148. }
  149. void md5_final(unsigned char digest[16], MD5_CTX *context)
  150. {
  151. unsigned char bits[8];
  152. unsigned int index, padLen;
  153. md5_encode(bits, context->count, 8);
  154. index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  155. padLen = (index < 56) ? (56 - index) : (120 - index);
  156. md5_update(context, PADDING, padLen);
  157. md5_update(context, bits, 8);
  158. md5_encode(digest, context->state, 16);
  159. memset((POINTER)context, 0, sizeof(*context));
  160. }
  161. void md5_digest(char *pszInput, unsigned long nInputSize, char *pszOutPut)
  162. {
  163. MD5_CTX context;
  164. md5_init(&context);
  165. md5_update(&context, (unsigned char *)pszInput, nInputSize);
  166. md5_final((unsigned char *)pszOutPut, &context);
  167. }