md5.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. md5.hpp is a reformulation of the md5.h and md5.c code from
  3. http://www.opensource.apple.com/source/cups/cups-59/cups/md5.c to allow it to
  4. function as a component of a header only library. This conversion was done by
  5. Peter Thorson (webmaster@zaphoyd.com) in 2012 for the WebSocket++ project. The
  6. changes are released under the same license as the original (listed below)
  7. */
  8. /*
  9. Copyright (C) 1999, 2002 Aladdin Enterprises. All rights reserved.
  10. This software is provided 'as-is', without any express or implied
  11. warranty. In no event will the authors be held liable for any damages
  12. arising from the use of this software.
  13. Permission is granted to anyone to use this software for any purpose,
  14. including commercial applications, and to alter it and redistribute it
  15. freely, subject to the following restrictions:
  16. 1. The origin of this software must not be misrepresented; you must not
  17. claim that you wrote the original software. If you use this software
  18. in a product, an acknowledgment in the product documentation would be
  19. appreciated but is not required.
  20. 2. Altered source versions must be plainly marked as such, and must not be
  21. misrepresented as being the original software.
  22. 3. This notice may not be removed or altered from any source distribution.
  23. L. Peter Deutsch
  24. ghost@aladdin.com
  25. */
  26. /* $Id: md5.h,v 1.4 2002/04/13 19:20:28 lpd Exp $ */
  27. /*
  28. Independent implementation of MD5 (RFC 1321).
  29. This code implements the MD5 Algorithm defined in RFC 1321, whose
  30. text is available at
  31. http://www.ietf.org/rfc/rfc1321.txt
  32. The code is derived from the text of the RFC, including the test suite
  33. (section A.5) but excluding the rest of Appendix A. It does not include
  34. any code or documentation that is identified in the RFC as being
  35. copyrighted.
  36. The original and principal author of md5.h is L. Peter Deutsch
  37. <ghost@aladdin.com>. Other authors are noted in the change history
  38. that follows (in reverse chronological order):
  39. 2002-04-13 lpd Removed support for non-ANSI compilers; removed
  40. references to Ghostscript; clarified derivation from RFC 1321;
  41. now handles byte order either statically or dynamically.
  42. 1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
  43. 1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5);
  44. added conditionalization for C++ compilation from Martin
  45. Purschke <purschke@bnl.gov>.
  46. 1999-05-03 lpd Original version.
  47. */
  48. #ifndef WEBSOCKETPP_COMMON_MD5_HPP
  49. #define WEBSOCKETPP_COMMON_MD5_HPP
  50. /*
  51. * This package supports both compile-time and run-time determination of CPU
  52. * byte order. If ARCH_IS_BIG_ENDIAN is defined as 0, the code will be
  53. * compiled to run only on little-endian CPUs; if ARCH_IS_BIG_ENDIAN is
  54. * defined as non-zero, the code will be compiled to run only on big-endian
  55. * CPUs; if ARCH_IS_BIG_ENDIAN is not defined, the code will be compiled to
  56. * run on either big- or little-endian CPUs, but will run slightly less
  57. * efficiently on either one than if ARCH_IS_BIG_ENDIAN is defined.
  58. */
  59. #include <stddef.h>
  60. #include <string>
  61. #include <cstring>
  62. namespace websocketpp {
  63. /// Provides MD5 hashing functionality
  64. namespace md5 {
  65. typedef unsigned char md5_byte_t; /* 8-bit byte */
  66. typedef unsigned int md5_word_t; /* 32-bit word */
  67. /* Define the state of the MD5 Algorithm. */
  68. typedef struct md5_state_s {
  69. md5_word_t count[2]; /* message length in bits, lsw first */
  70. md5_word_t abcd[4]; /* digest buffer */
  71. md5_byte_t buf[64]; /* accumulate block */
  72. } md5_state_t;
  73. /* Initialize the algorithm. */
  74. inline void md5_init(md5_state_t *pms);
  75. /* Append a string to the message. */
  76. inline void md5_append(md5_state_t *pms, md5_byte_t const * data, size_t nbytes);
  77. /* Finish the message and return the digest. */
  78. inline void md5_finish(md5_state_t *pms, md5_byte_t digest[16]);
  79. #undef ZSW_MD5_BYTE_ORDER /* 1 = big-endian, -1 = little-endian, 0 = unknown */
  80. #ifdef ARCH_IS_BIG_ENDIAN
  81. # define ZSW_MD5_BYTE_ORDER (ARCH_IS_BIG_ENDIAN ? 1 : -1)
  82. #else
  83. # define ZSW_MD5_BYTE_ORDER 0
  84. #endif
  85. #define ZSW_MD5_T_MASK ((md5_word_t)~0)
  86. #define ZSW_MD5_T1 /* 0xd76aa478 */ (ZSW_MD5_T_MASK ^ 0x28955b87)
  87. #define ZSW_MD5_T2 /* 0xe8c7b756 */ (ZSW_MD5_T_MASK ^ 0x173848a9)
  88. #define ZSW_MD5_T3 0x242070db
  89. #define ZSW_MD5_T4 /* 0xc1bdceee */ (ZSW_MD5_T_MASK ^ 0x3e423111)
  90. #define ZSW_MD5_T5 /* 0xf57c0faf */ (ZSW_MD5_T_MASK ^ 0x0a83f050)
  91. #define ZSW_MD5_T6 0x4787c62a
  92. #define ZSW_MD5_T7 /* 0xa8304613 */ (ZSW_MD5_T_MASK ^ 0x57cfb9ec)
  93. #define ZSW_MD5_T8 /* 0xfd469501 */ (ZSW_MD5_T_MASK ^ 0x02b96afe)
  94. #define ZSW_MD5_T9 0x698098d8
  95. #define ZSW_MD5_T10 /* 0x8b44f7af */ (ZSW_MD5_T_MASK ^ 0x74bb0850)
  96. #define ZSW_MD5_T11 /* 0xffff5bb1 */ (ZSW_MD5_T_MASK ^ 0x0000a44e)
  97. #define ZSW_MD5_T12 /* 0x895cd7be */ (ZSW_MD5_T_MASK ^ 0x76a32841)
  98. #define ZSW_MD5_T13 0x6b901122
  99. #define ZSW_MD5_T14 /* 0xfd987193 */ (ZSW_MD5_T_MASK ^ 0x02678e6c)
  100. #define ZSW_MD5_T15 /* 0xa679438e */ (ZSW_MD5_T_MASK ^ 0x5986bc71)
  101. #define ZSW_MD5_T16 0x49b40821
  102. #define ZSW_MD5_T17 /* 0xf61e2562 */ (ZSW_MD5_T_MASK ^ 0x09e1da9d)
  103. #define ZSW_MD5_T18 /* 0xc040b340 */ (ZSW_MD5_T_MASK ^ 0x3fbf4cbf)
  104. #define ZSW_MD5_T19 0x265e5a51
  105. #define ZSW_MD5_T20 /* 0xe9b6c7aa */ (ZSW_MD5_T_MASK ^ 0x16493855)
  106. #define ZSW_MD5_T21 /* 0xd62f105d */ (ZSW_MD5_T_MASK ^ 0x29d0efa2)
  107. #define ZSW_MD5_T22 0x02441453
  108. #define ZSW_MD5_T23 /* 0xd8a1e681 */ (ZSW_MD5_T_MASK ^ 0x275e197e)
  109. #define ZSW_MD5_T24 /* 0xe7d3fbc8 */ (ZSW_MD5_T_MASK ^ 0x182c0437)
  110. #define ZSW_MD5_T25 0x21e1cde6
  111. #define ZSW_MD5_T26 /* 0xc33707d6 */ (ZSW_MD5_T_MASK ^ 0x3cc8f829)
  112. #define ZSW_MD5_T27 /* 0xf4d50d87 */ (ZSW_MD5_T_MASK ^ 0x0b2af278)
  113. #define ZSW_MD5_T28 0x455a14ed
  114. #define ZSW_MD5_T29 /* 0xa9e3e905 */ (ZSW_MD5_T_MASK ^ 0x561c16fa)
  115. #define ZSW_MD5_T30 /* 0xfcefa3f8 */ (ZSW_MD5_T_MASK ^ 0x03105c07)
  116. #define ZSW_MD5_T31 0x676f02d9
  117. #define ZSW_MD5_T32 /* 0x8d2a4c8a */ (ZSW_MD5_T_MASK ^ 0x72d5b375)
  118. #define ZSW_MD5_T33 /* 0xfffa3942 */ (ZSW_MD5_T_MASK ^ 0x0005c6bd)
  119. #define ZSW_MD5_T34 /* 0x8771f681 */ (ZSW_MD5_T_MASK ^ 0x788e097e)
  120. #define ZSW_MD5_T35 0x6d9d6122
  121. #define ZSW_MD5_T36 /* 0xfde5380c */ (ZSW_MD5_T_MASK ^ 0x021ac7f3)
  122. #define ZSW_MD5_T37 /* 0xa4beea44 */ (ZSW_MD5_T_MASK ^ 0x5b4115bb)
  123. #define ZSW_MD5_T38 0x4bdecfa9
  124. #define ZSW_MD5_T39 /* 0xf6bb4b60 */ (ZSW_MD5_T_MASK ^ 0x0944b49f)
  125. #define ZSW_MD5_T40 /* 0xbebfbc70 */ (ZSW_MD5_T_MASK ^ 0x4140438f)
  126. #define ZSW_MD5_T41 0x289b7ec6
  127. #define ZSW_MD5_T42 /* 0xeaa127fa */ (ZSW_MD5_T_MASK ^ 0x155ed805)
  128. #define ZSW_MD5_T43 /* 0xd4ef3085 */ (ZSW_MD5_T_MASK ^ 0x2b10cf7a)
  129. #define ZSW_MD5_T44 0x04881d05
  130. #define ZSW_MD5_T45 /* 0xd9d4d039 */ (ZSW_MD5_T_MASK ^ 0x262b2fc6)
  131. #define ZSW_MD5_T46 /* 0xe6db99e5 */ (ZSW_MD5_T_MASK ^ 0x1924661a)
  132. #define ZSW_MD5_T47 0x1fa27cf8
  133. #define ZSW_MD5_T48 /* 0xc4ac5665 */ (ZSW_MD5_T_MASK ^ 0x3b53a99a)
  134. #define ZSW_MD5_T49 /* 0xf4292244 */ (ZSW_MD5_T_MASK ^ 0x0bd6ddbb)
  135. #define ZSW_MD5_T50 0x432aff97
  136. #define ZSW_MD5_T51 /* 0xab9423a7 */ (ZSW_MD5_T_MASK ^ 0x546bdc58)
  137. #define ZSW_MD5_T52 /* 0xfc93a039 */ (ZSW_MD5_T_MASK ^ 0x036c5fc6)
  138. #define ZSW_MD5_T53 0x655b59c3
  139. #define ZSW_MD5_T54 /* 0x8f0ccc92 */ (ZSW_MD5_T_MASK ^ 0x70f3336d)
  140. #define ZSW_MD5_T55 /* 0xffeff47d */ (ZSW_MD5_T_MASK ^ 0x00100b82)
  141. #define ZSW_MD5_T56 /* 0x85845dd1 */ (ZSW_MD5_T_MASK ^ 0x7a7ba22e)
  142. #define ZSW_MD5_T57 0x6fa87e4f
  143. #define ZSW_MD5_T58 /* 0xfe2ce6e0 */ (ZSW_MD5_T_MASK ^ 0x01d3191f)
  144. #define ZSW_MD5_T59 /* 0xa3014314 */ (ZSW_MD5_T_MASK ^ 0x5cfebceb)
  145. #define ZSW_MD5_T60 0x4e0811a1
  146. #define ZSW_MD5_T61 /* 0xf7537e82 */ (ZSW_MD5_T_MASK ^ 0x08ac817d)
  147. #define ZSW_MD5_T62 /* 0xbd3af235 */ (ZSW_MD5_T_MASK ^ 0x42c50dca)
  148. #define ZSW_MD5_T63 0x2ad7d2bb
  149. #define ZSW_MD5_T64 /* 0xeb86d391 */ (ZSW_MD5_T_MASK ^ 0x14792c6e)
  150. static void md5_process(md5_state_t *pms, md5_byte_t const * data /*[64]*/) {
  151. md5_word_t
  152. a = pms->abcd[0], b = pms->abcd[1],
  153. c = pms->abcd[2], d = pms->abcd[3];
  154. md5_word_t t;
  155. #if ZSW_MD5_BYTE_ORDER > 0
  156. /* Define storage only for big-endian CPUs. */
  157. md5_word_t X[16];
  158. #else
  159. /* Define storage for little-endian or both types of CPUs. */
  160. md5_word_t xbuf[16];
  161. md5_word_t const * X;
  162. #endif
  163. {
  164. #if ZSW_MD5_BYTE_ORDER == 0
  165. /*
  166. * Determine dynamically whether this is a big-endian or
  167. * little-endian machine, since we can use a more efficient
  168. * algorithm on the latter.
  169. */
  170. static int const w = 1;
  171. if (*((md5_byte_t const *)&w)) /* dynamic little-endian */
  172. #endif
  173. #if ZSW_MD5_BYTE_ORDER <= 0 /* little-endian */
  174. {
  175. /*
  176. * On little-endian machines, we can process properly aligned
  177. * data without copying it.
  178. */
  179. if (!((data - (md5_byte_t const *)0) & 3)) {
  180. /* data are properly aligned */
  181. X = (md5_word_t const *)data;
  182. } else {
  183. /* not aligned */
  184. std::memcpy(xbuf, data, 64);
  185. X = xbuf;
  186. }
  187. }
  188. #endif
  189. #if ZSW_MD5_BYTE_ORDER == 0
  190. else /* dynamic big-endian */
  191. #endif
  192. #if ZSW_MD5_BYTE_ORDER >= 0 /* big-endian */
  193. {
  194. /*
  195. * On big-endian machines, we must arrange the bytes in the
  196. * right order.
  197. */
  198. const md5_byte_t *xp = data;
  199. int i;
  200. # if ZSW_MD5_BYTE_ORDER == 0
  201. X = xbuf; /* (dynamic only) */
  202. # else
  203. # define xbuf X /* (static only) */
  204. # endif
  205. for (i = 0; i < 16; ++i, xp += 4)
  206. xbuf[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24);
  207. }
  208. #endif
  209. }
  210. #define ZSW_MD5_ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
  211. /* Round 1. */
  212. /* Let [abcd k s i] denote the operation
  213. a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
  214. #define ZSW_MD5_F(x, y, z) (((x) & (y)) | (~(x) & (z)))
  215. #define SET(a, b, c, d, k, s, Ti)\
  216. t = a + ZSW_MD5_F(b,c,d) + X[k] + Ti;\
  217. a = ZSW_MD5_ROTATE_LEFT(t, s) + b
  218. /* Do the following 16 operations. */
  219. SET(a, b, c, d, 0, 7, ZSW_MD5_T1);
  220. SET(d, a, b, c, 1, 12, ZSW_MD5_T2);
  221. SET(c, d, a, b, 2, 17, ZSW_MD5_T3);
  222. SET(b, c, d, a, 3, 22, ZSW_MD5_T4);
  223. SET(a, b, c, d, 4, 7, ZSW_MD5_T5);
  224. SET(d, a, b, c, 5, 12, ZSW_MD5_T6);
  225. SET(c, d, a, b, 6, 17, ZSW_MD5_T7);
  226. SET(b, c, d, a, 7, 22, ZSW_MD5_T8);
  227. SET(a, b, c, d, 8, 7, ZSW_MD5_T9);
  228. SET(d, a, b, c, 9, 12, ZSW_MD5_T10);
  229. SET(c, d, a, b, 10, 17, ZSW_MD5_T11);
  230. SET(b, c, d, a, 11, 22, ZSW_MD5_T12);
  231. SET(a, b, c, d, 12, 7, ZSW_MD5_T13);
  232. SET(d, a, b, c, 13, 12, ZSW_MD5_T14);
  233. SET(c, d, a, b, 14, 17, ZSW_MD5_T15);
  234. SET(b, c, d, a, 15, 22, ZSW_MD5_T16);
  235. #undef SET
  236. /* Round 2. */
  237. /* Let [abcd k s i] denote the operation
  238. a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */
  239. #define ZSW_MD5_G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
  240. #define SET(a, b, c, d, k, s, Ti)\
  241. t = a + ZSW_MD5_G(b,c,d) + X[k] + Ti;\
  242. a = ZSW_MD5_ROTATE_LEFT(t, s) + b
  243. /* Do the following 16 operations. */
  244. SET(a, b, c, d, 1, 5, ZSW_MD5_T17);
  245. SET(d, a, b, c, 6, 9, ZSW_MD5_T18);
  246. SET(c, d, a, b, 11, 14, ZSW_MD5_T19);
  247. SET(b, c, d, a, 0, 20, ZSW_MD5_T20);
  248. SET(a, b, c, d, 5, 5, ZSW_MD5_T21);
  249. SET(d, a, b, c, 10, 9, ZSW_MD5_T22);
  250. SET(c, d, a, b, 15, 14, ZSW_MD5_T23);
  251. SET(b, c, d, a, 4, 20, ZSW_MD5_T24);
  252. SET(a, b, c, d, 9, 5, ZSW_MD5_T25);
  253. SET(d, a, b, c, 14, 9, ZSW_MD5_T26);
  254. SET(c, d, a, b, 3, 14, ZSW_MD5_T27);
  255. SET(b, c, d, a, 8, 20, ZSW_MD5_T28);
  256. SET(a, b, c, d, 13, 5, ZSW_MD5_T29);
  257. SET(d, a, b, c, 2, 9, ZSW_MD5_T30);
  258. SET(c, d, a, b, 7, 14, ZSW_MD5_T31);
  259. SET(b, c, d, a, 12, 20, ZSW_MD5_T32);
  260. #undef SET
  261. /* Round 3. */
  262. /* Let [abcd k s t] denote the operation
  263. a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */
  264. #define ZSW_MD5_H(x, y, z) ((x) ^ (y) ^ (z))
  265. #define SET(a, b, c, d, k, s, Ti)\
  266. t = a + ZSW_MD5_H(b,c,d) + X[k] + Ti;\
  267. a = ZSW_MD5_ROTATE_LEFT(t, s) + b
  268. /* Do the following 16 operations. */
  269. SET(a, b, c, d, 5, 4, ZSW_MD5_T33);
  270. SET(d, a, b, c, 8, 11, ZSW_MD5_T34);
  271. SET(c, d, a, b, 11, 16, ZSW_MD5_T35);
  272. SET(b, c, d, a, 14, 23, ZSW_MD5_T36);
  273. SET(a, b, c, d, 1, 4, ZSW_MD5_T37);
  274. SET(d, a, b, c, 4, 11, ZSW_MD5_T38);
  275. SET(c, d, a, b, 7, 16, ZSW_MD5_T39);
  276. SET(b, c, d, a, 10, 23, ZSW_MD5_T40);
  277. SET(a, b, c, d, 13, 4, ZSW_MD5_T41);
  278. SET(d, a, b, c, 0, 11, ZSW_MD5_T42);
  279. SET(c, d, a, b, 3, 16, ZSW_MD5_T43);
  280. SET(b, c, d, a, 6, 23, ZSW_MD5_T44);
  281. SET(a, b, c, d, 9, 4, ZSW_MD5_T45);
  282. SET(d, a, b, c, 12, 11, ZSW_MD5_T46);
  283. SET(c, d, a, b, 15, 16, ZSW_MD5_T47);
  284. SET(b, c, d, a, 2, 23, ZSW_MD5_T48);
  285. #undef SET
  286. /* Round 4. */
  287. /* Let [abcd k s t] denote the operation
  288. a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */
  289. #define ZSW_MD5_I(x, y, z) ((y) ^ ((x) | ~(z)))
  290. #define SET(a, b, c, d, k, s, Ti)\
  291. t = a + ZSW_MD5_I(b,c,d) + X[k] + Ti;\
  292. a = ZSW_MD5_ROTATE_LEFT(t, s) + b
  293. /* Do the following 16 operations. */
  294. SET(a, b, c, d, 0, 6, ZSW_MD5_T49);
  295. SET(d, a, b, c, 7, 10, ZSW_MD5_T50);
  296. SET(c, d, a, b, 14, 15, ZSW_MD5_T51);
  297. SET(b, c, d, a, 5, 21, ZSW_MD5_T52);
  298. SET(a, b, c, d, 12, 6, ZSW_MD5_T53);
  299. SET(d, a, b, c, 3, 10, ZSW_MD5_T54);
  300. SET(c, d, a, b, 10, 15, ZSW_MD5_T55);
  301. SET(b, c, d, a, 1, 21, ZSW_MD5_T56);
  302. SET(a, b, c, d, 8, 6, ZSW_MD5_T57);
  303. SET(d, a, b, c, 15, 10, ZSW_MD5_T58);
  304. SET(c, d, a, b, 6, 15, ZSW_MD5_T59);
  305. SET(b, c, d, a, 13, 21, ZSW_MD5_T60);
  306. SET(a, b, c, d, 4, 6, ZSW_MD5_T61);
  307. SET(d, a, b, c, 11, 10, ZSW_MD5_T62);
  308. SET(c, d, a, b, 2, 15, ZSW_MD5_T63);
  309. SET(b, c, d, a, 9, 21, ZSW_MD5_T64);
  310. #undef SET
  311. /* Then perform the following additions. (That is increment each
  312. of the four registers by the value it had before this block
  313. was started.) */
  314. pms->abcd[0] += a;
  315. pms->abcd[1] += b;
  316. pms->abcd[2] += c;
  317. pms->abcd[3] += d;
  318. }
  319. void md5_init(md5_state_t *pms) {
  320. pms->count[0] = pms->count[1] = 0;
  321. pms->abcd[0] = 0x67452301;
  322. pms->abcd[1] = /*0xefcdab89*/ ZSW_MD5_T_MASK ^ 0x10325476;
  323. pms->abcd[2] = /*0x98badcfe*/ ZSW_MD5_T_MASK ^ 0x67452301;
  324. pms->abcd[3] = 0x10325476;
  325. }
  326. void md5_append(md5_state_t *pms, md5_byte_t const * data, size_t nbytes) {
  327. md5_byte_t const * p = data;
  328. size_t left = nbytes;
  329. int offset = (pms->count[0] >> 3) & 63;
  330. md5_word_t nbits = (md5_word_t)(nbytes << 3);
  331. if (nbytes <= 0)
  332. return;
  333. /* Update the message length. */
  334. pms->count[1] += nbytes >> 29;
  335. pms->count[0] += nbits;
  336. if (pms->count[0] < nbits)
  337. pms->count[1]++;
  338. /* Process an initial partial block. */
  339. if (offset) {
  340. int copy = (offset + nbytes > 64 ? 64 - offset : static_cast<int>(nbytes));
  341. std::memcpy(pms->buf + offset, p, copy);
  342. if (offset + copy < 64)
  343. return;
  344. p += copy;
  345. left -= copy;
  346. md5_process(pms, pms->buf);
  347. }
  348. /* Process full blocks. */
  349. for (; left >= 64; p += 64, left -= 64)
  350. md5_process(pms, p);
  351. /* Process a final partial block. */
  352. if (left)
  353. std::memcpy(pms->buf, p, left);
  354. }
  355. void md5_finish(md5_state_t *pms, md5_byte_t digest[16]) {
  356. static md5_byte_t const pad[64] = {
  357. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  358. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  359. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  360. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  361. };
  362. md5_byte_t data[8];
  363. int i;
  364. /* Save the length before padding. */
  365. for (i = 0; i < 8; ++i)
  366. data[i] = (md5_byte_t)(pms->count[i >> 2] >> ((i & 3) << 3));
  367. /* Pad to 56 bytes mod 64. */
  368. md5_append(pms, pad, ((55 - (pms->count[0] >> 3)) & 63) + 1);
  369. /* Append the length. */
  370. md5_append(pms, data, 8);
  371. for (i = 0; i < 16; ++i)
  372. digest[i] = (md5_byte_t)(pms->abcd[i >> 2] >> ((i & 3) << 3));
  373. }
  374. // some convenience c++ functions
  375. inline std::string md5_hash_string(std::string const & s) {
  376. char digest[16];
  377. md5_state_t state;
  378. md5_init(&state);
  379. md5_append(&state, (md5_byte_t const *)s.c_str(), s.size());
  380. md5_finish(&state, (md5_byte_t *)digest);
  381. std::string ret;
  382. ret.resize(16);
  383. std::copy(digest,digest+16,ret.begin());
  384. return ret;
  385. }
  386. const char hexval[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  387. inline std::string md5_hash_hex(std::string const & input) {
  388. std::string hash = md5_hash_string(input);
  389. std::string hex;
  390. for (size_t i = 0; i < hash.size(); i++) {
  391. hex.push_back(hexval[((hash[i] >> 4) & 0xF)]);
  392. hex.push_back(hexval[(hash[i]) & 0x0F]);
  393. }
  394. return hex;
  395. }
  396. } // md5
  397. } // websocketpp
  398. #endif // WEBSOCKETPP_COMMON_MD5_HPP