诸暨麻将添加redis
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

274 lignes
7.6 KiB

  1. #include "stdafx.h"
  2. #include "SHA1.h"
  3. #include <string.h>
  4. namespace zl
  5. {
  6. namespace util
  7. {
  8. static const int kSHA1DigestSize = 20;
  9. SHA1::SHA1()
  10. {
  11. reset();
  12. }
  13. SHA1::~SHA1()
  14. {
  15. }
  16. // SHA1Init - Initialize new context.
  17. void SHA1::reset()
  18. {
  19. // SHA1 initialization constants.
  20. context_.state[0] = 0x67452301;
  21. context_.state[1] = 0xEFCDAB89;
  22. context_.state[2] = 0x98BADCFE;
  23. context_.state[3] = 0x10325476;
  24. context_.state[4] = 0xC3D2E1F0;
  25. context_.count[0] = context_.count[1] = 0;
  26. }
  27. #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
  28. // blk0() and blk() perform the initial expand.
  29. // I got the idea of expanding during the round function from SSLeay
  30. #define blk0(i) (block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) | \
  31. (rol(block->l[i], 8) & 0x00FF00FF))
  32. #define blk(i) (block->l[i & 15] = rol(block->l[(i + 13) & 15] ^ \
  33. block->l[(i + 8) & 15] ^ block->l[(i + 2) & 15] ^ block->l[i & 15], 1))
  34. // (R0+R1), R2, R3, R4 are the different operations used in SHA1.
  35. #define R0(v, w, x, y, z, i) \
  36. z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \
  37. w = rol(w, 30);
  38. #define R1(v, w, x, y, z, i) \
  39. z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \
  40. w = rol(w, 30);
  41. #define R2(v, w, x, y, z, i) \
  42. z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5);\
  43. w = rol(w, 30);
  44. #define R3(v, w, x, y, z, i) \
  45. z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \
  46. w = rol(w, 30);
  47. #define R4(v, w, x, y, z, i) \
  48. z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \
  49. w = rol(w, 30);
  50. // Hash a single 512-bit block. This is the core of the algorithm.
  51. void SHA1::sha1Transform(uint32_t state[5], const uint8_t buffer[64])
  52. {
  53. union CHAR64LONG16
  54. {
  55. uint8_t c[64];
  56. uint32_t l[16];
  57. };
  58. // Note(fbarchard): This option does modify the user's data buffer.
  59. CHAR64LONG16* block = const_cast<CHAR64LONG16*>(reinterpret_cast<const CHAR64LONG16*>(buffer));
  60. // Copy context_.state[] to working vars.
  61. uint32_t a = state[0];
  62. uint32_t b = state[1];
  63. uint32_t c = state[2];
  64. uint32_t d = state[3];
  65. uint32_t e = state[4];
  66. // 4 rounds of 20 operations each. Loop unrolled.
  67. // Note(fbarchard): The following has lint warnings for multiple ; on
  68. // a line and no space after , but is left as-is to be similar to the
  69. // original code.
  70. R0(a, b, c, d, e, 0);
  71. R0(e, a, b, c, d, 1);
  72. R0(d, e, a, b, c, 2);
  73. R0(c, d, e, a, b, 3);
  74. R0(b, c, d, e, a, 4);
  75. R0(a, b, c, d, e, 5);
  76. R0(e, a, b, c, d, 6);
  77. R0(d, e, a, b, c, 7);
  78. R0(c, d, e, a, b, 8);
  79. R0(b, c, d, e, a, 9);
  80. R0(a, b, c, d, e, 10);
  81. R0(e, a, b, c, d, 11);
  82. R0(d, e, a, b, c, 12);
  83. R0(c, d, e, a, b, 13);
  84. R0(b, c, d, e, a, 14);
  85. R0(a, b, c, d, e, 15);
  86. R1(e, a, b, c, d, 16);
  87. R1(d, e, a, b, c, 17);
  88. R1(c, d, e, a, b, 18);
  89. R1(b, c, d, e, a, 19);
  90. R2(a, b, c, d, e, 20);
  91. R2(e, a, b, c, d, 21);
  92. R2(d, e, a, b, c, 22);
  93. R2(c, d, e, a, b, 23);
  94. R2(b, c, d, e, a, 24);
  95. R2(a, b, c, d, e, 25);
  96. R2(e, a, b, c, d, 26);
  97. R2(d, e, a, b, c, 27);
  98. R2(c, d, e, a, b, 28);
  99. R2(b, c, d, e, a, 29);
  100. R2(a, b, c, d, e, 30);
  101. R2(e, a, b, c, d, 31);
  102. R2(d, e, a, b, c, 32);
  103. R2(c, d, e, a, b, 33);
  104. R2(b, c, d, e, a, 34);
  105. R2(a, b, c, d, e, 35);
  106. R2(e, a, b, c, d, 36);
  107. R2(d, e, a, b, c, 37);
  108. R2(c, d, e, a, b, 38);
  109. R2(b, c, d, e, a, 39);
  110. R3(a, b, c, d, e, 40);
  111. R3(e, a, b, c, d, 41);
  112. R3(d, e, a, b, c, 42);
  113. R3(c, d, e, a, b, 43);
  114. R3(b, c, d, e, a, 44);
  115. R3(a, b, c, d, e, 45);
  116. R3(e, a, b, c, d, 46);
  117. R3(d, e, a, b, c, 47);
  118. R3(c, d, e, a, b, 48);
  119. R3(b, c, d, e, a, 49);
  120. R3(a, b, c, d, e, 50);
  121. R3(e, a, b, c, d, 51);
  122. R3(d, e, a, b, c, 52);
  123. R3(c, d, e, a, b, 53);
  124. R3(b, c, d, e, a, 54);
  125. R3(a, b, c, d, e, 55);
  126. R3(e, a, b, c, d, 56);
  127. R3(d, e, a, b, c, 57);
  128. R3(c, d, e, a, b, 58);
  129. R3(b, c, d, e, a, 59);
  130. R4(a, b, c, d, e, 60);
  131. R4(e, a, b, c, d, 61);
  132. R4(d, e, a, b, c, 62);
  133. R4(c, d, e, a, b, 63);
  134. R4(b, c, d, e, a, 64);
  135. R4(a, b, c, d, e, 65);
  136. R4(e, a, b, c, d, 66);
  137. R4(d, e, a, b, c, 67);
  138. R4(c, d, e, a, b, 68);
  139. R4(b, c, d, e, a, 69);
  140. R4(a, b, c, d, e, 70);
  141. R4(e, a, b, c, d, 71);
  142. R4(d, e, a, b, c, 72);
  143. R4(c, d, e, a, b, 73);
  144. R4(b, c, d, e, a, 74);
  145. R4(a, b, c, d, e, 75);
  146. R4(e, a, b, c, d, 76);
  147. R4(d, e, a, b, c, 77);
  148. R4(c, d, e, a, b, 78);
  149. R4(b, c, d, e, a, 79);
  150. // Add the working vars back into context.state[].
  151. state[0] += a;
  152. state[1] += b;
  153. state[2] += c;
  154. state[3] += d;
  155. state[4] += e;
  156. }
  157. void SHA1::update(const std::string& src)
  158. {
  159. const uint8_t* buff = reinterpret_cast<const uint8_t*>(src.data());
  160. update(buff, src.size());
  161. }
  162. // Run your data through this.
  163. void SHA1::update(const uint8_t* data, size_t input_len)
  164. {
  165. size_t i = 0;
  166. // Compute number of bytes mod 64.
  167. size_t index = (context_.count[0] >> 3) & 63;
  168. // Update number of bits.
  169. // TODO(xxx): Use uint64 instead of 2 uint32_t for count.
  170. // count[0] has low 29 bits for byte count + 3 pad 0's making 32 bits for
  171. // bit count.
  172. // Add bit count to low uint32_t
  173. context_.count[0] += static_cast<uint32_t>(input_len << 3);
  174. if (context_.count[0] < static_cast<uint32_t>(input_len << 3))
  175. {
  176. ++context_.count[1]; // if overlow (carry), add one to high word
  177. }
  178. context_.count[1] += static_cast<uint32_t>(input_len >> 29);
  179. if ((index + input_len) > 63)
  180. {
  181. i = 64 - index;
  182. memcpy(&context_.buffer[index], data, i);
  183. sha1Transform(context_.state, context_.buffer);
  184. for (; i + 63 < input_len; i += 64)
  185. {
  186. sha1Transform(context_.state, data + i);
  187. }
  188. index = 0;
  189. }
  190. memcpy(&context_.buffer[index], &data[i], input_len - i);
  191. }
  192. // Add padding and return the message digest.
  193. void SHA1::finalInternal()
  194. {
  195. uint8_t finalcount[8];
  196. for (int i = 0; i < 8; ++i)
  197. {
  198. // Endian independent
  199. finalcount[i] = static_cast<uint8_t>((context_.count[(i >= 4 ? 0 : 1)] >> ((3 - (i & 3)) * 8)) & 255);
  200. }
  201. update(reinterpret_cast<const uint8_t*>("\200"), 1);
  202. while ((context_.count[0] & 504) != 448)
  203. {
  204. update(reinterpret_cast<const uint8_t*>("\0"), 1);
  205. }
  206. // Should cause a SHA1Transform().
  207. update(finalcount, 8);
  208. // Wipe variables.
  209. memset(finalcount, 0, 8); // SWR
  210. }
  211. void SHA1::final(void* digest)
  212. {
  213. char* data = static_cast<char*>(digest);
  214. finalInternal();
  215. for (int i = 0; i < kSHA1DigestSize; ++i)
  216. {
  217. data[i] = static_cast<uint8_t>((context_.state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
  218. }
  219. }
  220. static std::string encodeAsString(const void* data, size_t size, bool uppercase = false)
  221. {
  222. const char* hex_digits = uppercase ? "0123456789ABCDEF" : "0123456789abcdef";
  223. const unsigned char* p = static_cast<const unsigned char*>(data);
  224. const unsigned char *first = p;
  225. const unsigned char *end = p + size;
  226. std::string str;
  227. while (first != end)
  228. {
  229. unsigned char ch = *first;
  230. str.push_back(hex_digits[ch >> 4]);
  231. str.push_back(hex_digits[ch & 0x0F]);
  232. ++first;
  233. }
  234. return str;
  235. }
  236. std::string SHA1::hexFinal()
  237. {
  238. uint8_t digest[20];
  239. final(&digest);
  240. return encodeAsString(digest, 20);
  241. }
  242. std::string SHA1::hexDigest(const std::string& src)
  243. {
  244. SHA1 sha1;
  245. sha1.update(src);
  246. return sha1.hexFinal();
  247. }
  248. }
  249. }