2020-11-11 00:13:09 +01:00
|
|
|
#include "views/view_hashes.hpp"
|
|
|
|
|
2020-11-11 10:47:02 +01:00
|
|
|
#include "providers/provider.hpp"
|
|
|
|
|
2020-11-12 12:00:50 +01:00
|
|
|
#include "crypto.hpp"
|
2020-11-11 00:13:09 +01:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2020-11-22 15:32:37 +01:00
|
|
|
#include "utils.hpp"
|
2020-11-19 11:37:16 +01:00
|
|
|
|
2020-11-11 00:13:09 +01:00
|
|
|
namespace hex {
|
|
|
|
|
2020-11-11 09:18:35 +01:00
|
|
|
ViewHashes::ViewHashes(prv::Provider* &dataProvider) : View(), m_dataProvider(dataProvider) {
|
2020-11-17 13:58:50 +01:00
|
|
|
View::subscribeEvent(Events::DataChanged, [this](const void*){
|
2020-11-12 09:38:52 +01:00
|
|
|
this->m_shouldInvalidate = true;
|
|
|
|
});
|
2020-11-11 00:13:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ViewHashes::~ViewHashes() {
|
2020-11-12 09:38:52 +01:00
|
|
|
View::unsubscribeEvent(Events::DataChanged);
|
2020-11-11 00:13:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-12 12:00:50 +01:00
|
|
|
static void formatBigHexInt(auto dataArray, char *buffer, size_t bufferSize) {
|
|
|
|
for (int i = 0; i < dataArray.size(); i++)
|
2020-11-22 15:32:37 +01:00
|
|
|
snprintf(buffer + 8 * i, bufferSize - 8 * i, "%08X", hex::changeEndianess(dataArray[i], std::endian::big));
|
2020-11-12 12:00:50 +01:00
|
|
|
}
|
|
|
|
|
2020-11-11 00:13:09 +01:00
|
|
|
void ViewHashes::createView() {
|
|
|
|
if (!this->m_windowOpen)
|
|
|
|
return;
|
|
|
|
|
2020-11-23 15:51:58 +01:00
|
|
|
if (ImGui::Begin("Hashing", &this->m_windowOpen, ImGuiWindowFlags_NoCollapse)) {
|
2020-11-11 00:13:09 +01:00
|
|
|
ImGui::BeginChild("##scrolling", ImVec2(0, 0), false, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoNav);
|
|
|
|
ImGui::NewLine();
|
|
|
|
|
2020-11-11 09:18:35 +01:00
|
|
|
if (this->m_dataProvider != nullptr && this->m_dataProvider->isAvailable()) {
|
2020-11-11 00:13:09 +01:00
|
|
|
|
2020-11-12 12:00:50 +01:00
|
|
|
if (ImGui::Combo("Hash Function", &this->m_currHashFunction, HashFunctionNames,sizeof(HashFunctionNames) / sizeof(const char *)))
|
|
|
|
this->m_shouldInvalidate = true;
|
2020-11-11 00:13:09 +01:00
|
|
|
|
2020-11-11 09:18:35 +01:00
|
|
|
ImGui::NewLine();
|
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::NewLine();
|
2020-11-11 00:13:09 +01:00
|
|
|
|
2020-11-11 09:18:35 +01:00
|
|
|
ImGui::InputInt("Begin", &this->m_hashStart, 0, 0, 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
|
|
|
|
2020-11-11 09:18:35 +01:00
|
|
|
ImGui::InputInt("End", &this->m_hashEnd, 0, 0, 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
|
|
|
|
2020-11-11 11:56:24 +01:00
|
|
|
size_t dataSize = this->m_dataProvider->getSize();
|
|
|
|
if (this->m_hashEnd >= dataSize)
|
|
|
|
this->m_hashEnd = dataSize - 1;
|
|
|
|
|
2020-11-11 09:18:35 +01:00
|
|
|
ImGui::NewLine();
|
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::NewLine();
|
2020-11-11 00:13:09 +01:00
|
|
|
|
2020-11-11 09:18:35 +01:00
|
|
|
if (this->m_hashEnd >= this->m_hashStart) {
|
2020-11-11 00:13:09 +01:00
|
|
|
|
2020-11-11 09:18:35 +01:00
|
|
|
switch (this->m_currHashFunction) {
|
|
|
|
case 0: // CRC16
|
|
|
|
{
|
2020-11-12 12:00:50 +01:00
|
|
|
int polynomial = 0, init = 0;
|
2020-11-11 09:18:35 +01:00
|
|
|
|
|
|
|
ImGui::InputInt("Initial Value", &init, 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::InputInt("Polynomial", &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();
|
|
|
|
ImGui::Separator();
|
|
|
|
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)
|
2020-11-12 12:00:50 +01:00
|
|
|
result = crc16(this->m_dataProvider, this->m_hashStart, this->m_hashEnd - this->m_hashStart + 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);
|
|
|
|
ImGui::InputText("Hash value", buffer, ImGuiInputTextFlags_ReadOnly);
|
2020-11-11 09:18:35 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1: // CRC32
|
|
|
|
{
|
2020-11-12 12:00:50 +01:00
|
|
|
int polynomial = 0, init = 0;
|
2020-11-11 09:18:35 +01:00
|
|
|
|
|
|
|
ImGui::InputInt("Initial Value", &init, 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::InputInt("Polynomial", &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();
|
|
|
|
ImGui::Separator();
|
|
|
|
ImGui::NewLine();
|
|
|
|
|
|
|
|
static u32 result = 0;
|
|
|
|
|
2020-11-12 09:38:52 +01:00
|
|
|
if (this->m_shouldInvalidate)
|
2020-11-12 12:00:50 +01:00
|
|
|
result = crc32(this->m_dataProvider, this->m_hashStart, this->m_hashEnd - this->m_hashStart + 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);
|
|
|
|
ImGui::InputText("Hash value", buffer, ImGuiInputTextFlags_ReadOnly);
|
2020-11-11 09:18:35 +01:00
|
|
|
}
|
|
|
|
break;
|
2020-11-12 12:00:50 +01:00
|
|
|
case 2: // MD4
|
2020-11-11 09:18:35 +01:00
|
|
|
{
|
|
|
|
static std::array<u32, 4> result;
|
|
|
|
|
2020-11-12 09:38:52 +01:00
|
|
|
if (this->m_shouldInvalidate)
|
2020-11-12 12:00:50 +01:00
|
|
|
result = md4(this->m_dataProvider, this->m_hashStart, this->m_hashEnd - this->m_hashStart + 1);
|
|
|
|
|
|
|
|
char buffer[sizeof(result) * 2 + 1];
|
|
|
|
formatBigHexInt(result, buffer, sizeof(buffer));
|
|
|
|
ImGui::InputText("Hash value", buffer, ImGuiInputTextFlags_ReadOnly);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3: // MD5
|
|
|
|
{
|
|
|
|
static std::array<u32, 4> result = { 0 };
|
|
|
|
|
|
|
|
if (this->m_shouldInvalidate)
|
|
|
|
result = md5(this->m_dataProvider, this->m_hashStart, this->m_hashEnd - this->m_hashStart + 1);
|
|
|
|
|
|
|
|
char buffer[sizeof(result) * 2 + 1];
|
|
|
|
formatBigHexInt(result, buffer, sizeof(buffer));
|
|
|
|
ImGui::InputText("Hash value", buffer, ImGuiInputTextFlags_ReadOnly);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 4: // SHA-1
|
|
|
|
{
|
|
|
|
static std::array<u32, 5> result = { 0 };
|
|
|
|
|
|
|
|
if (this->m_shouldInvalidate)
|
|
|
|
result = sha1(this->m_dataProvider, this->m_hashStart, this->m_hashEnd - this->m_hashStart + 1);
|
|
|
|
|
|
|
|
char buffer[sizeof(result) * 2 + 1];
|
|
|
|
formatBigHexInt(result, buffer, sizeof(buffer));
|
|
|
|
ImGui::InputText("Hash value", buffer, ImGuiInputTextFlags_ReadOnly);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 5: // SHA-224
|
|
|
|
{
|
|
|
|
static std::array<u32, 7> result = { 0 };
|
|
|
|
|
|
|
|
if (this->m_shouldInvalidate)
|
|
|
|
result = sha224(this->m_dataProvider, this->m_hashStart, this->m_hashEnd - this->m_hashStart + 1);
|
|
|
|
|
|
|
|
char buffer[sizeof(result) * 2 + 1];
|
|
|
|
formatBigHexInt(result, buffer, sizeof(buffer));
|
|
|
|
ImGui::InputText("Hash value", buffer, ImGuiInputTextFlags_ReadOnly);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 6: // SHA-256
|
|
|
|
{
|
|
|
|
static std::array<u32, 8> result;
|
|
|
|
|
|
|
|
if (this->m_shouldInvalidate)
|
|
|
|
result = sha256(this->m_dataProvider, this->m_hashStart, this->m_hashEnd - this->m_hashStart + 1);
|
|
|
|
|
|
|
|
char buffer[sizeof(result) * 2 + 1];
|
|
|
|
formatBigHexInt(result, buffer, sizeof(buffer));
|
|
|
|
ImGui::InputText("Hash value", buffer, ImGuiInputTextFlags_ReadOnly);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 7: // SHA-384
|
|
|
|
{
|
|
|
|
static std::array<u32, 12> result;
|
|
|
|
|
|
|
|
if (this->m_shouldInvalidate)
|
|
|
|
result = sha384(this->m_dataProvider, this->m_hashStart, this->m_hashEnd - this->m_hashStart + 1);
|
|
|
|
|
|
|
|
char buffer[sizeof(result) * 2 + 1];
|
|
|
|
formatBigHexInt(result, buffer, sizeof(buffer));
|
|
|
|
ImGui::InputText("Hash value", buffer, ImGuiInputTextFlags_ReadOnly);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 8: // SHA-512
|
|
|
|
{
|
|
|
|
static std::array<u32, 16> result;
|
|
|
|
|
|
|
|
if (this->m_shouldInvalidate)
|
|
|
|
result = sha512(this->m_dataProvider, this->m_hashStart, this->m_hashEnd - this->m_hashStart + 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));
|
|
|
|
ImGui::InputText("Hash value", 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
|
|
|
}
|
|
|
|
|
|
|
|
void ViewHashes::createMenu() {
|
2020-11-11 09:22:55 +01:00
|
|
|
if (ImGui::BeginMenu("View")) {
|
2020-11-11 00:13:09 +01:00
|
|
|
ImGui::MenuItem("Hash View", "", &this->m_windowOpen);
|
|
|
|
ImGui::EndMenu();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|