Skip to content

Commit

Permalink
Support file input source in ToolsScene
Browse files Browse the repository at this point in the history
  • Loading branch information
baderouaich committed Jul 14, 2024
1 parent 7b2fda8 commit e7be726
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
31 changes: 23 additions & 8 deletions src/Scenes/ToolsScene/HashingTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@
NS_ENIGMA_BEGIN
void HashingTool::OnCreate() {
ENIGMA_TRACE_CURRENT_FUNCTION();

// dm Register Available Hashing Algorithms
//m_hash_algorithms[HashAlgorithm::MD5] = std::make_unique<CryptoPP::MD5>();
m_selected_hash = HashAlgorithm::MD5; // MD5 selected by default
}

void HashingTool::OnDraw(Scene *parent) {
const auto& [win_w, win_h] = Application::getInstance()->GetWindow()->GetSize();

static constexpr const auto spacing = [](const std::uint8_t& n) noexcept { for (std::uint8_t i = 0; i < n; i++) ImGui::Spacing(); };
static constexpr const auto inline_dummy = [](const float& x, const float& y) noexcept { ImGui::SameLine(); ImGui::Dummy(ImVec2(x, y)); };

static auto& fonts = Application::getInstance()->GetFonts();
//static ImFont* const& font_audiowide_regular_45 = fonts.at("Audiowide-Regular-45");
Expand Down Expand Up @@ -141,15 +139,32 @@ void HashingTool::OnDestroy() {
void HashingTool::OnCalculateHashButtonPressed() {
if (m_input.empty()) return;
m_output.clear();

try {
switch (m_selected_hash) {
#define CASE_PERFORM_HASH(var, hash_algo, cryptopp_hash_name) \
case Enigma::HashingTool::HashAlgorithm::hash_algo: { \
if (!var) var = std::make_unique<CryptoPP::cryptopp_hash_name>(); \
[[maybe_unused]] const auto ss = CryptoPP::StringSource(m_input, true, new CryptoPP::HashFilter(*var, new CryptoPP::HexEncoder(new CryptoPP::StringSink(m_output), false))); \
break; \

#define CASE_PERFORM_HASH(var, hash_algo, cryptopp_hash_name) \
case Enigma::HashingTool::HashAlgorithm::hash_algo: { \
if (!var) var = std::make_unique<CryptoPP::cryptopp_hash_name>(); \
switch (m_input_source) { \
case InputSource::Text: { \
[[maybe_unused]] const auto ss = CryptoPP::StringSource(m_input, true, new CryptoPP::HashFilter(*var, new CryptoPP::HexEncoder(new CryptoPP::StringSink(m_output), false))); \
break; \
} \
case InputSource::File: { \
ENIGMA_ASSERT_OR_THROW(fs::is_regular_file(m_input), "No such file found"); \
[[maybe_unused]] const auto ss = CryptoPP::FileSource(m_input.c_str(), true, new CryptoPP::HashFilter(*var, new CryptoPP::HexEncoder(new CryptoPP::StringSink(m_output), false))); \
break; \
} \
default: { \
ENIGMA_ASSERT(false, "Unhandled InputSource"); \
break; \
}; \
} \
break; \
}


CASE_PERFORM_HASH(m_md2, MD2, Weak::MD2);
CASE_PERFORM_HASH(m_md4, MD4, Weak::MD4);
CASE_PERFORM_HASH(m_md5, MD5, Weak::MD5);
Expand Down
19 changes: 17 additions & 2 deletions src/Scenes/ToolsScene/HashingTool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,23 @@ class HashingTool : public Tool {
void OnCalculateHashButtonPressed();
void OnCopyHashButtonPressed();

private: /* Hash Algorithms */
std::string m_input{}; // text to calculate hash for (Message)
private: /* Hash Algorithms */
enum class InputSource : byte {
Text,
File,
ENIGMA_ENUM_DECLARE_BEGIN_END(Text)
} m_input_source = InputSource::Text;
static const char *inputSourceEnumToStr(InputSource is) {
switch (is) {
default:
case InputSource::Text:
return "Text";
case InputSource::File:
return "File";
}
}

std::string m_input{}; // text to calculate hash for (Message) or input file
std::string m_output{}; // calculated hash (Digest)

HashAlgorithm m_selected_hash; // Radio buttons selected hash algorithm
Expand Down

0 comments on commit e7be726

Please sign in to comment.