From 491666965df8e77c41328816c033401ba43637a3 Mon Sep 17 00:00:00 2001 From: altalk23 <45172705+altalk23@users.noreply.github.com> Date: Mon, 22 Jan 2024 20:42:11 +0300 Subject: [PATCH] fuck you newlines in hashing --- cmake/GeodeFile.cmake | 21 ++++++++++++++++++++- loader/hash/hash.cpp | 13 +++++++++++++ loader/hash/hash.hpp | 2 ++ loader/src/loader/updater.cpp | 3 ++- 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/cmake/GeodeFile.cmake b/cmake/GeodeFile.cmake index dd9230a3..d3ec3ad7 100644 --- a/cmake/GeodeFile.cmake +++ b/cmake/GeodeFile.cmake @@ -313,11 +313,12 @@ function(package_geode_resources_now proname src dest header_dest) # list(APPEND HASHED_EXTENSIONS ".png") # list(APPEND HASHED_EXTENSIONS ".mp3") # list(APPEND HASHED_EXTENSIONS ".ogg") - list(APPEND HASHED_EXTENSIONS ".md") + list(APPEND HASHED_TEXT_EXTENSIONS ".md") foreach(file ${RESOURCE_FILES}) cmake_path(GET file FILENAME FILE_NAME) get_filename_component(FILE_EXTENSION ${file} EXT) + list(FIND HASHED_EXTENSIONS "${FILE_EXTENSION}" FILE_SHOULD_HASH) if (NOT FILE_NAME STREQUAL ".geode_cache" AND NOT FILE_SHOULD_HASH EQUAL -1) @@ -331,6 +332,24 @@ function(package_geode_resources_now proname src dest header_dest) endif() + list(FIND HASHED_TEXT_EXTENSIONS "${FILE_EXTENSION}" FILE_SHOULD_TEXT_HASH) + + if (NOT FILE_NAME STREQUAL ".geode_cache" AND NOT FILE_SHOULD_TEXT_HASH EQUAL -1) + + # create list of lines form the contens of a file + file(STRINGS ${file} LINES) + list(JOIN LINES "" JOINED) + # compute hash of the lines + string(LENGTH "${JOINED}" FILE_SIZE) + string(SHA256 COMPUTED_HASH "${JOINED}") + + message(STATUS "Hashed ${file} to ${COMPUTED_HASH} (${FILE_SIZE} bytes)") + list(APPEND HEADER_FILE "\t{ \"${FILE_NAME}\", \"${COMPUTED_HASH}\" },\n") + + # list(APPEND HEADER_FILE "\t\"${FILE_NAME}\",\n") + + endif() + endforeach() list(APPEND HEADER_FILE "}\;\n") diff --git a/loader/hash/hash.cpp b/loader/hash/hash.cpp index d733b279..5eab8e79 100644 --- a/loader/hash/hash.cpp +++ b/loader/hash/hash.cpp @@ -39,6 +39,19 @@ std::string calculateSHA256(ghc::filesystem::path const& path) { return picosha2::bytes_to_hex_string(hash.begin(), hash.end()); } +std::string calculateSHA256Text(ghc::filesystem::path const& path) { + // remove all newlines + std::vector hash(picosha2::k_digest_size); + std::ifstream file(path); + std::string text; + std::string line; + while (std::getline(file, line)) { + text += line; + } + picosha2::hash256(text.begin(), text.end(), hash.begin(), hash.end()); + return picosha2::bytes_to_hex_string(hash.begin(), hash.end()); +} + std::string calculateHash(ghc::filesystem::path const& path) { return calculateSHA3_256(path); } \ No newline at end of file diff --git a/loader/hash/hash.hpp b/loader/hash/hash.hpp index 9bfa89d6..defd753f 100644 --- a/loader/hash/hash.hpp +++ b/loader/hash/hash.hpp @@ -7,4 +7,6 @@ std::string calculateSHA3_256(ghc::filesystem::path const& path); std::string calculateSHA256(ghc::filesystem::path const& path); +std::string calculateSHA256Text(ghc::filesystem::path const& path); + std::string calculateHash(ghc::filesystem::path const& path); diff --git a/loader/src/loader/updater.cpp b/loader/src/loader/updater.cpp index 85480225..62f63278 100644 --- a/loader/src/loader/updater.cpp +++ b/loader/src/loader/updater.cpp @@ -218,7 +218,8 @@ bool updater::verifyLoaderResources() { continue; } // verify hash - auto hash = calculateSHA256(file.path()); + // if we hash anything other than text, change this + auto hash = calculateSHA256Text(file.path()); const auto& expected = LOADER_RESOURCE_HASHES.at(name); if (hash != expected) { log::debug("Resource hash mismatch: {} ({}, {})", name, hash.substr(0, 7), expected.substr(0, 7));