|
1 | 1 | #include "HTTPConnection.hpp" |
2 | | -#include "Websocket.hpp" |
3 | | -#include <hwcrypto/sha.h> |
4 | 2 |
|
5 | 3 | namespace httpsserver { |
6 | 4 |
|
7 | | - |
8 | 5 | HTTPConnection::HTTPConnection(ResourceResolver * resResolver): |
9 | 6 | _resResolver(resResolver) { |
10 | 7 | _socket = -1; |
@@ -584,12 +581,29 @@ bool HTTPConnection::checkWebsocket() { |
584 | 581 |
|
585 | 582 | std::string HTTPConnection::websocketKeyResponseHash(std::string key) { |
586 | 583 | std::string newKey = key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; |
587 | | - uint8_t shaData[20]; |
| 584 | + uint8_t shaData[HTTPS_SHA1_LENGTH]; |
588 | 585 | esp_sha(SHA1, (uint8_t*)newKey.data(), newKey.length(), shaData); |
589 | | - //GeneralUtils::hexDump(shaData, 20); |
590 | | - std::string retStr; |
591 | | - base64Encode(std::string((char*)shaData, sizeof(shaData)), &retStr); |
592 | | - return retStr; |
| 586 | + |
| 587 | + // Get output size required for base64 representation |
| 588 | + size_t b64BufferSize = 0; |
| 589 | + mbedtls_base64_encode(nullptr, 0, &b64BufferSize, (const unsigned char*)shaData, HTTPS_SHA1_LENGTH); |
| 590 | + |
| 591 | + // Do the real encoding |
| 592 | + unsigned char bufferOut[b64BufferSize]; |
| 593 | + size_t bytesEncoded = 0; |
| 594 | + int res = mbedtls_base64_encode( |
| 595 | + bufferOut, |
| 596 | + b64BufferSize, |
| 597 | + &bytesEncoded, |
| 598 | + (const unsigned char*)shaData, |
| 599 | + HTTPS_SHA1_LENGTH |
| 600 | + ); |
| 601 | + |
| 602 | + // Check result and return the encoded string |
| 603 | + if (res != 0) { |
| 604 | + return std::string(); |
| 605 | + } |
| 606 | + return std::string((char*)bufferOut, bytesEncoded); |
593 | 607 | } // WebsocketKeyResponseHash |
594 | 608 |
|
595 | 609 | } /* namespace httpsserver */ |
0 commit comments