md5.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * This RFC 1321 compatible MD5 implementation originated at:
  3. * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5
  4. *
  5. * Author:
  6. * Alexander Peslyak, better known as Solar Designer <solar at openwall.com>
  7. *
  8. * This software was written by Alexander Peslyak in 2001. No copyright is
  9. * claimed, and the software is hereby placed in the public domain.
  10. * In case this attempt to disclaim copyright and place the software in the
  11. * public domain is deemed null and void, then the software is
  12. * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the
  13. * general public under the following terms:
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted.
  17. *
  18. * There's ABSOLUTELY NO WARRANTY, express or implied.
  19. *
  20. */
  21. // Distributed under the Boost Software License, Version 1.0. (See
  22. // accompanying file LICENSE_1_0.txt or copy at
  23. // https://www.boost.org/LICENSE_1_0.txt)
  24. #ifndef BOOST_UUID_MD5_HPP
  25. #define BOOST_UUID_MD5_HPP
  26. #include <boost/cast.hpp>
  27. #include <boost/config.hpp>
  28. #include <boost/cstdint.hpp>
  29. #include <boost/uuid/uuid.hpp> // for version
  30. #include <boost/predef/other/endian.h>
  31. #include <string.h>
  32. namespace boost {
  33. namespace uuids {
  34. namespace detail {
  35. class md5
  36. {
  37. public:
  38. typedef unsigned int(digest_type)[4];
  39. md5()
  40. {
  41. MD5_Init(&ctx_);
  42. }
  43. void process_byte(unsigned char byte)
  44. {
  45. MD5_Update(&ctx_, &byte, 1);
  46. }
  47. void process_bytes(void const* buffer, std::size_t byte_count)
  48. {
  49. MD5_Update(&ctx_, buffer, boost::numeric_cast<unsigned long>(byte_count));
  50. }
  51. void get_digest(digest_type& digest)
  52. {
  53. MD5_Final(reinterpret_cast<unsigned char *>(&digest[0]), &ctx_);
  54. }
  55. unsigned char get_version() const
  56. {
  57. // RFC 4122 Section 4.1.3
  58. return uuid::version_name_based_md5;
  59. }
  60. private:
  61. /* Any 32-bit or wider unsigned integer data type will do */
  62. typedef uint32_t MD5_u32plus;
  63. typedef struct {
  64. MD5_u32plus lo, hi;
  65. MD5_u32plus a, b, c, d;
  66. unsigned char buffer[64];
  67. MD5_u32plus block[16];
  68. } MD5_CTX;
  69. /*
  70. * The basic MD5 functions.
  71. *
  72. * F and G are optimized compared to their RFC 1321 definitions for
  73. * architectures that lack an AND-NOT instruction, just like in Colin Plumb's
  74. * implementation.
  75. */
  76. BOOST_FORCEINLINE MD5_u32plus BOOST_UUID_DETAIL_MD5_F(MD5_u32plus x, MD5_u32plus y, MD5_u32plus z) { return ((z) ^ ((x) & ((y) ^ (z)))); }
  77. BOOST_FORCEINLINE MD5_u32plus BOOST_UUID_DETAIL_MD5_G(MD5_u32plus x, MD5_u32plus y, MD5_u32plus z) { return ((y) ^ ((z) & ((x) ^ (y)))); }
  78. BOOST_FORCEINLINE MD5_u32plus BOOST_UUID_DETAIL_MD5_H(MD5_u32plus x, MD5_u32plus y, MD5_u32plus z) { return (((x) ^ (y)) ^ (z)); }
  79. BOOST_FORCEINLINE MD5_u32plus BOOST_UUID_DETAIL_MD5_H2(MD5_u32plus x, MD5_u32plus y, MD5_u32plus z) { return ((x) ^ ((y) ^ (z))); }
  80. BOOST_FORCEINLINE MD5_u32plus BOOST_UUID_DETAIL_MD5_I(MD5_u32plus x, MD5_u32plus y, MD5_u32plus z) { return ((y) ^ ((x) | ~(z))); }
  81. /*
  82. * The MD5 transformation for all four rounds.
  83. */
  84. #define BOOST_UUID_DETAIL_MD5_STEP(f, a, b, c, d, x, t, s) \
  85. (a) += f((b), (c), (d)) + (x) + (t); \
  86. (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
  87. (a) += (b);
  88. /*
  89. * SET reads 4 input bytes in little-endian byte order and stores them in a
  90. * properly aligned word in host byte order.
  91. *
  92. * The check for little-endian architectures that tolerate unaligned memory
  93. * accesses is just an optimization. Nothing will break if it fails to detect
  94. * a suitable architecture.
  95. *
  96. * Unfortunately, this optimization may be a C strict aliasing rules violation
  97. * if the caller's data buffer has effective type that cannot be aliased by
  98. * MD5_u32plus. In practice, this problem may occur if these MD5 routines are
  99. * inlined into a calling function, or with future and dangerously advanced
  100. * link-time optimizations. For the time being, keeping these MD5 routines in
  101. * their own translation unit avoids the problem.
  102. */
  103. #if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
  104. #define BOOST_UUID_DETAIL_MD5_SET(n) \
  105. (*(MD5_u32plus *)&ptr[(n) * 4])
  106. #define BOOST_UUID_DETAIL_MD5_GET(n) \
  107. BOOST_UUID_DETAIL_MD5_SET(n)
  108. #else
  109. #define BOOST_UUID_DETAIL_MD5_SET(n) \
  110. (ctx->block[(n)] = \
  111. (MD5_u32plus)ptr[(n) * 4] | \
  112. ((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \
  113. ((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \
  114. ((MD5_u32plus)ptr[(n) * 4 + 3] << 24))
  115. #define BOOST_UUID_DETAIL_MD5_GET(n) \
  116. (ctx->block[(n)])
  117. #endif
  118. /*
  119. * This processes one or more 64-byte data blocks, but does NOT update the bit
  120. * counters. There are no alignment requirements.
  121. */
  122. const void *body(MD5_CTX *ctx, const void *data, unsigned long size)
  123. {
  124. const unsigned char *ptr;
  125. MD5_u32plus a, b, c, d;
  126. MD5_u32plus saved_a, saved_b, saved_c, saved_d;
  127. ptr = (const unsigned char *)data;
  128. a = ctx->a;
  129. b = ctx->b;
  130. c = ctx->c;
  131. d = ctx->d;
  132. do {
  133. saved_a = a;
  134. saved_b = b;
  135. saved_c = c;
  136. saved_d = d;
  137. /* Round 1 */
  138. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_F, a, b, c, d, BOOST_UUID_DETAIL_MD5_SET(0), 0xd76aa478, 7)
  139. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_F, d, a, b, c, BOOST_UUID_DETAIL_MD5_SET(1), 0xe8c7b756, 12)
  140. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_F, c, d, a, b, BOOST_UUID_DETAIL_MD5_SET(2), 0x242070db, 17)
  141. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_F, b, c, d, a, BOOST_UUID_DETAIL_MD5_SET(3), 0xc1bdceee, 22)
  142. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_F, a, b, c, d, BOOST_UUID_DETAIL_MD5_SET(4), 0xf57c0faf, 7)
  143. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_F, d, a, b, c, BOOST_UUID_DETAIL_MD5_SET(5), 0x4787c62a, 12)
  144. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_F, c, d, a, b, BOOST_UUID_DETAIL_MD5_SET(6), 0xa8304613, 17)
  145. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_F, b, c, d, a, BOOST_UUID_DETAIL_MD5_SET(7), 0xfd469501, 22)
  146. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_F, a, b, c, d, BOOST_UUID_DETAIL_MD5_SET(8), 0x698098d8, 7)
  147. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_F, d, a, b, c, BOOST_UUID_DETAIL_MD5_SET(9), 0x8b44f7af, 12)
  148. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_F, c, d, a, b, BOOST_UUID_DETAIL_MD5_SET(10), 0xffff5bb1, 17)
  149. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_F, b, c, d, a, BOOST_UUID_DETAIL_MD5_SET(11), 0x895cd7be, 22)
  150. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_F, a, b, c, d, BOOST_UUID_DETAIL_MD5_SET(12), 0x6b901122, 7)
  151. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_F, d, a, b, c, BOOST_UUID_DETAIL_MD5_SET(13), 0xfd987193, 12)
  152. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_F, c, d, a, b, BOOST_UUID_DETAIL_MD5_SET(14), 0xa679438e, 17)
  153. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_F, b, c, d, a, BOOST_UUID_DETAIL_MD5_SET(15), 0x49b40821, 22)
  154. /* Round 2 */
  155. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_G, a, b, c, d, BOOST_UUID_DETAIL_MD5_GET(1), 0xf61e2562, 5)
  156. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_G, d, a, b, c, BOOST_UUID_DETAIL_MD5_GET(6), 0xc040b340, 9)
  157. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_G, c, d, a, b, BOOST_UUID_DETAIL_MD5_GET(11), 0x265e5a51, 14)
  158. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_G, b, c, d, a, BOOST_UUID_DETAIL_MD5_GET(0), 0xe9b6c7aa, 20)
  159. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_G, a, b, c, d, BOOST_UUID_DETAIL_MD5_GET(5), 0xd62f105d, 5)
  160. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_G, d, a, b, c, BOOST_UUID_DETAIL_MD5_GET(10), 0x02441453, 9)
  161. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_G, c, d, a, b, BOOST_UUID_DETAIL_MD5_GET(15), 0xd8a1e681, 14)
  162. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_G, b, c, d, a, BOOST_UUID_DETAIL_MD5_GET(4), 0xe7d3fbc8, 20)
  163. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_G, a, b, c, d, BOOST_UUID_DETAIL_MD5_GET(9), 0x21e1cde6, 5)
  164. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_G, d, a, b, c, BOOST_UUID_DETAIL_MD5_GET(14), 0xc33707d6, 9)
  165. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_G, c, d, a, b, BOOST_UUID_DETAIL_MD5_GET(3), 0xf4d50d87, 14)
  166. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_G, b, c, d, a, BOOST_UUID_DETAIL_MD5_GET(8), 0x455a14ed, 20)
  167. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_G, a, b, c, d, BOOST_UUID_DETAIL_MD5_GET(13), 0xa9e3e905, 5)
  168. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_G, d, a, b, c, BOOST_UUID_DETAIL_MD5_GET(2), 0xfcefa3f8, 9)
  169. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_G, c, d, a, b, BOOST_UUID_DETAIL_MD5_GET(7), 0x676f02d9, 14)
  170. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_G, b, c, d, a, BOOST_UUID_DETAIL_MD5_GET(12), 0x8d2a4c8a, 20)
  171. /* Round 3 */
  172. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_H, a, b, c, d, BOOST_UUID_DETAIL_MD5_GET(5), 0xfffa3942, 4)
  173. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_H2, d, a, b, c, BOOST_UUID_DETAIL_MD5_GET(8), 0x8771f681, 11)
  174. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_H, c, d, a, b, BOOST_UUID_DETAIL_MD5_GET(11), 0x6d9d6122, 16)
  175. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_H2, b, c, d, a, BOOST_UUID_DETAIL_MD5_GET(14), 0xfde5380c, 23)
  176. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_H, a, b, c, d, BOOST_UUID_DETAIL_MD5_GET(1), 0xa4beea44, 4)
  177. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_H2, d, a, b, c, BOOST_UUID_DETAIL_MD5_GET(4), 0x4bdecfa9, 11)
  178. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_H, c, d, a, b, BOOST_UUID_DETAIL_MD5_GET(7), 0xf6bb4b60, 16)
  179. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_H2, b, c, d, a, BOOST_UUID_DETAIL_MD5_GET(10), 0xbebfbc70, 23)
  180. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_H, a, b, c, d, BOOST_UUID_DETAIL_MD5_GET(13), 0x289b7ec6, 4)
  181. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_H2, d, a, b, c, BOOST_UUID_DETAIL_MD5_GET(0), 0xeaa127fa, 11)
  182. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_H, c, d, a, b, BOOST_UUID_DETAIL_MD5_GET(3), 0xd4ef3085, 16)
  183. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_H2, b, c, d, a, BOOST_UUID_DETAIL_MD5_GET(6), 0x04881d05, 23)
  184. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_H, a, b, c, d, BOOST_UUID_DETAIL_MD5_GET(9), 0xd9d4d039, 4)
  185. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_H2, d, a, b, c, BOOST_UUID_DETAIL_MD5_GET(12), 0xe6db99e5, 11)
  186. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_H, c, d, a, b, BOOST_UUID_DETAIL_MD5_GET(15), 0x1fa27cf8, 16)
  187. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_H2, b, c, d, a, BOOST_UUID_DETAIL_MD5_GET(2), 0xc4ac5665, 23)
  188. /* Round 4 */
  189. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_I, a, b, c, d, BOOST_UUID_DETAIL_MD5_GET(0), 0xf4292244, 6)
  190. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_I, d, a, b, c, BOOST_UUID_DETAIL_MD5_GET(7), 0x432aff97, 10)
  191. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_I, c, d, a, b, BOOST_UUID_DETAIL_MD5_GET(14), 0xab9423a7, 15)
  192. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_I, b, c, d, a, BOOST_UUID_DETAIL_MD5_GET(5), 0xfc93a039, 21)
  193. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_I, a, b, c, d, BOOST_UUID_DETAIL_MD5_GET(12), 0x655b59c3, 6)
  194. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_I, d, a, b, c, BOOST_UUID_DETAIL_MD5_GET(3), 0x8f0ccc92, 10)
  195. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_I, c, d, a, b, BOOST_UUID_DETAIL_MD5_GET(10), 0xffeff47d, 15)
  196. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_I, b, c, d, a, BOOST_UUID_DETAIL_MD5_GET(1), 0x85845dd1, 21)
  197. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_I, a, b, c, d, BOOST_UUID_DETAIL_MD5_GET(8), 0x6fa87e4f, 6)
  198. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_I, d, a, b, c, BOOST_UUID_DETAIL_MD5_GET(15), 0xfe2ce6e0, 10)
  199. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_I, c, d, a, b, BOOST_UUID_DETAIL_MD5_GET(6), 0xa3014314, 15)
  200. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_I, b, c, d, a, BOOST_UUID_DETAIL_MD5_GET(13), 0x4e0811a1, 21)
  201. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_I, a, b, c, d, BOOST_UUID_DETAIL_MD5_GET(4), 0xf7537e82, 6)
  202. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_I, d, a, b, c, BOOST_UUID_DETAIL_MD5_GET(11), 0xbd3af235, 10)
  203. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_I, c, d, a, b, BOOST_UUID_DETAIL_MD5_GET(2), 0x2ad7d2bb, 15)
  204. BOOST_UUID_DETAIL_MD5_STEP(BOOST_UUID_DETAIL_MD5_I, b, c, d, a, BOOST_UUID_DETAIL_MD5_GET(9), 0xeb86d391, 21)
  205. a += saved_a;
  206. b += saved_b;
  207. c += saved_c;
  208. d += saved_d;
  209. ptr += 64;
  210. } while (size -= 64);
  211. ctx->a = a;
  212. ctx->b = b;
  213. ctx->c = c;
  214. ctx->d = d;
  215. return ptr;
  216. }
  217. void MD5_Init(MD5_CTX *ctx)
  218. {
  219. ctx->a = 0x67452301;
  220. ctx->b = 0xefcdab89;
  221. ctx->c = 0x98badcfe;
  222. ctx->d = 0x10325476;
  223. ctx->lo = 0;
  224. ctx->hi = 0;
  225. }
  226. void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size)
  227. {
  228. MD5_u32plus saved_lo;
  229. unsigned long used, available;
  230. saved_lo = ctx->lo;
  231. if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
  232. ctx->hi++;
  233. ctx->hi += size >> 29;
  234. used = saved_lo & 0x3f;
  235. if (used) {
  236. available = 64 - used;
  237. if (size < available) {
  238. memcpy(&ctx->buffer[used], data, size);
  239. return;
  240. }
  241. memcpy(&ctx->buffer[used], data, available);
  242. data = (const unsigned char *)data + available;
  243. size -= available;
  244. body(ctx, ctx->buffer, 64);
  245. }
  246. if (size >= 64) {
  247. data = body(ctx, data, size & ~(unsigned long)0x3f);
  248. size &= 0x3f;
  249. }
  250. memcpy(ctx->buffer, data, size);
  251. }
  252. // This must remain consistent no matter the endianness
  253. #define BOOST_UUID_DETAIL_MD5_OUT(dst, src) \
  254. (dst)[0] = (unsigned char)(src); \
  255. (dst)[1] = (unsigned char)((src) >> 8); \
  256. (dst)[2] = (unsigned char)((src) >> 16); \
  257. (dst)[3] = (unsigned char)((src) >> 24);
  258. //
  259. // A big-endian issue with MD5 results was resolved
  260. // in boost 1.71. If you generated md5 name-based uuids
  261. // with boost 1.66 through 1.70 and stored them, then
  262. // set the following compatibility flag to ensure that
  263. // your hash generation remains consistent.
  264. //
  265. #if defined(BOOST_UUID_COMPAT_PRE_1_71_MD5)
  266. #define BOOST_UUID_DETAIL_MD5_BYTE_OUT(dst, src) \
  267. BOOST_UUID_DETAIL_MD5_OUT(dst, src)
  268. #else
  269. //
  270. // We're copying into a byte buffer which is actually
  271. // backed by an unsigned int array, which later on
  272. // is then swabbed one more time by the basic name
  273. // generator. Therefore the logic here is reversed.
  274. // This was done to minimize the impact to existing
  275. // name-based hash generation. The correct fix would
  276. // be to make this and name generation endian-correct
  277. // but that would even break previously generated sha1
  278. // hashes too.
  279. //
  280. #if BOOST_ENDIAN_LITTLE_BYTE
  281. #define BOOST_UUID_DETAIL_MD5_BYTE_OUT(dst, src) \
  282. (dst)[0] = (unsigned char)((src) >> 24); \
  283. (dst)[1] = (unsigned char)((src) >> 16); \
  284. (dst)[2] = (unsigned char)((src) >> 8); \
  285. (dst)[3] = (unsigned char)(src);
  286. #else
  287. #define BOOST_UUID_DETAIL_MD5_BYTE_OUT(dst, src) \
  288. (dst)[0] = (unsigned char)(src); \
  289. (dst)[1] = (unsigned char)((src) >> 8); \
  290. (dst)[2] = (unsigned char)((src) >> 16); \
  291. (dst)[3] = (unsigned char)((src) >> 24);
  292. #endif
  293. #endif // BOOST_UUID_COMPAT_PRE_1_71_MD5
  294. void MD5_Final(unsigned char *result, MD5_CTX *ctx)
  295. {
  296. unsigned long used, available;
  297. used = ctx->lo & 0x3f;
  298. ctx->buffer[used++] = 0x80;
  299. available = 64 - used;
  300. if (available < 8) {
  301. memset(&ctx->buffer[used], 0, available);
  302. body(ctx, ctx->buffer, 64);
  303. used = 0;
  304. available = 64;
  305. }
  306. memset(&ctx->buffer[used], 0, available - 8);
  307. ctx->lo <<= 3;
  308. BOOST_UUID_DETAIL_MD5_OUT(&ctx->buffer[56], ctx->lo)
  309. BOOST_UUID_DETAIL_MD5_OUT(&ctx->buffer[60], ctx->hi)
  310. body(ctx, ctx->buffer, 64);
  311. BOOST_UUID_DETAIL_MD5_BYTE_OUT(&result[0], ctx->a)
  312. BOOST_UUID_DETAIL_MD5_BYTE_OUT(&result[4], ctx->b)
  313. BOOST_UUID_DETAIL_MD5_BYTE_OUT(&result[8], ctx->c)
  314. BOOST_UUID_DETAIL_MD5_BYTE_OUT(&result[12], ctx->d)
  315. memset(ctx, 0, sizeof(*ctx));
  316. }
  317. #undef BOOST_UUID_DETAIL_MD5_OUT
  318. #undef BOOST_UUID_DETAIL_MD5_SET
  319. #undef BOOST_UUID_DETAIL_MD5_GET
  320. #undef BOOST_UUID_DETAIL_MD5_STEP
  321. MD5_CTX ctx_;
  322. };
  323. } // detail
  324. } // uuids
  325. } // boost
  326. #endif // BOOST_UUID_MD5_HPP