| 371 | } |
| 372 | |
| 373 | std::string RandomWsKey() { |
| 374 | static constexpr char chars[] = |
| 375 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 376 | std::random_device rd; |
| 377 | uint8_t nonce[16] = {}; |
| 378 | for (auto& byte : nonce) byte = static_cast<uint8_t>(rd()); |
| 379 | |
| 380 | std::string out; |
| 381 | out.reserve(24); |
| 382 | for (size_t i = 0; i < sizeof(nonce); i += 3) { |
| 383 | const uint32_t b0 = nonce[i]; |
| 384 | const uint32_t b1 = i + 1 < sizeof(nonce) ? nonce[i + 1] : 0; |
| 385 | const uint32_t b2 = i + 2 < sizeof(nonce) ? nonce[i + 2] : 0; |
| 386 | const uint32_t value = (b0 << 16) | (b1 << 8) | b2; |
| 387 | out.push_back(chars[(value >> 18) & 0x3f]); |
| 388 | out.push_back(chars[(value >> 12) & 0x3f]); |
| 389 | out.push_back(i + 1 < sizeof(nonce) ? chars[(value >> 6) & 0x3f] : '='); |
| 390 | out.push_back(i + 2 < sizeof(nonce) ? chars[value & 0x3f] : '='); |
| 391 | } |
| 392 | return out; |
| 393 | } |
| 394 | |
| 395 | } // namespace |
| 396 | |