feat: Added basic byte sum hash

This commit is contained in:
WerWolv 2024-01-21 23:31:53 +01:00
parent 3783ec6a23
commit 7068a883ed
2 changed files with 52 additions and 0 deletions

View File

@ -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",

View File

@ -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<u8> {
std::array<u8, 8> 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<int>();
m_size = data.at("size").get<int>();
} catch (std::exception&) { }
}
private:
u64 m_initialValue = 0x00;
int m_size = 1;
};
void registerHashes() {
ContentRegistry::Hashes::add<HashMD5>();
@ -438,6 +487,8 @@ namespace hex::plugin::hashes {
ContentRegistry::Hashes::add<HashSHA384>();
ContentRegistry::Hashes::add<HashSHA512>();
ContentRegistry::Hashes::add<HashSum>();
ContentRegistry::Hashes::add<HashCRC<u8>>("hex.hashes.hash.crc8", crypt::crc8, 0x07, 0x0000, 0x0000);
ContentRegistry::Hashes::add<HashCRC<u16>>("hex.hashes.hash.crc16", crypt::crc16, 0x8005, 0x0000, 0x0000);
ContentRegistry::Hashes::add<HashCRC<u32>>("hex.hashes.hash.crc32", crypt::crc32, 0x04C1'1DB7, 0xFFFF'FFFF, 0xFFFF'FFFF, true, true);