linux版本中间件

Md5.cpp 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #include "Md5.h"
  2. using namespace md5space;
  3. /* Constants for MD5Transform routine. */
  4. #define S11 7
  5. #define S12 12
  6. #define S13 17
  7. #define S14 22
  8. #define S21 5
  9. #define S22 9
  10. #define S23 14
  11. #define S24 20
  12. #define S31 4
  13. #define S32 11
  14. #define S33 16
  15. #define S34 23
  16. #define S41 6
  17. #define S42 10
  18. #define S43 15
  19. #define S44 21
  20. /* F, G, H and I are basic MD5 functions.
  21. */
  22. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  23. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  24. #define H(x, y, z) ((x) ^ (y) ^ (z))
  25. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  26. /* ROTATE_LEFT rotates x left n bits.
  27. */
  28. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  29. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  30. Rotation is separate from addition to prevent recomputation.
  31. */
  32. #define FF(a, b, c, d, x, s, ac) (a) += F ((b), (c), (d)) + (x) + ac; (a) = ROTATE_LEFT ((a), (s)); (a) += (b);
  33. #define GG(a, b, c, d, x, s, ac) (a) += G ((b), (c), (d)) + (x) + ac; (a) = ROTATE_LEFT ((a), (s)); (a) += (b);
  34. #define HH(a, b, c, d, x, s, ac) (a) += H ((b), (c), (d)) + (x) + ac; (a) = ROTATE_LEFT ((a), (s)); (a) += (b);
  35. #define II(a, b, c, d, x, s, ac) (a) += I ((b), (c), (d)) + (x) + ac; (a) = ROTATE_LEFT ((a), (s));(a) += (b);
  36. const byte MD5::PADDING[64] = { 0x80 };
  37. const char MD5::HEX[16] = {
  38. '0', '1', '2', '3',
  39. '4', '5', '6', '7',
  40. '8', '9', 'a', 'b',
  41. 'c', 'd', 'e', 'f'
  42. };
  43. /* Default construct. */
  44. MD5::MD5() {
  45. reset();
  46. }
  47. /* Construct a MD5 object with a input buffer. */
  48. MD5::MD5(const void *input, size_t length) {
  49. reset();
  50. update(input, length);
  51. }
  52. /* Construct a MD5 object with a string. */
  53. MD5::MD5(const std::string &str) {
  54. reset();
  55. update(str);
  56. }
  57. /* Construct a MD5 object with a file. */
  58. MD5::MD5(std::ifstream &in) {
  59. reset();
  60. update(in);
  61. }
  62. /* Return the message-digest */
  63. const byte* MD5::digest() {
  64. if (!_finished) {
  65. _finished = true;
  66. final();
  67. }
  68. return _digest;
  69. }
  70. /* Reset the calculate state */
  71. void MD5::reset() {
  72. _finished = false;
  73. /* reset number of bits. */
  74. _count[0] = _count[1] = 0;
  75. /* Load magic initialization constants. */
  76. _state[0] = 0x67452301;
  77. _state[1] = 0xefcdab89;
  78. _state[2] = 0x98badcfe;
  79. _state[3] = 0x10325476;
  80. }
  81. /* Updating the context with a input buffer. */
  82. void MD5::update(const void *input, size_t length) {
  83. update((const byte*)input, length);
  84. }
  85. /* Updating the context with a string. */
  86. void MD5::update(const std::string &str) {
  87. update((const byte*)str.c_str(), str.length());
  88. }
  89. /* Updating the context with a file. */
  90. void MD5::update(std::ifstream &in) {
  91. if (!in)
  92. return;
  93. std::streamsize length;
  94. char buffer[BUFFER_SIZE];
  95. while (!in.eof()) {
  96. in.read(buffer, BUFFER_SIZE);
  97. length = in.gcount();
  98. if (length > 0)
  99. update(buffer, length);
  100. }
  101. in.close();
  102. }
  103. /* MD5 block update operation. Continues an MD5 message-digest
  104. operation, processing another message block, and updating the
  105. context.
  106. */
  107. void MD5::update(const byte *input, size_t length) {
  108. ulong i, index, partLen;
  109. _finished = false;
  110. /* Compute number of bytes mod 64 */
  111. index = (ulong)((_count[0] >> 3) & 0x3f);
  112. /* update number of bits */
  113. if ((_count[0] += ((ulong)length << 3)) < ((ulong)length << 3))
  114. _count[1]++;
  115. _count[1] += ((ulong)length >> 29);
  116. partLen = 64 - index;
  117. /* transform as many times as possible. */
  118. if (length >= partLen) {
  119. memcpy(&_buffer[index], input, partLen);
  120. transform(_buffer);
  121. for (i = partLen; i + 63 < length; i += 64)
  122. transform(&input[i]);
  123. index = 0;
  124. }
  125. else {
  126. i = 0;
  127. }
  128. /* Buffer remaining input */
  129. memcpy(&_buffer[index], &input[i], length - i);
  130. }
  131. /* MD5 finalization. Ends an MD5 message-_digest operation, writing the
  132. the message _digest and zeroizing the context.
  133. */
  134. void MD5::final() {
  135. byte bits[8];
  136. ulong oldState[4];
  137. ulong oldCount[2];
  138. ulong index, padLen;
  139. /* Save current state and count. */
  140. memcpy(oldState, _state, 16);
  141. memcpy(oldCount, _count, 8);
  142. /* Save number of bits */
  143. encode(_count, bits, 8);
  144. /* Pad out to 56 mod 64. */
  145. index = (ulong)((_count[0] >> 3) & 0x3f);
  146. padLen = (index < 56) ? (56 - index) : (120 - index);
  147. update(PADDING, padLen);
  148. /* Append length (before padding) */
  149. update(bits, 8);
  150. /* Store state in digest */
  151. encode(_state, _digest, 16);
  152. /* Restore current state and count. */
  153. memcpy(_state, oldState, 16);
  154. memcpy(_count, oldCount, 8);
  155. }
  156. /* MD5 basic transformation. Transforms _state based on block. */
  157. void MD5::transform(const byte block[64]) {
  158. ulong a = _state[0], b = _state[1], c = _state[2], d = _state[3], x[16];
  159. decode(block, x, 64);
  160. /* Round 1 */
  161. FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
  162. FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
  163. FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */
  164. FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
  165. FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
  166. FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
  167. FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
  168. FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
  169. FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
  170. FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
  171. FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  172. FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  173. FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  174. FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  175. FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  176. FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  177. /* Round 2 */
  178. GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */
  179. GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */
  180. GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  181. GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */
  182. GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */
  183. GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  184. GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  185. GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */
  186. GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */
  187. GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  188. GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */
  189. GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */
  190. GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  191. GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */
  192. GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */
  193. GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  194. /* Round 3 */
  195. HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */
  196. HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */
  197. HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  198. HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  199. HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */
  200. HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */
  201. HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */
  202. HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  203. HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  204. HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */
  205. HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */
  206. HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */
  207. HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */
  208. HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  209. HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  210. HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */
  211. /* Round 4 */
  212. II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */
  213. II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */
  214. II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  215. II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */
  216. II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  217. II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */
  218. II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  219. II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */
  220. II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */
  221. II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  222. II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */
  223. II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  224. II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */
  225. II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  226. II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */
  227. II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */
  228. _state[0] += a;
  229. _state[1] += b;
  230. _state[2] += c;
  231. _state[3] += d;
  232. }
  233. /* Encodes input (ulong) into output (byte). Assumes length is
  234. a multiple of 4.
  235. */
  236. void MD5::encode(const ulong *input, byte *output, size_t length) {
  237. for (size_t i = 0, j = 0; j < length; i++, j += 4) {
  238. output[j] = (byte)(input[i] & 0xff);
  239. output[j + 1] = (byte)((input[i] >> 8) & 0xff);
  240. output[j + 2] = (byte)((input[i] >> 16) & 0xff);
  241. output[j + 3] = (byte)((input[i] >> 24) & 0xff);
  242. }
  243. }
  244. /* Decodes input (byte) into output (ulong). Assumes length is
  245. a multiple of 4.
  246. */
  247. void MD5::decode(const byte *input, ulong *output, size_t length) {
  248. for (size_t i = 0, j = 0; j < length; i++, j += 4) {
  249. output[i] = ((ulong)input[j]) | (((ulong)input[j + 1]) << 8) |
  250. (((ulong)input[j + 2]) << 16) | (((ulong)input[j + 3]) << 24);
  251. }
  252. }
  253. /* Convert byte array to hex string. */
  254. std::string MD5::bytesToHexString(const byte *input, size_t length) {
  255. std::string str;
  256. str.reserve(length << 1);
  257. for (size_t i = 0; i < length; i++) {
  258. int t = input[i];
  259. int a = t / 16;
  260. int b = t % 16;
  261. str.append(1, HEX[a]);
  262. str.append(1, HEX[b]);
  263. }
  264. return str;
  265. }
  266. /* Convert digest to string value */
  267. std::string MD5::toString() {
  268. return bytesToHexString(digest(), 8);
  269. }
  270. std::string MD5::getMd5()
  271. {
  272. return bytesToHexString(digest(), 8);
  273. }