diff --git a/GCryptCLI/include/ModuleHashing.h b/GCryptCLI/include/ModuleHashing.h new file mode 100644 index 0000000..24ef854 --- /dev/null +++ b/GCryptCLI/include/ModuleHashing.h @@ -0,0 +1,18 @@ +#ifndef GCRYPTCLI_MODULE_HASHING_H +#define GCRYPTCLI_MODULE_HASHING_H + +namespace Module { + //! This module will hash supplied input + class Hashing { + public: + //! Will run the module + static void Run(); + + private: + // No instanciation! >:( + Hashing() {}; + }; +} + +#endif + diff --git a/GCryptCLI/src/Configuration.cpp b/GCryptCLI/src/Configuration.cpp index d0a6ee4..e56365c 100644 --- a/GCryptCLI/src/Configuration.cpp +++ b/GCryptCLI/src/Configuration.cpp @@ -93,11 +93,8 @@ void Configuration::DecideCiphertextFormat() { // So we have no iobase explicitly specified.. Let's default.. - // If we are encrypting or hashing, - else if ( - (activeModule == MODULE::ENCRYPTION) || - (activeModule == MODULE::HASH) - ) { + // If we are encrypting, + else if (activeModule == MODULE::ENCRYPTION) { // and input comes from a parameter, // and output goes to stdout, // let's assume base-16. @@ -116,6 +113,12 @@ void Configuration::DecideCiphertextFormat() { } + // Else, if we are hashing, + else if (activeModule == MODULE::HASH) { + // output is always defaults to base 16 + ciphertextFormat = IOBASE_FORMAT::BASE_16; + } + // Else, if we are decrypting, else if (activeModule == MODULE::DECRYPTION) { // and input comes from a parameter, we'll assume base-16. diff --git a/GCryptCLI/src/ModuleHashing.cpp b/GCryptCLI/src/ModuleHashing.cpp new file mode 100644 index 0000000..4f88ba2 --- /dev/null +++ b/GCryptCLI/src/ModuleHashing.cpp @@ -0,0 +1,53 @@ +#include "ModuleHashing.h" +#include "DataIngestionLayer.h" +#include "DataOutputLayer.h" +#include "KeyManager.h" +#include + +using namespace Module; +using namespace Leonetienne::GCrypt; + +void Hashing::Run() { + + // Initialize the data ingestion layer + IO::DataIngestionLayer::Init(); + + // Initialize the data output layer + IO::DataOutputLayer::Init(); + + // Initialize a ghasher + GHash hasher; + + // Read in new blocks, if not reached eof + while (!IO::DataIngestionLayer::IsFinished()) { + if (!IO::DataIngestionLayer::ReachedEOF()) { + IO::DataIngestionLayer::ReadBlock(); + } + + // Process a block, if one is ready + if (IO::DataIngestionLayer::IsBlockReady()) { + const Block cleartext = IO::DataIngestionLayer::GetNextBlock(); + hasher.Digest(cleartext); + } + } + + // Wait until we've finished digesting all blocks + // Enqueue that single block (the hash result) to the output layer + IO::DataOutputLayer::Enqueue(hasher.GetHashsum()); + + // Tell the data output layer that that was the last block + IO::DataOutputLayer::ReachedEOF(); + + // Dump it + while (!IO::DataOutputLayer::IsFinished()) { + IO::DataOutputLayer::WriteBlock(); + } + + // Destruct the data ingestion layer + IO::DataIngestionLayer::Destruct(); + + // Destruct the data output layer + IO::DataOutputLayer::Destruct(); + + return; +} diff --git a/GCryptCLI/src/main.cpp b/GCryptCLI/src/main.cpp index 1fb080a..0240519 100644 --- a/GCryptCLI/src/main.cpp +++ b/GCryptCLI/src/main.cpp @@ -4,6 +4,7 @@ #include "ModuleGenerateKey.h" #include "ModuleEncryption.h" #include "ModuleDecryption.h" +#include "ModuleHashing.h" int main(int argc, char* const* argv) { @@ -18,10 +19,6 @@ int main(int argc, char* const* argv) { // Launch our module switch (Configuration::activeModule) { - case Configuration::MODULE::GENERATE_KEY: - Module::GenerateKey::Run(); - break; - case Configuration::MODULE::ENCRYPTION: Module::Encryption::Run(); break; @@ -29,6 +26,14 @@ int main(int argc, char* const* argv) { case Configuration::MODULE::DECRYPTION: Module::Decryption::Run(); break; + + case Configuration::MODULE::GENERATE_KEY: + Module::GenerateKey::Run(); + break; + + case Configuration::MODULE::HASH: + Module::Hashing::Run(); + break; } return 0;