hybi13.ipp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. char a[16];
  26. for(int i = 0; i < 16; i += 4)
  27. {
  28. auto const v = g();
  29. a[i ] = v & 0xff;
  30. a[i+1] = (v >> 8) & 0xff;
  31. a[i+2] = (v >> 16) & 0xff;
  32. a[i+3] = (v >> 24) & 0xff;
  33. }
  34. key.resize(key.max_size());
  35. key.resize(beast::detail::base64::encode(
  36. key.data(), &a[0], 16));
  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::max_size_n);
  44. static_string<sec_ws_key_type::max_size_n + 36> m(key);
  45. m.append("258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
  46. beast::detail::sha1_context ctx;
  47. beast::detail::init(ctx);
  48. beast::detail::update(ctx, m.data(), m.size());
  49. char digest[beast::detail::sha1_context::digest_size];
  50. beast::detail::finish(ctx, &digest[0]);
  51. accept.resize(accept.max_size());
  52. accept.resize(beast::detail::base64::encode(
  53. accept.data(), &digest[0], sizeof(digest)));
  54. }
  55. } // detail
  56. } // websocket
  57. } // beast
  58. } // boost
  59. #endif // BOOST_BEAST_WEBSOCKET_DETAIL_HYBI13_IPP