fuck you newlines in hashing

This commit is contained in:
altalk23 2024-01-22 20:42:11 +03:00
parent 316e652b19
commit 491666965d
4 changed files with 37 additions and 2 deletions

View file

@ -313,11 +313,12 @@ function(package_geode_resources_now proname src dest header_dest)
# list(APPEND HASHED_EXTENSIONS ".png") # list(APPEND HASHED_EXTENSIONS ".png")
# list(APPEND HASHED_EXTENSIONS ".mp3") # list(APPEND HASHED_EXTENSIONS ".mp3")
# list(APPEND HASHED_EXTENSIONS ".ogg") # list(APPEND HASHED_EXTENSIONS ".ogg")
list(APPEND HASHED_EXTENSIONS ".md") list(APPEND HASHED_TEXT_EXTENSIONS ".md")
foreach(file ${RESOURCE_FILES}) foreach(file ${RESOURCE_FILES})
cmake_path(GET file FILENAME FILE_NAME) cmake_path(GET file FILENAME FILE_NAME)
get_filename_component(FILE_EXTENSION ${file} EXT) get_filename_component(FILE_EXTENSION ${file} EXT)
list(FIND HASHED_EXTENSIONS "${FILE_EXTENSION}" FILE_SHOULD_HASH) list(FIND HASHED_EXTENSIONS "${FILE_EXTENSION}" FILE_SHOULD_HASH)
if (NOT FILE_NAME STREQUAL ".geode_cache" AND NOT FILE_SHOULD_HASH EQUAL -1) 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() 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() endforeach()
list(APPEND HEADER_FILE "}\;\n") list(APPEND HEADER_FILE "}\;\n")

View file

@ -39,6 +39,19 @@ std::string calculateSHA256(ghc::filesystem::path const& path) {
return picosha2::bytes_to_hex_string(hash.begin(), hash.end()); return picosha2::bytes_to_hex_string(hash.begin(), hash.end());
} }
std::string calculateSHA256Text(ghc::filesystem::path const& path) {
// remove all newlines
std::vector<uint8_t> 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) { std::string calculateHash(ghc::filesystem::path const& path) {
return calculateSHA3_256(path); return calculateSHA3_256(path);
} }

View file

@ -7,4 +7,6 @@ std::string calculateSHA3_256(ghc::filesystem::path const& path);
std::string calculateSHA256(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); std::string calculateHash(ghc::filesystem::path const& path);

View file

@ -218,7 +218,8 @@ bool updater::verifyLoaderResources() {
continue; continue;
} }
// verify hash // 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); const auto& expected = LOADER_RESOURCE_HASHES.at(name);
if (hash != expected) { if (hash != expected) {
log::debug("Resource hash mismatch: {} ({}, {})", name, hash.substr(0, 7), expected.substr(0, 7)); log::debug("Resource hash mismatch: {} ({}, {})", name, hash.substr(0, 7), expected.substr(0, 7));