From ee3d6ec24b2c5e41407f186f32b36c67b7d99ccb Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 13 Feb 2024 20:20:48 +0100 Subject: [PATCH] feat: Made sum hash calculation more useful --- plugins/hashes/romfs/lang/de_DE.json | 6 +++- plugins/hashes/romfs/lang/en_US.json | 6 +++- plugins/hashes/source/content/hashes.cpp | 40 ++++++++++++++++++------ 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/plugins/hashes/romfs/lang/de_DE.json b/plugins/hashes/romfs/lang/de_DE.json index ede7f5841..9193c12fb 100644 --- a/plugins/hashes/romfs/lang/de_DE.json +++ b/plugins/hashes/romfs/lang/de_DE.json @@ -16,6 +16,8 @@ "hex.hashes.hash.common.salt": "Salt", "hex.hashes.hash.common.security_level": "Sicherheitsstufe", "hex.hashes.hash.common.size": "Grösse", + "hex.hashes.hash.common.input_size": "Input Grösse", + "hex.hashes.hash.common.output_size": "Output Grösse", "hex.hashes.hash.common.standard": "Standart", "hex.hashes.hash.common.standard.custom": "Benutzerdefiniert", "hex.hashes.hash.common.xor_out": "XOR Out", @@ -27,6 +29,8 @@ "hex.hashes.view.hashes.remove": "Hash entfernen", "hex.hashes.view.hashes.table.name": "Name", "hex.hashes.view.hashes.table.result": "Resultat", - "hex.hashes.view.hashes.table.type": "Typ" + "hex.hashes.view.hashes.table.type": "Typ", + "hex.hashes.hash.sum": "Summe", + "hex.hashes.hash.sum.fold": "Resultat zusammenfalten" } } \ No newline at end of file diff --git a/plugins/hashes/romfs/lang/en_US.json b/plugins/hashes/romfs/lang/en_US.json index 28ba0d205..f70ffb30a 100644 --- a/plugins/hashes/romfs/lang/en_US.json +++ b/plugins/hashes/romfs/lang/en_US.json @@ -20,6 +20,8 @@ "hex.hashes.hash.common.key": "Key", "hex.hashes.hash.common.security_level": "Security Level", "hex.hashes.hash.common.size": "Hash Size", + "hex.hashes.hash.common.input_size": "Input Size", + "hex.hashes.hash.common.output_size": "Output Size", "hex.hashes.hash.common.rounds": "Hash Rounds", "hex.hashes.hash.common.salt": "Salt", "hex.hashes.hash.common.standard": "Standard", @@ -27,6 +29,8 @@ "hex.hashes.hash.common.personalization": "Personalization", "hex.hashes.hash.common.refl_in": "Reflect In", "hex.hashes.hash.common.refl_out": "Reflect Out", - "hex.hashes.hash.common.xor_out": "XOR Out" + "hex.hashes.hash.common.xor_out": "XOR Out", + "hex.hashes.hash.sum": "Sum", + "hex.hashes.hash.sum.fold": "Fold result" } } diff --git a/plugins/hashes/source/content/hashes.cpp b/plugins/hashes/source/content/hashes.cpp index b270b1a6c..7a854378f 100644 --- a/plugins/hashes/source/content/hashes.cpp +++ b/plugins/hashes/source/content/hashes.cpp @@ -459,34 +459,54 @@ namespace hex::plugin::hashes { HashSum() : Hash("hex.hashes.hash.sum") {} Function create(std::string name) override { - return Hash::create(name, [this](const Region& region, prv::Provider *provider) -> std::vector { + return Hash::create(name, [hash = *this](const Region& region, prv::Provider *provider) -> std::vector { std::array result = { 0x00 }; auto reader = prv::ProviderReader(provider); reader.seek(region.getStartAddress()); reader.setEndAddress(region.getEndAddress()); - u64 sum = m_initialValue; + u64 sum = hash.m_initialValue; + + u8 progress = 0; for (u8 byte : reader) { - sum += byte; + sum += (byte << (8 * progress)); + progress += 1; + + progress = progress % hash.m_inputSize; } - std::memcpy(result.data(), &sum, m_size); + u64 foldedSum = sum; + if (hash.m_foldOutput) { + while (foldedSum >= (1LLU << (hash.m_outputSize * 8))) { + u64 partialSum = 0; + for (size_t i = 0; i < sizeof(u64); i += hash.m_inputSize) { + u64 value = 0; + std::memcpy(&value, reinterpret_cast(&foldedSum) + i, hash.m_inputSize); + partialSum += value; + } + foldedSum = partialSum; + } + } - return { result.begin(), result.begin() + m_size }; + std::memcpy(result.data(), &foldedSum, hash.m_outputSize); + + return { result.begin(), result.begin() + hash.m_outputSize }; }); } void draw() override { ImGuiExt::InputHexadecimal("hex.hashes.hash.common.iv"_lang, &m_initialValue); - ImGui::SliderInt("hex.hashes.hash.common.size"_lang, &m_size, 1, 8, "%d", ImGuiSliderFlags_AlwaysClamp); + ImGui::SliderInt("hex.hashes.hash.common.input_size"_lang, &m_inputSize, 1, 8, "%d", ImGuiSliderFlags_AlwaysClamp); + ImGui::SliderInt("hex.hashes.hash.common.output_size"_lang, &m_outputSize, 1, 8, "%d", ImGuiSliderFlags_AlwaysClamp); + ImGui::Checkbox("hex.hashes.hash.sum.fold"_lang, &m_foldOutput); } [[nodiscard]] nlohmann::json store() const override { nlohmann::json result; result["iv"] = m_initialValue; - result["size"] = m_size; + result["size"] = m_outputSize; return result; } @@ -494,13 +514,15 @@ namespace hex::plugin::hashes { void load(const nlohmann::json &data) override { try { m_initialValue = data.at("iv").get(); - m_size = data.at("size").get(); + m_outputSize = data.at("size").get(); } catch (std::exception&) { } } private: u64 m_initialValue = 0x00; - int m_size = 1; + int m_inputSize = 1; + int m_outputSize = 1; + bool m_foldOutput = false; }; class HashSnefru : public ContentRegistry::Hashes::Hash {