diff --git a/td/mtproto/TlsInit.cpp b/td/mtproto/TlsInit.cpp index 4e704b8ba..98fdfbc28 100644 --- a/td/mtproto/TlsInit.cpp +++ b/td/mtproto/TlsInit.cpp @@ -19,7 +19,6 @@ #include #include -#include namespace td { namespace mtproto { @@ -398,10 +397,7 @@ class TlsHelloStore { CHECK(storer.get_offset() == data.size()); parts.push_back(std::move(data)); } - for (size_t i = 1; i < parts.size(); i++) { - auto pos = static_cast(Random::secure_int32()) % (i + 1); - std::swap(parts[i], parts[pos]); - } + Random::shuffle(parts); for (auto &part : parts) { dest_.copy_from(part); dest_.remove_prefix(part.size()); diff --git a/tdutils/td/utils/Random.h b/tdutils/td/utils/Random.h index ee0d159ae..8665e0168 100644 --- a/tdutils/td/utils/Random.h +++ b/tdutils/td/utils/Random.h @@ -9,6 +9,8 @@ #include "td/utils/common.h" #include "td/utils/Slice.h" +#include + namespace td { class Random { @@ -24,6 +26,15 @@ class Random { // works only for current thread static void add_seed(Slice bytes, double entropy = 0); static void secure_cleanup(); + + template + static void shuffle(vector &v) { + for (size_t i = 1; i < v.size(); i++) { + auto pos = static_cast(secure_int32()) % (i + 1); + using std::swap; + swap(v[i], v[pos]); + } + } #endif static uint32 fast_uint32();