1// 2// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) 3// 4// Distributed under the Boost Software License, Version 1.0. (See accompanying 5// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6// 7// Official repository: https://github.com/boostorg/beast 8// 9 10#ifndef BOOST_BEAST_WEBSOCKET_DETAIL_HYBI13_IPP 11#define BOOST_BEAST_WEBSOCKET_DETAIL_HYBI13_IPP 12 13#include <boost/beast/websocket/detail/hybi13.hpp> 14#include <boost/beast/core/detail/sha1.hpp> 15#include <boost/beast/websocket/detail/prng.hpp> 16 17#include <boost/assert.hpp> 18#include <cstdint> 19#include <string> 20 21namespace boost { 22namespace beast { 23namespace websocket { 24namespace detail { 25 26void 27make_sec_ws_key(sec_ws_key_type& key) 28{ 29 auto g = make_prng(true); 30 std::uint32_t a[4]; 31 for (auto& v : a) 32 v = g(); 33 key.resize(key.max_size()); 34 key.resize(beast::detail::base64::encode( 35 key.data(), &a[0], sizeof(a))); 36} 37 38void 39make_sec_ws_accept( 40 sec_ws_accept_type& accept, 41 string_view key) 42{ 43 BOOST_ASSERT(key.size() <= sec_ws_key_type::max_size_n); 44 using namespace beast::detail::string_literals; 45 auto const guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"_sv; 46 beast::detail::sha1_context ctx; 47 beast::detail::init(ctx); 48 beast::detail::update(ctx, key.data(), key.size()); 49 beast::detail::update(ctx, guid.data(), guid.size()); 50 char digest[beast::detail::sha1_context::digest_size]; 51 beast::detail::finish(ctx, &digest[0]); 52 accept.resize(accept.max_size()); 53 accept.resize(beast::detail::base64::encode( 54 accept.data(), &digest[0], sizeof(digest))); 55} 56 57} // detail 58} // websocket 59} // beast 60} // boost 61 62#endif // BOOST_BEAST_WEBSOCKET_DETAIL_HYBI13_IPP 63