| 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 | |
| 21 | namespace boost { |
| 22 | namespace beast { |
| 23 | namespace websocket { |
| 24 | namespace detail { |
| 25 | |
| 26 | void |
| 27 | make_sec_ws_key(sec_ws_key_type& key) |
| 28 | { |
| 29 | auto g = make_prng(secure: true); |
| 30 | std::uint32_t a[4]; |
| 31 | for (auto& v : a) |
| 32 | v = g(); |
| 33 | key.resize(n: key.max_size()); |
| 34 | key.resize(n: beast::detail::base64::encode( |
| 35 | dest: key.data(), src: &a[0], len: sizeof(a))); |
| 36 | } |
| 37 | |
| 38 | void |
| 39 | make_sec_ws_accept( |
| 40 | sec_ws_accept_type& accept, |
| 41 | string_view key) |
| 42 | { |
| 43 | BOOST_ASSERT(key.size() <= sec_ws_key_type::static_capacity); |
| 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, message: key.data(), size: key.size()); |
| 49 | beast::detail::update(ctx, message: guid.data(), size: guid.size()); |
| 50 | char digest[beast::detail::sha1_context::digest_size]; |
| 51 | beast::detail::finish(ctx, digest: &digest[0]); |
| 52 | accept.resize(n: accept.max_size()); |
| 53 | accept.resize(n: beast::detail::base64::encode( |
| 54 | dest: accept.data(), src: &digest[0], len: sizeof(digest))); |
| 55 | } |
| 56 | |
| 57 | } // detail |
| 58 | } // websocket |
| 59 | } // beast |
| 60 | } // boost |
| 61 | |
| 62 | #endif // BOOST_BEAST_WEBSOCKET_DETAIL_HYBI13_IPP |
| 63 | |