hybi13.ipp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #ifndef BOOST_BEAST_WEBSOCKET_DETAIL_HYBI13_IPP
  10. #define BOOST_BEAST_WEBSOCKET_DETAIL_HYBI13_IPP
  11. #include <boost/beast/websocket/detail/hybi13.hpp>
  12. #include <boost/beast/core/detail/sha1.hpp>
  13. #include <boost/beast/websocket/detail/prng.hpp>
  14. #include <boost/assert.hpp>
  15. #include <cstdint>
  16. #include <string>
  17. namespace boost {
  18. namespace beast {
  19. namespace websocket {
  20. namespace detail {
  21. void
  22. make_sec_ws_key(sec_ws_key_type& key)
  23. {
  24. auto g = make_prng(true);
  25. std::uint32_t a[4];
  26. for (auto& v : a)
  27. v = g();
  28. key.resize(key.max_size());
  29. key.resize(beast::detail::base64::encode(
  30. key.data(), &a[0], sizeof(a)));
  31. }
  32. void
  33. make_sec_ws_accept(
  34. sec_ws_accept_type& accept,
  35. string_view key)
  36. {
  37. BOOST_ASSERT(key.size() <= sec_ws_key_type::max_size_n);
  38. using namespace beast::detail::string_literals;
  39. auto const guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"_sv;
  40. beast::detail::sha1_context ctx;
  41. beast::detail::init(ctx);
  42. beast::detail::update(ctx, key.data(), key.size());
  43. beast::detail::update(ctx, guid.data(), guid.size());
  44. char digest[beast::detail::sha1_context::digest_size];
  45. beast::detail::finish(ctx, &digest[0]);
  46. accept.resize(accept.max_size());
  47. accept.resize(beast::detail::base64::encode(
  48. accept.data(), &digest[0], sizeof(digest)));
  49. }
  50. } // detail
  51. } // websocket
  52. } // beast
  53. } // boost
  54. #endif // BOOST_BEAST_WEBSOCKET_DETAIL_HYBI13_IPP