2022-05-30 16:36:46 +02:00
|
|
|
#include <hex/api/content_registry.hpp>
|
|
|
|
#include <hex/api/localization.hpp>
|
|
|
|
#include <hex/helpers/crypto.hpp>
|
|
|
|
|
|
|
|
#include <hex/ui/imgui_imhex_extensions.h>
|
|
|
|
|
2023-01-18 14:30:56 +01:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2022-05-30 16:36:46 +02:00
|
|
|
namespace hex::plugin::builtin {
|
|
|
|
|
|
|
|
class HashMD5 : public ContentRegistry::Hashes::Hash {
|
|
|
|
public:
|
2022-06-14 11:54:34 +02:00
|
|
|
HashMD5() : Hash("hex.builtin.hash.md5") {}
|
2022-05-30 16:36:46 +02:00
|
|
|
|
|
|
|
Function create(std::string name) override {
|
|
|
|
return Hash::create(name, [](const Region& region, prv::Provider *provider) -> std::vector<u8> {
|
|
|
|
auto array = crypt::md5(provider, region.address, region.size);
|
|
|
|
|
|
|
|
return { array.begin(), array.end() };
|
|
|
|
});
|
|
|
|
}
|
2023-01-18 14:30:56 +01:00
|
|
|
|
|
|
|
[[nodiscard]] nlohmann::json store() const override { return { }; }
|
|
|
|
void load(const nlohmann::json &) override {}
|
2022-05-30 16:36:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class HashSHA1 : public ContentRegistry::Hashes::Hash {
|
|
|
|
public:
|
2022-06-14 11:54:34 +02:00
|
|
|
HashSHA1() : Hash("hex.builtin.hash.sha1") {}
|
2022-05-30 16:36:46 +02:00
|
|
|
|
|
|
|
Function create(std::string name) override {
|
|
|
|
return Hash::create(name, [](const Region& region, prv::Provider *provider) -> std::vector<u8> {
|
|
|
|
auto array = crypt::sha1(provider, region.address, region.size);
|
|
|
|
|
|
|
|
return { array.begin(), array.end() };
|
|
|
|
});
|
|
|
|
}
|
2023-01-18 14:30:56 +01:00
|
|
|
|
|
|
|
[[nodiscard]] nlohmann::json store() const override { return { }; }
|
|
|
|
void load(const nlohmann::json &) override {}
|
2022-05-30 16:36:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class HashSHA224 : public ContentRegistry::Hashes::Hash {
|
|
|
|
public:
|
2022-06-14 11:54:34 +02:00
|
|
|
HashSHA224() : Hash("hex.builtin.hash.sha224") {}
|
2022-05-30 16:36:46 +02:00
|
|
|
|
|
|
|
Function create(std::string name) override {
|
|
|
|
return Hash::create(name, [](const Region& region, prv::Provider *provider) -> std::vector<u8> {
|
|
|
|
auto array = crypt::sha224(provider, region.address, region.size);
|
|
|
|
|
|
|
|
return { array.begin(), array.end() };
|
|
|
|
});
|
|
|
|
}
|
2023-01-18 14:30:56 +01:00
|
|
|
|
|
|
|
[[nodiscard]] nlohmann::json store() const override { return { }; }
|
|
|
|
void load(const nlohmann::json &) override {}
|
2022-05-30 16:36:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class HashSHA256 : public ContentRegistry::Hashes::Hash {
|
|
|
|
public:
|
2022-06-14 11:54:34 +02:00
|
|
|
HashSHA256() : Hash("hex.builtin.hash.sha256") {}
|
2022-05-30 16:36:46 +02:00
|
|
|
|
|
|
|
Function create(std::string name) override {
|
|
|
|
return Hash::create(name, [](const Region& region, prv::Provider *provider) -> std::vector<u8> {
|
|
|
|
auto array = crypt::sha256(provider, region.address, region.size);
|
|
|
|
|
|
|
|
return { array.begin(), array.end() };
|
|
|
|
});
|
|
|
|
}
|
2023-01-18 14:30:56 +01:00
|
|
|
|
|
|
|
[[nodiscard]] nlohmann::json store() const override { return { }; }
|
|
|
|
void load(const nlohmann::json &) override {}
|
2022-05-30 16:36:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class HashSHA384 : public ContentRegistry::Hashes::Hash {
|
|
|
|
public:
|
2022-06-14 11:54:34 +02:00
|
|
|
HashSHA384() : Hash("hex.builtin.hash.sha384") {}
|
2022-05-30 16:36:46 +02:00
|
|
|
|
|
|
|
Function create(std::string name) override {
|
|
|
|
return Hash::create(name, [](const Region& region, prv::Provider *provider) -> std::vector<u8> {
|
|
|
|
auto array = crypt::sha384(provider, region.address, region.size);
|
|
|
|
|
|
|
|
return { array.begin(), array.end() };
|
|
|
|
});
|
|
|
|
}
|
2023-01-18 14:30:56 +01:00
|
|
|
|
|
|
|
[[nodiscard]] nlohmann::json store() const override { return { }; }
|
|
|
|
void load(const nlohmann::json &) override {}
|
2022-05-30 16:36:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class HashSHA512 : public ContentRegistry::Hashes::Hash {
|
|
|
|
public:
|
|
|
|
HashSHA512() : Hash("hex.builtin.hash.sha512") {}
|
|
|
|
|
|
|
|
Function create(std::string name) override {
|
|
|
|
return Hash::create(name, [](const Region& region, prv::Provider *provider) -> std::vector<u8> {
|
|
|
|
auto array = crypt::sha512(provider, region.address, region.size);
|
|
|
|
|
|
|
|
return { array.begin(), array.end() };
|
|
|
|
});
|
|
|
|
}
|
2023-01-18 14:30:56 +01:00
|
|
|
|
|
|
|
[[nodiscard]] nlohmann::json store() const override { return { }; }
|
|
|
|
void load(const nlohmann::json &) override {}
|
2022-05-30 16:36:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class HashCRC : public ContentRegistry::Hashes::Hash {
|
|
|
|
public:
|
|
|
|
using CRCFunction = T(*)(prv::Provider*&, u64, size_t, u32, u32, u32, bool, bool);
|
2023-03-22 10:53:57 +01:00
|
|
|
HashCRC(const std::string &name, const CRCFunction &crcFunction, u32 polynomial, u32 initialValue, u32 xorOut, bool reflectIn = false, bool reflectOut = false)
|
|
|
|
: Hash(name), m_crcFunction(crcFunction), m_polynomial(polynomial), m_initialValue(initialValue), m_xorOut(xorOut), m_reflectIn(reflectIn), m_reflectOut(reflectOut) {}
|
2022-05-30 16:36:46 +02:00
|
|
|
|
|
|
|
void draw() override {
|
|
|
|
ImGui::InputHexadecimal("hex.builtin.hash.crc.poly"_lang, &this->m_polynomial);
|
|
|
|
ImGui::InputHexadecimal("hex.builtin.hash.crc.iv"_lang, &this->m_initialValue);
|
|
|
|
ImGui::InputHexadecimal("hex.builtin.hash.crc.xor_out"_lang, &this->m_xorOut);
|
|
|
|
|
|
|
|
ImGui::NewLine();
|
|
|
|
|
|
|
|
ImGui::Checkbox("hex.builtin.hash.crc.refl_in"_lang, &this->m_reflectIn);
|
|
|
|
ImGui::Checkbox("hex.builtin.hash.crc.refl_out"_lang, &this->m_reflectOut);
|
|
|
|
}
|
|
|
|
|
|
|
|
Function create(std::string name) override {
|
|
|
|
return Hash::create(name, [hash = *this](const Region& region, prv::Provider *provider) -> std::vector<u8> {
|
|
|
|
auto result = hash.m_crcFunction(provider, region.address, region.size, hash.m_polynomial, hash.m_initialValue, hash.m_xorOut, hash.m_reflectIn, hash.m_reflectOut);
|
|
|
|
|
|
|
|
std::vector<u8> bytes(sizeof(result), 0x00);
|
|
|
|
std::memcpy(bytes.data(), &result, bytes.size());
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-18 14:30:56 +01:00
|
|
|
[[nodiscard]] nlohmann::json store() const override {
|
|
|
|
nlohmann::json result;
|
|
|
|
|
|
|
|
result["polynomial"] = this->m_polynomial;
|
|
|
|
result["initialValue"] = this->m_initialValue;
|
|
|
|
result["xorOut"] = this->m_xorOut;
|
|
|
|
result["reflectIn"] = this->m_reflectIn;
|
|
|
|
result["reflectOut"] = this->m_reflectOut;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void load(const nlohmann::json &json) override {
|
|
|
|
try {
|
2023-05-16 11:33:00 +02:00
|
|
|
this->m_polynomial = json.at("polynomial");
|
|
|
|
this->m_initialValue = json.at("initialValue");
|
|
|
|
this->m_xorOut = json.at("xorOut");
|
|
|
|
this->m_reflectIn = json.at("reflectIn");
|
|
|
|
this->m_reflectOut = json.at("reflectOut");
|
2023-01-18 14:30:56 +01:00
|
|
|
} catch (std::exception&) { }
|
|
|
|
}
|
|
|
|
|
2022-05-30 16:36:46 +02:00
|
|
|
private:
|
|
|
|
CRCFunction m_crcFunction;
|
|
|
|
|
|
|
|
u32 m_polynomial;
|
|
|
|
u32 m_initialValue;
|
|
|
|
u32 m_xorOut;
|
|
|
|
bool m_reflectIn = false, m_reflectOut = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
void registerHashes() {
|
|
|
|
ContentRegistry::Hashes::add<HashMD5>();
|
|
|
|
|
|
|
|
ContentRegistry::Hashes::add<HashSHA1>();
|
|
|
|
ContentRegistry::Hashes::add<HashSHA224>();
|
|
|
|
ContentRegistry::Hashes::add<HashSHA256>();
|
|
|
|
ContentRegistry::Hashes::add<HashSHA384>();
|
|
|
|
ContentRegistry::Hashes::add<HashSHA512>();
|
|
|
|
|
2023-01-25 00:06:48 +01:00
|
|
|
ContentRegistry::Hashes::add<HashCRC<u8>>("hex.builtin.hash.crc8", crypt::crc8, 0x07, 0x0000, 0x0000);
|
2022-06-14 11:54:34 +02:00
|
|
|
ContentRegistry::Hashes::add<HashCRC<u16>>("hex.builtin.hash.crc16", crypt::crc16, 0x8005, 0x0000, 0x0000);
|
|
|
|
ContentRegistry::Hashes::add<HashCRC<u32>>("hex.builtin.hash.crc32", crypt::crc32, 0x04C1'1DB7, 0xFFFF'FFFF, 0xFFFF'FFFF);
|
2023-03-22 10:53:57 +01:00
|
|
|
ContentRegistry::Hashes::add<HashCRC<u32>>("hex.builtin.hash.crc32mpeg", crypt::crc32, 0x04C1'1DB7, 0xFFFF'FFFF, 0x0000'0000, false, false);
|
|
|
|
ContentRegistry::Hashes::add<HashCRC<u32>>("hex.builtin.hash.crc32posix", crypt::crc32, 0x04C1'1DB7, 0x0000'0000, 0xFFFF'FFFF, false, false);
|
|
|
|
ContentRegistry::Hashes::add<HashCRC<u32>>("hex.builtin.hash.crc32c", crypt::crc32, 0x1EDC'6F41, 0xFFFF'FFFF, 0xFFFF'FFFF, true, true);
|
2022-05-30 16:36:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|