55#define _AIEBU_COMMON_UID_MD5_H_
66
77#include < vector>
8+ #include < algorithm>
9+ #include < cstring>
10+ #include < iomanip>
11+ #include < <cstdint> >
12+ #include < sstream>
13+ #include < boost/version.hpp>
814#include < boost/uuid/detail/md5.hpp>
9- #include < boost/algorithm/hex.hpp>
1015
1116namespace aiebu {
1217
@@ -35,9 +40,28 @@ class uid_md5 {
3540
3641 constexpr size_t element_size = sizeof (digest[0 ]);
3742
38- // Early-exit doesn't work here. If sizeof(digest[0]] == 1
39- // then the compiler would complain about unreachable code
40- // because the if is compiled at compile time.
43+ // Boost 1.71+ writes MD5 digest bytes in RFC 1321 order (little-endian per 32-bit
44+ // word). Older Boost used host-endian byte layout in the digest buffer; the
45+ // reverse below normalized that for stable output. Applying the reverse on 1.71+
46+ // scrambles the digest (see boost/uuid/detail/md5.hpp, BOOST_UUID_DETAIL_MD5_BYTE_OUT).
47+
48+ // digest_type is either unsigned char[16] (element_size == 1) or unsigned int[4]
49+ // (element_size == 4), depending on Boost. Pre-1.71 vs 1.71+ MD5 byte layout differs.
50+
51+ // BOOST_VERSION < 107100 and element_size ==1 -- reverse each 4-byte word
52+ // BOOST_VERSION < 107100 and element_size !=1 -- do not reverse
53+ // BOOST_VERSION >= 107100 and element_size !=1 -- reverse each element_size chunk
54+ // BOOST_VERSION >= 107100 and element_size ==1 -- do not reverse
55+ #if BOOST_VERSION < 107100 || defined(BOOST_UUID_COMPAT_PRE_1_71_MD5)
56+ // Pre-1.71 (or compat): element_size == 1 -> reverse each 4-byte MD5 word;
57+ // element_size != 1 -> leave sig as memcpy'd from digest.
58+ if constexpr (element_size == 1 ) {
59+ for (size_t i = 0 ; i < md5_size; i += 4 )
60+ std::reverse (sig.data () + i, sig.data () + i + 4 );
61+ }
62+ #else
63+ // Boost >= 1.71: element_size != 1 -> reverse each element-sized chunk;
64+ // element_size == 1 -> leave as memcpy'd.
4165 if constexpr (element_size != 1 ) {
4266 // Different boost versions model digest_type differently:
4367 // 1. typedef unsigned int(digest_type)[4];
@@ -53,6 +77,7 @@ class uid_md5 {
5377 tcurr = tend;
5478 }
5579 }
80+ #endif
5681
5782 return sig;
5883 }
0 commit comments