1
0
mirror of synced 2024-11-24 15:50:16 +01:00

feat: Made sum hash calculation more useful

This commit is contained in:
WerWolv 2024-02-13 20:20:48 +01:00
parent 03beca1099
commit ee3d6ec24b
3 changed files with 41 additions and 11 deletions

View File

@ -16,6 +16,8 @@
"hex.hashes.hash.common.salt": "Salt", "hex.hashes.hash.common.salt": "Salt",
"hex.hashes.hash.common.security_level": "Sicherheitsstufe", "hex.hashes.hash.common.security_level": "Sicherheitsstufe",
"hex.hashes.hash.common.size": "Grösse", "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": "Standart",
"hex.hashes.hash.common.standard.custom": "Benutzerdefiniert", "hex.hashes.hash.common.standard.custom": "Benutzerdefiniert",
"hex.hashes.hash.common.xor_out": "XOR Out", "hex.hashes.hash.common.xor_out": "XOR Out",
@ -27,6 +29,8 @@
"hex.hashes.view.hashes.remove": "Hash entfernen", "hex.hashes.view.hashes.remove": "Hash entfernen",
"hex.hashes.view.hashes.table.name": "Name", "hex.hashes.view.hashes.table.name": "Name",
"hex.hashes.view.hashes.table.result": "Resultat", "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"
} }
} }

View File

@ -20,6 +20,8 @@
"hex.hashes.hash.common.key": "Key", "hex.hashes.hash.common.key": "Key",
"hex.hashes.hash.common.security_level": "Security Level", "hex.hashes.hash.common.security_level": "Security Level",
"hex.hashes.hash.common.size": "Hash Size", "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.rounds": "Hash Rounds",
"hex.hashes.hash.common.salt": "Salt", "hex.hashes.hash.common.salt": "Salt",
"hex.hashes.hash.common.standard": "Standard", "hex.hashes.hash.common.standard": "Standard",
@ -27,6 +29,8 @@
"hex.hashes.hash.common.personalization": "Personalization", "hex.hashes.hash.common.personalization": "Personalization",
"hex.hashes.hash.common.refl_in": "Reflect In", "hex.hashes.hash.common.refl_in": "Reflect In",
"hex.hashes.hash.common.refl_out": "Reflect Out", "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"
} }
} }

View File

@ -459,34 +459,54 @@ namespace hex::plugin::hashes {
HashSum() : Hash("hex.hashes.hash.sum") {} HashSum() : Hash("hex.hashes.hash.sum") {}
Function create(std::string name) override { Function create(std::string name) override {
return Hash::create(name, [this](const Region& region, prv::Provider *provider) -> std::vector<u8> { return Hash::create(name, [hash = *this](const Region& region, prv::Provider *provider) -> std::vector<u8> {
std::array<u8, 8> result = { 0x00 }; std::array<u8, 8> result = { 0x00 };
auto reader = prv::ProviderReader(provider); auto reader = prv::ProviderReader(provider);
reader.seek(region.getStartAddress()); reader.seek(region.getStartAddress());
reader.setEndAddress(region.getEndAddress()); reader.setEndAddress(region.getEndAddress());
u64 sum = m_initialValue; u64 sum = hash.m_initialValue;
u8 progress = 0;
for (u8 byte : reader) { 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<const u8*>(&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 { void draw() override {
ImGuiExt::InputHexadecimal("hex.hashes.hash.common.iv"_lang, &m_initialValue); 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 { [[nodiscard]] nlohmann::json store() const override {
nlohmann::json result; nlohmann::json result;
result["iv"] = m_initialValue; result["iv"] = m_initialValue;
result["size"] = m_size; result["size"] = m_outputSize;
return result; return result;
} }
@ -494,13 +514,15 @@ namespace hex::plugin::hashes {
void load(const nlohmann::json &data) override { void load(const nlohmann::json &data) override {
try { try {
m_initialValue = data.at("iv").get<int>(); m_initialValue = data.at("iv").get<int>();
m_size = data.at("size").get<int>(); m_outputSize = data.at("size").get<int>();
} catch (std::exception&) { } } catch (std::exception&) { }
} }
private: private:
u64 m_initialValue = 0x00; 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 { class HashSnefru : public ContentRegistry::Hashes::Hash {