From eda26319c420b70f831ebb0eeb05277c149a8995 Mon Sep 17 00:00:00 2001 From: Vladislav Ostapov Date: Thu, 14 Nov 2024 11:34:28 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20sh?= =?UTF-8?q?a256=20=D0=B4=D0=BB=D1=8F=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B0=20?= =?UTF-8?q?=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/auth/utils.cpp | 7 +++++-- src/auth/utils.h | 1 + src/main.cpp | 5 ++++- src/server/resource.cpp | 2 -- static/main.html | 6 +++++- 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/auth/utils.cpp b/src/auth/utils.cpp index 32fcc55..8450d3a 100644 --- a/src/auth/utils.cpp +++ b/src/auth/utils.cpp @@ -9,9 +9,12 @@ std::string http::utils::sha256(const std::string &payload) { - // Вычисляем SHA256 хеш + return sha256(payload.c_str(), payload.size()); +} + +std::string http::utils::sha256(const char* data, size_t size) { unsigned char hash[SHA256_DIGEST_LENGTH]; - SHA256(reinterpret_cast(payload.c_str()), payload.length(), hash); + SHA256(reinterpret_cast(data), size, hash); // Преобразуем хеш в шестнадцатеричную строку std::stringstream ss; diff --git a/src/auth/utils.h b/src/auth/utils.h index 0fe3ba9..d924725 100644 --- a/src/auth/utils.h +++ b/src/auth/utils.h @@ -6,6 +6,7 @@ namespace http::utils { std::string sha256(const std::string& payload); + std::string sha256(const char* data, size_t size); std::string sha256AsB64(const std::string& payload); std::string b64Encode(const char* data, size_t size); diff --git a/src/main.cpp b/src/main.cpp index bed9e08..dfe9038 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,6 +19,7 @@ #include "terminal_api_driver.h" #include "auth/resources.h" #include "auth/jwt.h" +#include "auth/utils.h" namespace ssl = boost::asio::ssl; // from @@ -347,7 +348,9 @@ public: rep.headers.push_back({.name = "Content-Type", .value = toString(mime_types::json)}); std::string result = R"({"status":"ok","fwsize":)"; result += std::to_string(req.payload.size()); - result += "}"; + result += ",\"sha256\":\""; + result += http::utils::sha256(req.payload.data(), req.payload.size()); + result += "\"}"; rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); })); } diff --git a/src/server/resource.cpp b/src/server/resource.cpp index 7fa27e8..afd33d1 100644 --- a/src/server/resource.cpp +++ b/src/server/resource.cpp @@ -3,8 +3,6 @@ #include #include -#include "../../dependencies/control_system/common/protocol_commands.h" - static void loadFile(const std::string& path, std::vector& content) { std::ifstream is(path, std::ios::in | std::ios::binary); if (!is) { diff --git a/static/main.html b/static/main.html index 9287c6b..e3179ed 100644 --- a/static/main.html +++ b/static/main.html @@ -619,6 +619,7 @@ @@ -954,7 +955,8 @@ uploadFw: { progress: null, - filename: null + filename: null, + sha256: null }, testState: false, @@ -1313,6 +1315,8 @@ }); xhr.addEventListener("loadend", () => { this.uploadFw.progress = 100 + const rep = JSON.parse(xhr.responseText); + this.uploadFw.sha256 = rep['sha256'] resolve(xhr.readyState === 4 && xhr.status === 200); }); xhr.open("PUT", "/api/firmwareUpdate", true);