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)) {
|
2020-11-11 00:13:09 +01:00
|
|
|
ImGui::BeginChild("##scrolling", ImVec2(0, 0), false, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoNav);
|
|
|
|
|
2021-01-12 23:28:41 +01:00
|
|
|
auto provider = SharedData::currentProvider;
|
2020-12-27 15:39:06 +01:00
|
|
|
if (provider != nullptr && provider->isAvailable()) {
|
2020-11-11 00:13:09 +01:00
|
|
|
|
2021-02-11 23:09:45 +01:00
|
|
|
ImGui::TextUnformatted("hex.common.region"_lang);
|
2020-11-11 09:18:35 +01:00
|
|
|
ImGui::Separator();
|
2020-11-11 00:13:09 +01:00
|
|
|
|
2020-11-28 15:53:11 +01:00
|
|
|
ImGui::InputScalarN("##nolabel", ImGuiDataType_U64, this->m_hashRegion, 2, nullptr, nullptr, "%08X", ImGuiInputTextFlags_CharsHexadecimal);
|
2020-11-12 09:38:52 +01:00
|
|
|
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
|
2020-11-11 00:13:09 +01:00
|
|
|
|
2021-02-11 23:09:45 +01:00
|
|
|
ImGui::Checkbox("hex.common.match_selection"_lang, &this->m_shouldMatchSelection);
|
2021-05-23 22:46:33 +02:00
|
|
|
if (ImGui::IsItemEdited()) {
|
|
|
|
// Force execution of Region Selection Event
|
|
|
|
EventManager::post<RequestSelectionChange>(Region{ 0, 0 });
|
|
|
|
this->m_shouldInvalidate = true;
|
|
|
|
}
|
2020-11-11 00:13:09 +01:00
|
|
|
|
2020-11-11 09:18:35 +01:00
|
|
|
ImGui::NewLine();
|
2021-02-11 23:09:45 +01:00
|
|
|
ImGui::TextUnformatted("hex.view.hashes.settings"_lang);
|
2020-11-11 09:18:35 +01:00
|
|
|
ImGui::Separator();
|
2020-11-11 00:13:09 +01:00
|
|
|
|
2021-02-11 23:09:45 +01:00
|
|
|
if (ImGui::Combo("hex.view.hashes.function"_lang, &this->m_currHashFunction, HashFunctionNames,sizeof(HashFunctionNames) / sizeof(const char *)))
|
2020-11-28 15:53:11 +01:00
|
|
|
this->m_shouldInvalidate = true;
|
|
|
|
|
2020-12-27 15:39:06 +01:00
|
|
|
size_t dataSize = provider->getSize();
|
2021-04-16 21:50:15 +02:00
|
|
|
if (this->m_hashRegion[1] >= provider->getBaseAddress() + dataSize)
|
2021-06-06 17:59:54 +02:00
|
|
|
this->m_hashRegion[1] = provider->getBaseAddress() + dataSize;
|
2020-11-28 15:53:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
if (this->m_hashRegion[1] >= this->m_hashRegion[0]) {
|
2020-11-11 00:13:09 +01:00
|
|
|
|
2020-11-11 09:18:35 +01:00
|
|
|
switch (this->m_currHashFunction) {
|
|
|
|
case 0: // CRC16
|
|
|
|
{
|
2021-06-06 18:00:14 +02:00
|
|
|
static int polynomial = 0x8005, init = 0x0000;
|
2020-11-11 09:18:35 +01:00
|
|
|
|
2021-02-11 23:09:45 +01:00
|
|
|
ImGui::InputInt("hex.view.hashes.iv"_lang, &init, 0, 0, ImGuiInputTextFlags_CharsHexadecimal);
|
2020-11-12 09:38:52 +01:00
|
|
|
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
|
|
|
|
|
2021-02-11 23:09:45 +01:00
|
|
|
ImGui::InputInt("hex.view.hashes.poly"_lang, &polynomial, 0, 0, ImGuiInputTextFlags_CharsHexadecimal);
|
2020-11-12 09:38:52 +01:00
|
|
|
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
|
2020-11-11 09:18:35 +01:00
|
|
|
|
|
|
|
ImGui::NewLine();
|
2020-11-11 00:13:09 +01:00
|
|
|
|
2020-11-11 09:18:35 +01:00
|
|
|
static u16 result = 0;
|
2020-11-11 00:13:09 +01:00
|
|
|
|
2020-11-12 09:38:52 +01:00
|
|
|
if (this->m_shouldInvalidate)
|
2021-02-02 23:11:23 +01:00
|
|
|
result = crypt::crc16(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1, polynomial, init);
|
2020-11-11 09:18:35 +01:00
|
|
|
|
2020-11-12 12:00:50 +01:00
|
|
|
char buffer[sizeof(result) * 2 + 1];
|
|
|
|
snprintf(buffer, sizeof(buffer), "%04X", result);
|
2020-11-28 15:53:11 +01:00
|
|
|
|
2021-02-11 23:09:45 +01:00
|
|
|
ImGui::TextUnformatted("hex.view.hashes.result"_lang);
|
2020-11-28 15:53:11 +01:00
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
|
2020-11-11 09:18:35 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1: // CRC32
|
|
|
|
{
|
2021-06-06 18:00:14 +02:00
|
|
|
static int polynomial = 0x04C11DB7, init = 0xFFFFFFFF;
|
2020-11-11 09:18:35 +01:00
|
|
|
|
2021-02-11 23:09:45 +01:00
|
|
|
ImGui::InputInt("hex.view.hashes.iv"_lang, &init, 0, 0, ImGuiInputTextFlags_CharsHexadecimal);
|
2020-11-12 09:38:52 +01:00
|
|
|
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
|
|
|
|
|
2021-02-11 23:09:45 +01:00
|
|
|
ImGui::InputInt("hex.view.hashes.poly"_lang, &polynomial, 0, 0, ImGuiInputTextFlags_CharsHexadecimal);
|
2020-11-12 09:38:52 +01:00
|
|
|
if (ImGui::IsItemEdited()) this->m_shouldInvalidate = true;
|
2020-11-11 09:18:35 +01:00
|
|
|
|
|
|
|
ImGui::NewLine();
|
|
|
|
|
|
|
|
static u32 result = 0;
|
|
|
|
|
2020-11-12 09:38:52 +01:00
|
|
|
if (this->m_shouldInvalidate)
|
2021-02-02 23:11:23 +01:00
|
|
|
result = crypt::crc32(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1, polynomial, init);
|
2020-11-11 09:18:35 +01:00
|
|
|
|
2020-11-12 12:00:50 +01:00
|
|
|
char buffer[sizeof(result) * 2 + 1];
|
|
|
|
snprintf(buffer, sizeof(buffer), "%08X", result);
|
2020-11-28 15:53:11 +01:00
|
|
|
|
2021-02-11 23:09:45 +01:00
|
|
|
ImGui::TextUnformatted("hex.view.hashes.result"_lang);
|
2020-11-28 15:53:11 +01:00
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
|
2020-11-11 09:18:35 +01:00
|
|
|
}
|
|
|
|
break;
|
2021-02-02 23:11:23 +01:00
|
|
|
case 2: // MD5
|
2020-11-11 09:18:35 +01:00
|
|
|
{
|
2021-02-02 23:11:23 +01:00
|
|
|
static std::array<u8, 16> result = { 0 };
|
2020-11-11 09:18:35 +01:00
|
|
|
|
2020-11-12 09:38:52 +01:00
|
|
|
if (this->m_shouldInvalidate)
|
2021-02-02 23:11:23 +01:00
|
|
|
result = crypt::md5(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
|
|
|
char buffer[sizeof(result) * 2 + 1];
|
|
|
|
formatBigHexInt(result, buffer, sizeof(buffer));
|
2020-11-28 15:53:11 +01:00
|
|
|
|
|
|
|
ImGui::NewLine();
|
2021-02-11 23:09:45 +01:00
|
|
|
ImGui::TextUnformatted("hex.view.hashes.result"_lang);
|
2020-11-28 15:53:11 +01:00
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
|
2020-11-12 12:00:50 +01:00
|
|
|
}
|
|
|
|
break;
|
2021-02-02 23:11:23 +01:00
|
|
|
case 3: // SHA-1
|
2020-11-12 12:00:50 +01:00
|
|
|
{
|
2021-02-02 23:11:23 +01:00
|
|
|
static std::array<u8, 20> result = { 0 };
|
2020-11-12 12:00:50 +01:00
|
|
|
|
|
|
|
if (this->m_shouldInvalidate)
|
2021-02-02 23:11:23 +01:00
|
|
|
result = crypt::sha1(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
|
|
|
char buffer[sizeof(result) * 2 + 1];
|
|
|
|
formatBigHexInt(result, buffer, sizeof(buffer));
|
2020-11-28 15:53:11 +01:00
|
|
|
|
|
|
|
ImGui::NewLine();
|
2021-02-11 23:09:45 +01:00
|
|
|
ImGui::TextUnformatted("hex.view.hashes.result"_lang);
|
2020-11-28 15:53:11 +01:00
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
|
2020-11-12 12:00:50 +01:00
|
|
|
}
|
|
|
|
break;
|
2021-02-02 23:11:23 +01:00
|
|
|
case 4: // SHA-224
|
2020-11-12 12:00:50 +01:00
|
|
|
{
|
2021-02-02 23:11:23 +01:00
|
|
|
static std::array<u8, 28> result = { 0 };
|
2020-11-12 12:00:50 +01:00
|
|
|
|
|
|
|
if (this->m_shouldInvalidate)
|
2021-02-02 23:11:23 +01:00
|
|
|
result = crypt::sha224(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
|
|
|
char buffer[sizeof(result) * 2 + 1];
|
|
|
|
formatBigHexInt(result, buffer, sizeof(buffer));
|
2020-11-28 15:53:11 +01:00
|
|
|
|
|
|
|
ImGui::NewLine();
|
2021-02-11 23:09:45 +01:00
|
|
|
ImGui::TextUnformatted("hex.view.hashes.result"_lang);
|
2020-11-28 15:53:11 +01:00
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
|
2020-11-12 12:00:50 +01:00
|
|
|
}
|
|
|
|
break;
|
2021-02-02 23:11:23 +01:00
|
|
|
case 5: // SHA-256
|
2020-11-12 12:00:50 +01:00
|
|
|
{
|
2021-02-02 23:11:23 +01:00
|
|
|
static std::array<u8, 32> result;
|
2020-11-12 12:00:50 +01:00
|
|
|
|
|
|
|
if (this->m_shouldInvalidate)
|
2021-02-02 23:11:23 +01:00
|
|
|
result = crypt::sha256(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
|
|
|
char buffer[sizeof(result) * 2 + 1];
|
|
|
|
formatBigHexInt(result, buffer, sizeof(buffer));
|
2020-11-28 15:53:11 +01:00
|
|
|
|
|
|
|
ImGui::NewLine();
|
2021-02-11 23:09:45 +01:00
|
|
|
ImGui::TextUnformatted("hex.view.hashes.result"_lang);
|
2020-11-28 15:53:11 +01:00
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
|
2020-11-12 12:00:50 +01:00
|
|
|
}
|
|
|
|
break;
|
2021-02-02 23:11:23 +01:00
|
|
|
case 6: // SHA-384
|
2020-11-12 12:00:50 +01:00
|
|
|
{
|
2021-02-02 23:11:23 +01:00
|
|
|
static std::array<u8, 48> result;
|
2020-11-12 12:00:50 +01:00
|
|
|
|
|
|
|
if (this->m_shouldInvalidate)
|
2021-02-02 23:11:23 +01:00
|
|
|
result = crypt::sha384(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
|
|
|
char buffer[sizeof(result) * 2 + 1];
|
|
|
|
formatBigHexInt(result, buffer, sizeof(buffer));
|
2020-11-28 15:53:11 +01:00
|
|
|
|
|
|
|
ImGui::NewLine();
|
2021-02-11 23:09:45 +01:00
|
|
|
ImGui::TextUnformatted("hex.view.hashes.result"_lang);
|
2020-11-28 15:53:11 +01:00
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
|
2020-11-12 12:00:50 +01:00
|
|
|
}
|
|
|
|
break;
|
2021-02-02 23:11:23 +01:00
|
|
|
case 7: // SHA-512
|
2020-11-12 12:00:50 +01:00
|
|
|
{
|
2021-02-02 23:11:23 +01:00
|
|
|
static std::array<u8, 64> result;
|
2020-11-12 12:00:50 +01:00
|
|
|
|
|
|
|
if (this->m_shouldInvalidate)
|
2021-02-02 23:11:23 +01:00
|
|
|
result = crypt::sha512(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
|
2020-11-11 09:18:35 +01:00
|
|
|
|
2020-11-12 12:00:50 +01:00
|
|
|
char buffer[sizeof(result) * 2 + 1];
|
|
|
|
formatBigHexInt(result, buffer, sizeof(buffer));
|
2020-11-28 15:53:11 +01:00
|
|
|
|
|
|
|
ImGui::NewLine();
|
2021-02-11 23:09:45 +01:00
|
|
|
ImGui::TextUnformatted("hex.view.hashes.result"_lang);
|
2020-11-28 15:53:11 +01:00
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
|
2020-11-11 09:18:35 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-11-11 00:13:09 +01:00
|
|
|
|
|
|
|
}
|
2020-11-11 09:18:35 +01:00
|
|
|
|
2020-11-12 09:38:52 +01:00
|
|
|
this->m_shouldInvalidate = false;
|
2020-11-11 00:13:09 +01:00
|
|
|
}
|
|
|
|
ImGui::EndChild();
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|