Added encryption module, and data output layer
This commit is contained in:
parent
a00b8cc310
commit
fd75678ea3
@ -49,7 +49,7 @@ namespace IO {
|
||||
|
||||
// Indicates whether EOF has been reached
|
||||
static bool reachedEof;
|
||||
// Indicates whether this class is initialized
|
||||
// Indicates whether this class has been initialized
|
||||
static bool initialized;
|
||||
|
||||
// All read blocks, that haven't been given out yet
|
||||
|
60
GCryptCLI/include/DataOutputLayer.h
Normal file
60
GCryptCLI/include/DataOutputLayer.h
Normal file
@ -0,0 +1,60 @@
|
||||
#ifndef GCRYPTCLI_DATAOUTPUTLAYER_H
|
||||
#define GCRYPTCLI_DATAOUTPUTLAYER_H
|
||||
|
||||
#include <iosfwd>
|
||||
#include <queue>
|
||||
#include <GCrypt/Block.h>
|
||||
#include "Configuration.h"
|
||||
|
||||
using namespace Leonetienne::GCrypt;
|
||||
|
||||
namespace IO {
|
||||
|
||||
// This class is used to read in data.
|
||||
class DataOutputLayer {
|
||||
public:
|
||||
//! Will initialize the output
|
||||
static void Init();
|
||||
|
||||
//! Will destruct the output layer (like, closing file handles)
|
||||
static void Destruct();
|
||||
|
||||
//! Will queue a block for writing
|
||||
static void Enqueue(const Block& block);
|
||||
|
||||
//! Will attempt to write the next block
|
||||
static void WriteBlock();
|
||||
|
||||
//! Indicates that no more blocks will be enqueued
|
||||
static void ReachedEOF();
|
||||
|
||||
//! Returns true, if all blocks have been written, and an EOF signal as been received
|
||||
static bool IsFinished();
|
||||
|
||||
private:
|
||||
static std::ostream* out;
|
||||
|
||||
// The format to write data in
|
||||
static Configuration::IOBASE_FORMAT outFormat;
|
||||
|
||||
// We have to hold on to a reference to a filestream,
|
||||
// even if we're always just reading from in.
|
||||
// We still have to CLOSE the file handle afterwards!
|
||||
static std::ofstream ofs;
|
||||
|
||||
// Indicates whether EOF has been reached
|
||||
static bool reachedEof;
|
||||
|
||||
// Indicates whether this class has been initialized
|
||||
static bool initialized;
|
||||
|
||||
// All blocks, that haven't been written yet
|
||||
static std::queue<Block> blocks;
|
||||
|
||||
//! No instanciation >:(
|
||||
DataOutputLayer() {};
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
18
GCryptCLI/include/ModuleEncryption.h
Normal file
18
GCryptCLI/include/ModuleEncryption.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef GCRYPTCLI_MODULE_ENCRYPTION_H
|
||||
#define GCRYPTCLI_MODULE_ENCRYPTION_H
|
||||
|
||||
namespace Module {
|
||||
//! This module will encrypt supplied input
|
||||
class Encryption {
|
||||
public:
|
||||
//! Will run the module
|
||||
static void Run();
|
||||
|
||||
private:
|
||||
// No instanciation! >:(
|
||||
Encryption() {};
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
135
GCryptCLI/src/DataOutputLayer.cpp
Normal file
135
GCryptCLI/src/DataOutputLayer.cpp
Normal file
@ -0,0 +1,135 @@
|
||||
#include "DataOutputLayer.h"
|
||||
#include "DataFormatter.h"
|
||||
#include "CommandlineInterface.h"
|
||||
#include <ostream>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
using namespace IO;
|
||||
|
||||
void DataOutputLayer::Init() {
|
||||
|
||||
// Set our ostream
|
||||
switch (Configuration::outputTo) {
|
||||
|
||||
// Are we writing to stdout?
|
||||
case Configuration::OUTPUT_TO::STDOUT:
|
||||
|
||||
// Redirect our ostream to stdout
|
||||
out = &std::cout;
|
||||
break;
|
||||
|
||||
// Are we writing to a file?
|
||||
case Configuration::OUTPUT_TO::FILE:
|
||||
|
||||
// Open the file
|
||||
ofs.open(
|
||||
Configuration::inputFilename,
|
||||
std::ios::out | std::ios::binary
|
||||
);
|
||||
|
||||
// A little bit of error handling
|
||||
if (!ofs.good()) {
|
||||
throw std::runtime_error("Unable to open outfilestream!");
|
||||
}
|
||||
|
||||
// Redirect our ostream to this outfilestream
|
||||
out = &ofs;
|
||||
break;
|
||||
}
|
||||
|
||||
// Determine which iobase format to write in
|
||||
// If we are decrypting, input is not formatted
|
||||
if (Configuration::activeModule == Configuration::MODULE::DECRYPTION) {
|
||||
outFormat = Configuration::IOBASE_FORMAT::BASE_BYTES;
|
||||
}
|
||||
// If we are doing anything else, output is the requested format
|
||||
else {
|
||||
outFormat = Configuration::iobaseFormat;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
reachedEof = false;
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
void DataOutputLayer::Destruct() {
|
||||
if (Configuration::outputTo == Configuration::OUTPUT_TO::FILE) {
|
||||
ofs.close();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void DataOutputLayer::Enqueue(const Block& block) {
|
||||
blocks.emplace(block);
|
||||
return;
|
||||
}
|
||||
|
||||
void DataOutputLayer::WriteBlock() {
|
||||
// Some error checking
|
||||
if (!initialized) {
|
||||
throw std::runtime_error("Attempted to write on uninitialized DataOutputLayer!");
|
||||
}
|
||||
|
||||
// Check if we have any block to write
|
||||
// and if we should (output-puffering)
|
||||
// Basically: only output if we have anything to output, and
|
||||
// if --puffer-output is given, only output once we have reachedEof.
|
||||
if (
|
||||
(blocks.size() > 0) &&
|
||||
(
|
||||
(!CommandlineInterface::Get().HasParam("--puffer-output")) ||
|
||||
(reachedEof)
|
||||
)
|
||||
) {
|
||||
|
||||
// Fetch the block to write
|
||||
const Block block = blocks.front();
|
||||
blocks.pop();
|
||||
|
||||
// Recode it to our output format
|
||||
const std::string formattedBlock =
|
||||
DataFormatter::FormatBlock(
|
||||
block,
|
||||
outFormat
|
||||
);
|
||||
|
||||
// Dump it
|
||||
*out << formattedBlock;
|
||||
|
||||
// If this is not the last block, and the used iobase set
|
||||
// requires it, append a seperator
|
||||
if (
|
||||
(!IsFinished()) &&
|
||||
(
|
||||
(outFormat == Configuration::IOBASE_FORMAT::BASE_UWU) ||
|
||||
(outFormat == Configuration::IOBASE_FORMAT::BASE_UGH)
|
||||
)
|
||||
) {
|
||||
*out << " ";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void DataOutputLayer::ReachedEOF() {
|
||||
reachedEof = true;
|
||||
return;
|
||||
}
|
||||
|
||||
bool DataOutputLayer::IsFinished() {
|
||||
return (reachedEof) && (blocks.size() == 0);
|
||||
}
|
||||
|
||||
std::ostream* DataOutputLayer::out;
|
||||
std::ofstream DataOutputLayer::ofs;
|
||||
bool DataOutputLayer::reachedEof = false;
|
||||
bool DataOutputLayer::initialized = false;
|
||||
Configuration::IOBASE_FORMAT DataOutputLayer::outFormat;
|
||||
std::queue<Block> DataOutputLayer::blocks;
|
||||
|
57
GCryptCLI/src/ModuleEncryption.cpp
Normal file
57
GCryptCLI/src/ModuleEncryption.cpp
Normal file
@ -0,0 +1,57 @@
|
||||
#include "ModuleEncryption.h"
|
||||
#include "DataIngestionLayer.h"
|
||||
#include "DataOutputLayer.h"
|
||||
#include "KeyManager.h"
|
||||
#include <iostream>
|
||||
#include <GCrypt/GCipher.h>
|
||||
|
||||
using namespace Module;
|
||||
using namespace Leonetienne::GCrypt;
|
||||
|
||||
void Encryption::Run() {
|
||||
|
||||
// Initialize the data ingestion layer
|
||||
IO::DataIngestionLayer::Init();
|
||||
|
||||
// Initialize the data output layer
|
||||
IO::DataOutputLayer::Init();
|
||||
|
||||
// Initialize a cipher
|
||||
GCipher cipher(
|
||||
KeyManager::GetKey(),
|
||||
GCipher::DIRECTION::ENCIPHER
|
||||
);
|
||||
|
||||
while (!IO::DataOutputLayer::IsFinished()) {
|
||||
// Read in new blocks, if not reached eof
|
||||
if (!IO::DataIngestionLayer::ReachedEOF()) {
|
||||
IO::DataIngestionLayer::ReadBlock();
|
||||
}
|
||||
|
||||
// Process a block, if one is ready
|
||||
if (IO::DataIngestionLayer::IsBlockReady()) {
|
||||
const Block cleartext = IO::DataIngestionLayer::GetNextBlock();
|
||||
const Block ciphertext = cipher.Digest(cleartext);
|
||||
|
||||
// Enqueue the block for output
|
||||
IO::DataOutputLayer::Enqueue(ciphertext);
|
||||
|
||||
// Tell the data output layer that it just received the
|
||||
// last block, if it did
|
||||
if (IO::DataIngestionLayer::IsFinished()) {
|
||||
IO::DataOutputLayer::ReachedEOF();
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt to write a block
|
||||
IO::DataOutputLayer::WriteBlock();
|
||||
}
|
||||
|
||||
// Destruct the data ingestion layer
|
||||
IO::DataIngestionLayer::Destruct();
|
||||
|
||||
// Destruct the data output layer
|
||||
IO::DataOutputLayer::Destruct();
|
||||
|
||||
return;
|
||||
}
|
@ -2,8 +2,7 @@
|
||||
#include "Configuration.h"
|
||||
#include "KeyManager.h"
|
||||
#include "ModuleGenerateKey.h"
|
||||
|
||||
#include "DataIngestionLayer.h"
|
||||
#include "ModuleEncryption.h"
|
||||
|
||||
int main(int argc, char* const* argv) {
|
||||
|
||||
@ -16,12 +15,12 @@ int main(int argc, char* const* argv) {
|
||||
// Prepare the key
|
||||
KeyManager::PrepareKey();
|
||||
|
||||
IO::DataIngestionLayer::Init();
|
||||
|
||||
// Launch our module
|
||||
switch (Configuration::activeModule) {
|
||||
case Configuration::MODULE::GENERATE_KEY:
|
||||
Module::GenerateKey::Run();
|
||||
case Configuration::MODULE::ENCRYPTION:
|
||||
Module::Encryption::Run();
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user