WebSocket服务器实现(接受密钥不匹配)

C++ WebSocket Server Implementation (Accept Key missmatch)

本文关键字:密钥 不匹配 服务器 实现 WebSocket      更新时间:2023-10-16

我试着搜索,但没有找到任何答案。我正在用c++做一个websocket服务器,但是我不能让accept-key工作。

下面是我的代码:
std::string magicKey = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
key.append(magicKey);   
unsigned char* sha_str = SHA1(reinterpret_cast<const unsigned char*>(key.c_str()), key.length(), nullptr);
std::string final = base64_encode(reinterpret_cast<const unsigned char*>(sha_str), strlen((char*)sha_str));

我使用openssl/sha。h

您是否在末尾连接"=" ?

等效perl代码是验证是否需要

    use Digest::SHA qw(sha1_base64);
    my $clientKey="xyz";
    my $specifcationGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
    my $fullWebSocketKey = "$clientKey$specifcationGUID";
    # dGhlIHNhbXBsZSBub25jZQ==258EAFA5-E914-47DA-95CA-C5AB0DC85B11
    #print "Fill socket key is $fullWebSocketKeyn";
    # 0xb3 0x7a 0x4f 0x2c 0xc0 0x62 0x4f 0x16 0x90 0xf6 0x46 0x06 0xcf 
    # 0x38 0x59 0x45 0xb2 0xbe 0xc4 0xea
    print sha1_base64($fullWebSocketKey) . "=";