Add and use Random::shuffle.

This commit is contained in:
levlam 2023-04-19 10:02:34 +03:00
parent af54239018
commit 4b7c1aee03
2 changed files with 12 additions and 5 deletions

View File

@ -19,7 +19,6 @@
#include <algorithm> #include <algorithm>
#include <cstring> #include <cstring>
#include <utility>
namespace td { namespace td {
namespace mtproto { namespace mtproto {
@ -398,10 +397,7 @@ class TlsHelloStore {
CHECK(storer.get_offset() == data.size()); CHECK(storer.get_offset() == data.size());
parts.push_back(std::move(data)); parts.push_back(std::move(data));
} }
for (size_t i = 1; i < parts.size(); i++) { Random::shuffle(parts);
auto pos = static_cast<size_t>(Random::secure_int32()) % (i + 1);
std::swap(parts[i], parts[pos]);
}
for (auto &part : parts) { for (auto &part : parts) {
dest_.copy_from(part); dest_.copy_from(part);
dest_.remove_prefix(part.size()); dest_.remove_prefix(part.size());

View File

@ -9,6 +9,8 @@
#include "td/utils/common.h" #include "td/utils/common.h"
#include "td/utils/Slice.h" #include "td/utils/Slice.h"
#include <utility>
namespace td { namespace td {
class Random { class Random {
@ -24,6 +26,15 @@ class Random {
// works only for current thread // works only for current thread
static void add_seed(Slice bytes, double entropy = 0); static void add_seed(Slice bytes, double entropy = 0);
static void secure_cleanup(); static void secure_cleanup();
template <class T>
static void shuffle(vector<T> &v) {
for (size_t i = 1; i < v.size(); i++) {
auto pos = static_cast<size_t>(secure_int32()) % (i + 1);
using std::swap;
swap(v[i], v[pos]);
}
}
#endif #endif
static uint32 fast_uint32(); static uint32 fast_uint32();