Browse Source

Use fill_random_bytes in random_string

Drop the thread_local std::mt19937 so multipart boundaries and the Digest
cnonce come from the same non-deterministic source as the WebSocket
masking key. This removes the last mt19937 from httplib.h.
yhirose 3 days ago
parent
commit
4a1700cb5e
1 changed files with 5 additions and 14 deletions
  1. 5 14
      httplib.h

+ 5 - 14
httplib.h

@@ -5604,7 +5604,7 @@ inline bool is_websocket_upgrade(const Request &req) {
   return true;
   return true;
 }
 }
 
 
-// RFC 6455 Section 5.3: the masking key must be unpredictable
+// Unpredictable bytes for masking keys (RFC 6455 Section 5.3) and nonces
 inline void fill_random_bytes(unsigned char *out, size_t n) {
 inline void fill_random_bytes(unsigned char *out, size_t n) {
   thread_local std::random_device rd;
   thread_local std::random_device rd;
   size_t filled = 0;
   size_t filled = 0;
@@ -9675,19 +9675,10 @@ inline std::string random_string(size_t length) {
   constexpr const char data[] =
   constexpr const char data[] =
       "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
       "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 
 
-  thread_local auto engine([]() {
-    // std::random_device might actually be deterministic on some
-    // platforms, but due to lack of support in the c++ standard library,
-    // doing better requires either some ugly hacks or breaking portability.
-    std::random_device seed_gen;
-    // Request 128 bits of entropy for initialization
-    std::seed_seq seed_sequence{seed_gen(), seed_gen(), seed_gen(), seed_gen()};
-    return std::mt19937(seed_sequence);
-  }());
-
-  std::string result;
-  for (size_t i = 0; i < length; i++) {
-    result += data[engine() % (sizeof(data) - 1)];
+  std::string result(length, '\0');
+  fill_random_bytes(reinterpret_cast<unsigned char *>(&result[0]), length);
+  for (auto &c : result) {
+    c = data[static_cast<unsigned char>(c) % (sizeof(data) - 1)];
   }
   }
   return result;
   return result;
 }
 }