From 7068a883edf55840c8abbe6417ca8624ce34fe6e Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 21 Jan 2024 23:31:53 +0100 Subject: [PATCH] feat: Added basic byte sum hash --- plugins/hashes/romfs/lang/en_US.json | 1 + plugins/hashes/source/content/hashes.cpp | 51 ++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/plugins/hashes/romfs/lang/en_US.json b/plugins/hashes/romfs/lang/en_US.json index 05e247fef..b0fbfc8ca 100644 --- a/plugins/hashes/romfs/lang/en_US.json +++ b/plugins/hashes/romfs/lang/en_US.json @@ -37,6 +37,7 @@ "hex.hashes.hash.sha256": "SHA256", "hex.hashes.hash.sha384": "SHA384", "hex.hashes.hash.sha512": "SHA512", + "hex.hashes.hash.sum": "Byte Sum", "hex.hashes.hash.tiger": "Tiger", "hex.hashes.hash.tiger2": "Tiger2", "hex.hashes.hash.blake2b": "Blake2B", diff --git a/plugins/hashes/source/content/hashes.cpp b/plugins/hashes/source/content/hashes.cpp index ab54a6127..2e0fd9651 100644 --- a/plugins/hashes/source/content/hashes.cpp +++ b/plugins/hashes/source/content/hashes.cpp @@ -429,6 +429,55 @@ namespace hex::plugin::hashes { int m_hashSize = 0; }; + class HashSum : public ContentRegistry::Hashes::Hash { + public: + 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 { + std::array result = { 0x00 }; + + auto reader = prv::ProviderReader(provider); + reader.seek(region.getStartAddress()); + reader.setEndAddress(region.getEndAddress()); + + u64 sum = m_initialValue; + for (u8 byte : reader) { + sum += byte; + } + + std::memcpy(result.data(), &sum, m_size); + + return { result.begin(), result.begin() + m_size }; + }); + } + + 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); + } + + [[nodiscard]] nlohmann::json store() const override { + nlohmann::json result; + + result["iv"] = m_initialValue; + result["size"] = m_size; + + return result; + } + + void load(const nlohmann::json &data) override { + try { + m_initialValue = data.at("iv").get(); + m_size = data.at("size").get(); + } catch (std::exception&) { } + } + + private: + u64 m_initialValue = 0x00; + int m_size = 1; + }; + void registerHashes() { ContentRegistry::Hashes::add(); @@ -438,6 +487,8 @@ namespace hex::plugin::hashes { ContentRegistry::Hashes::add(); ContentRegistry::Hashes::add(); + ContentRegistry::Hashes::add(); + ContentRegistry::Hashes::add>("hex.hashes.hash.crc8", crypt::crc8, 0x07, 0x0000, 0x0000); ContentRegistry::Hashes::add>("hex.hashes.hash.crc16", crypt::crc16, 0x8005, 0x0000, 0x0000); ContentRegistry::Hashes::add>("hex.hashes.hash.crc32", crypt::crc32, 0x04C1'1DB7, 0xFFFF'FFFF, 0xFFFF'FFFF, true, true);