2020-11-11 00:13:09 +01:00
|
|
|
#include "views/view_hashes.hpp"
|
|
|
|
|
2021-01-13 17:28:27 +01:00
|
|
|
#include <hex/providers/provider.hpp>
|
|
|
|
#include <hex/helpers/utils.hpp>
|
2021-02-03 11:54:41 +01:00
|
|
|
#include <hex/helpers/crypto.hpp>
|
2020-11-11 00:13:09 +01:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2020-11-19 11:37:16 +01:00
|
|
|
|
2020-11-11 00:13:09 +01:00
|
|
|
namespace hex {
|
|
|
|
|
2021-03-03 22:26:17 +01:00
|
|
|
ViewHashes::ViewHashes() : View("hex.view.hashes.name") {
|
2021-03-27 11:36:36 +01:00
|
|
|
EventManager::subscribe<EventDataChanged>(this, [this]() {
|
2020-11-12 09:38:52 +01:00
|
|
|
this->m_shouldInvalidate = true;
|
|
|
|
});
|
2020-11-28 15:53:11 +01:00
|
|
|
|
2021-03-27 11:36:36 +01:00
|
|
|
EventManager::subscribe<EventRegionSelected>(this, [this](Region region) {
|
2020-11-28 15:53:11 +01:00
|
|
|
if (this->m_shouldMatchSelection) {
|
|
|
|
this->m_hashRegion[0] = region.address;
|
2021-06-06 17:59:54 +02:00
|
|
|
this->m_hashRegion[1] = region.address + region.size;
|
2020-11-28 15:53:11 +01:00
|
|
|
this->m_shouldInvalidate = true;
|
|
|
|
}
|
|
|
|
});
|
2020-11-11 00:13:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ViewHashes::~ViewHashes() {
|
2021-03-27 11:36:36 +01:00
|
|
|
EventManager::unsubscribe<EventDataChanged>(this);
|
|
|
|
EventManager::unsubscribe<EventRegionSelected>(this);
|
2020-11-11 00:13:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-02 23:11:23 +01:00
|
|
|
template<size_t Size>
|
|
|
|
static void formatBigHexInt(std::array<u8, Size> dataArray, char *buffer, size_t bufferSize) {
|
2020-11-12 12:00:50 +01:00
|
|
|
for (int i = 0; i < dataArray.size(); i++)
|
2021-02-02 23:11:23 +01:00
|
|
|
snprintf(buffer + 2 * i, bufferSize - 2 * i, "%02X", dataArray[i]);
|
2020-11-12 12:00:50 +01:00
|
|
|
}
|
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
void ViewHashes::drawContent() {
|
2021-03-03 22:26:17 +01:00
|
|
|
if (ImGui::Begin(View::toWindowName("hex.view.hashes.name").c_str(), &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) {
|
2021-08-28 14:22:02 +02:00
|
|
|
if (ImGui::BeginChild("##scrolling", ImVec2(0, 0), false, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoNav)) {
|
2020-11-11 00:13:09 +01:00
|
|
|
|
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
auto provider = SharedData::currentProvider;
|
|
|
|
if (provider != nullptr && provider->isAvailable()) {
|
2020-11-11 00:13:09 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
ImGui::TextUnformatted("hex.common.region"_lang);
|
|
|
|
ImGui::Separator();
|
2020-11-11 00:13:09 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
ImGui::InputScalarN("##nolabel", ImGuiDataType_U64, this->m_hashRegion, 2, nullptr, nullptr, "%08X", ImGuiInputTextFlags_CharsHexadecimal);
|
|
|
|
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
|
2020-11-11 09:18:35 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
ImGui::Checkbox("hex.common.match_selection"_lang, &this->m_shouldMatchSelection);
|
|
|
|
if (ImGui::IsItemEdited()) {
|
|
|
|
// Force execution of Region Selection Event
|
|
|
|
EventManager::post<RequestSelectionChange>(Region{ 0, 0 });
|
|
|
|
this->m_shouldInvalidate = true;
|
|
|
|
}
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
ImGui::NewLine();
|
|
|
|
ImGui::TextUnformatted("hex.view.hashes.settings"_lang);
|
|
|
|
ImGui::Separator();
|
2020-11-28 15:53:11 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
if (ImGui::Combo("hex.view.hashes.function"_lang, &this->m_currHashFunction, HashFunctionNames,sizeof(HashFunctionNames) / sizeof(const char *)))
|
|
|
|
this->m_shouldInvalidate = true;
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
size_t dataSize = provider->getSize();
|
|
|
|
if (this->m_hashRegion[1] >= provider->getBaseAddress() + dataSize)
|
|
|
|
this->m_hashRegion[1] = provider->getBaseAddress() + dataSize;
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2020-11-28 15:53:11 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
if (this->m_hashRegion[1] >= this->m_hashRegion[0]) {
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
switch (this->m_currHashFunction) {
|
|
|
|
case 0: // CRC16
|
|
|
|
{
|
|
|
|
static int polynomial = 0x8005, init = 0x0000;
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
ImGui::InputInt("hex.view.hashes.iv"_lang, &init, 0, 0, ImGuiInputTextFlags_CharsHexadecimal);
|
|
|
|
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
|
2020-11-28 15:53:11 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
ImGui::InputInt("hex.view.hashes.poly"_lang, &polynomial, 0, 0, ImGuiInputTextFlags_CharsHexadecimal);
|
|
|
|
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
ImGui::NewLine();
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
static u16 result = 0;
|
2020-11-28 15:53:11 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
if (this->m_shouldInvalidate)
|
|
|
|
result = crypt::crc16(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1, polynomial, init);
|
|
|
|
|
|
|
|
char buffer[sizeof(result) * 2 + 1];
|
|
|
|
snprintf(buffer, sizeof(buffer), "%04X", result);
|
|
|
|
|
|
|
|
ImGui::TextUnformatted("hex.view.hashes.result"_lang);
|
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1: // CRC32
|
|
|
|
{
|
|
|
|
static int polynomial = 0x04C11DB7, init = 0xFFFFFFFF;
|
|
|
|
|
|
|
|
ImGui::InputInt("hex.view.hashes.iv"_lang, &init, 0, 0, ImGuiInputTextFlags_CharsHexadecimal);
|
|
|
|
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
|
|
|
|
|
|
|
|
ImGui::InputInt("hex.view.hashes.poly"_lang, &polynomial, 0, 0, ImGuiInputTextFlags_CharsHexadecimal);
|
|
|
|
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
|
|
|
|
|
|
|
|
ImGui::NewLine();
|
|
|
|
|
|
|
|
static u32 result = 0;
|
|
|
|
|
|
|
|
if (this->m_shouldInvalidate)
|
|
|
|
result = crypt::crc32(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1, polynomial, init);
|
|
|
|
|
|
|
|
char buffer[sizeof(result) * 2 + 1];
|
|
|
|
snprintf(buffer, sizeof(buffer), "%08X", result);
|
|
|
|
|
|
|
|
ImGui::TextUnformatted("hex.view.hashes.result"_lang);
|
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2: // MD5
|
|
|
|
{
|
|
|
|
static std::array<u8, 16> result = { 0 };
|
|
|
|
|
|
|
|
if (this->m_shouldInvalidate)
|
|
|
|
result = crypt::md5(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
|
|
|
|
|
|
|
|
char buffer[sizeof(result) * 2 + 1];
|
|
|
|
formatBigHexInt(result, buffer, sizeof(buffer));
|
|
|
|
|
|
|
|
ImGui::NewLine();
|
|
|
|
ImGui::TextUnformatted("hex.view.hashes.result"_lang);
|
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3: // SHA-1
|
|
|
|
{
|
|
|
|
static std::array<u8, 20> result = { 0 };
|
|
|
|
|
|
|
|
if (this->m_shouldInvalidate)
|
|
|
|
result = crypt::sha1(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
|
|
|
|
|
|
|
|
char buffer[sizeof(result) * 2 + 1];
|
|
|
|
formatBigHexInt(result, buffer, sizeof(buffer));
|
|
|
|
|
|
|
|
ImGui::NewLine();
|
|
|
|
ImGui::TextUnformatted("hex.view.hashes.result"_lang);
|
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 4: // SHA-224
|
|
|
|
{
|
|
|
|
static std::array<u8, 28> result = { 0 };
|
|
|
|
|
|
|
|
if (this->m_shouldInvalidate)
|
|
|
|
result = crypt::sha224(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
|
|
|
|
|
|
|
|
char buffer[sizeof(result) * 2 + 1];
|
|
|
|
formatBigHexInt(result, buffer, sizeof(buffer));
|
|
|
|
|
|
|
|
ImGui::NewLine();
|
|
|
|
ImGui::TextUnformatted("hex.view.hashes.result"_lang);
|
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 5: // SHA-256
|
|
|
|
{
|
|
|
|
static std::array<u8, 32> result;
|
|
|
|
|
|
|
|
if (this->m_shouldInvalidate)
|
|
|
|
result = crypt::sha256(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
|
|
|
|
|
|
|
|
char buffer[sizeof(result) * 2 + 1];
|
|
|
|
formatBigHexInt(result, buffer, sizeof(buffer));
|
|
|
|
|
|
|
|
ImGui::NewLine();
|
|
|
|
ImGui::TextUnformatted("hex.view.hashes.result"_lang);
|
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 6: // SHA-384
|
|
|
|
{
|
|
|
|
static std::array<u8, 48> result;
|
|
|
|
|
|
|
|
if (this->m_shouldInvalidate)
|
|
|
|
result = crypt::sha384(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
|
|
|
|
|
|
|
|
char buffer[sizeof(result) * 2 + 1];
|
|
|
|
formatBigHexInt(result, buffer, sizeof(buffer));
|
|
|
|
|
|
|
|
ImGui::NewLine();
|
|
|
|
ImGui::TextUnformatted("hex.view.hashes.result"_lang);
|
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 7: // SHA-512
|
|
|
|
{
|
|
|
|
static std::array<u8, 64> result;
|
|
|
|
|
|
|
|
if (this->m_shouldInvalidate)
|
|
|
|
result = crypt::sha512(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
|
|
|
|
|
|
|
|
char buffer[sizeof(result) * 2 + 1];
|
|
|
|
formatBigHexInt(result, buffer, sizeof(buffer));
|
|
|
|
|
|
|
|
ImGui::NewLine();
|
|
|
|
ImGui::TextUnformatted("hex.view.hashes.result"_lang);
|
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
|
|
|
|
}
|
|
|
|
break;
|
2020-11-12 12:00:50 +01:00
|
|
|
}
|
|
|
|
|
2020-11-11 09:18:35 +01:00
|
|
|
}
|
2020-11-11 00:13:09 +01:00
|
|
|
|
2021-08-28 14:22:02 +02:00
|
|
|
this->m_shouldInvalidate = false;
|
2020-11-11 00:13:09 +01:00
|
|
|
}
|
2021-08-28 14:22:02 +02:00
|
|
|
ImGui::EndChild();
|
2020-11-11 00:13:09 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-11 10:47:02 +01:00
|
|
|
ImGui::End();
|
2020-11-11 00:13:09 +01:00
|
|
|
}
|
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
void ViewHashes::drawMenu() {
|
2020-11-23 23:57:19 +01:00
|
|
|
|
2020-11-11 00:13:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|