5#include <openssl/sha.h>
6#include <openssl/evp.h>
21 if (key.size() != 24 || key.substr(22) !=
"==")
return false;
22 for (
size_t i = 0; i < 22; ++i) {
24 bool ok = (c >=
'A' && c <=
'Z') || (c >=
'a' && c <=
'z') || (c >=
'0' && c <=
'9') || c ==
'+' || c ==
'/';
25 if (!ok)
return false;
36 const std::string magic_string =
"258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
37 std::string combined = client_key + magic_string;
39 unsigned char hash[SHA_DIGEST_LENGTH];
40 SHA1(
reinterpret_cast<const unsigned char*
>(combined.c_str()), combined.length(), hash);
43 int expected_len = 4 * ((SHA_DIGEST_LENGTH + 2) / 3);
44 std::string base64_key(
static_cast<size_t>(expected_len),
'\0');
46 int encoded_len = EVP_EncodeBlock(
47 reinterpret_cast<unsigned char*
>(&base64_key[0]),
52 base64_key.resize(
static_cast<size_t>(encoded_len));
Utility class for WebSocket handshakes.
Definition WebSocket.hpp:14
static bool is_valid_client_key(std::string_view key)
Checks that a Sec-WebSocket-Key is the base64 encoding of 16 bytes (RFC 6455 section 4....
Definition WebSocket.hpp:20
static std::string generate_accept_key(const std::string &client_key)
Generates the Sec-WebSocket-Accept key for the WebSocket handshake.
Definition WebSocket.hpp:35
Definition legacy_namespaces.hpp:27