Code formatting
This commit is contained in:
parent
d62ca85c5b
commit
50687d658b
@ -67,7 +67,8 @@ size_t AES256_CBC::process_start(Mode mode,
|
|||||||
current += kIvSize;
|
current += kIvSize;
|
||||||
}
|
}
|
||||||
init(mode, encryption_key, iv);
|
init(mode, encryption_key, iv);
|
||||||
return output_offset + process_chunk(mode, current, end, output, output_offset, resize_in, resize_out);
|
return output_offset + process_chunk(mode, current, end, output,
|
||||||
|
output_offset, resize_in, resize_out);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t AES256_CBC::process_chunk(Mode mode,
|
size_t AES256_CBC::process_chunk(Mode mode,
|
||||||
|
30
src/crypto.h
30
src/crypto.h
@ -64,8 +64,9 @@ public:
|
|||||||
* @param flags flag bitset. Default (kBegin | kEnd)
|
* @param flags flag bitset. Default (kBegin | kEnd)
|
||||||
* @return processed data
|
* @return processed data
|
||||||
*/
|
*/
|
||||||
virtual raw_data process(
|
virtual raw_data process(const raw_data& inputdata,
|
||||||
const raw_data& inputdata, const std::bitset<Flags_num>& flags = Flags(kBegin | kEnd)) = 0;
|
const std::bitset<Flags_num>& flags =
|
||||||
|
Flags(kBegin | kEnd)) = 0;
|
||||||
|
|
||||||
/** @brief encrypt the content of a buffer using Cipher key
|
/** @brief encrypt the content of a buffer using Cipher key
|
||||||
* @details
|
* @details
|
||||||
@ -75,8 +76,9 @@ public:
|
|||||||
* @param flags flag bitset. Default (kBegin | kEnd)
|
* @param flags flag bitset. Default (kBegin | kEnd)
|
||||||
* @return encrypted data
|
* @return encrypted data
|
||||||
*/
|
*/
|
||||||
virtual raw_data encrypt(
|
virtual raw_data encrypt(const raw_data& plaintext,
|
||||||
const raw_data& plaintext, const std::bitset<Flags_num>& flags = Flags(kBegin | kEnd)) = 0;
|
const std::bitset<Flags_num>& flags =
|
||||||
|
Flags(kBegin | kEnd)) = 0;
|
||||||
|
|
||||||
/** @brief decrypt the content of a buffer using Cipher key
|
/** @brief decrypt the content of a buffer using Cipher key
|
||||||
* @details
|
* @details
|
||||||
@ -86,8 +88,9 @@ public:
|
|||||||
* @param flags flag bitset. Default (kBegin | kEnd)
|
* @param flags flag bitset. Default (kBegin | kEnd)
|
||||||
* @return decrypted data
|
* @return decrypted data
|
||||||
*/
|
*/
|
||||||
virtual raw_data decrypt(
|
virtual raw_data decrypt(const raw_data& ciphertext,
|
||||||
const raw_data& ciphertext, const std::bitset<Flags_num>& flags = Flags(kBegin | kEnd)) = 0;
|
const std::bitset<Flags_num>& flags =
|
||||||
|
Flags(kBegin | kEnd)) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Cipher context class
|
/** Cipher context class
|
||||||
@ -155,8 +158,9 @@ public:
|
|||||||
* @param flags flag bitset. Default (kBegin | kEnd)
|
* @param flags flag bitset. Default (kBegin | kEnd)
|
||||||
* @return processed data
|
* @return processed data
|
||||||
*/
|
*/
|
||||||
raw_data process(
|
raw_data process(const raw_data& inputdata,
|
||||||
const raw_data& inputdata, const std::bitset<Flags_num>& flags = Flags(kBegin | kEnd)) override;
|
const std::bitset<Flags_num>& flags =
|
||||||
|
Flags(kBegin | kEnd)) override;
|
||||||
|
|
||||||
/** @brief encrypt the content of a buffer using Cipher key
|
/** @brief encrypt the content of a buffer using Cipher key
|
||||||
* @details
|
* @details
|
||||||
@ -166,8 +170,9 @@ public:
|
|||||||
* @param flags flag bitset. Default (kBegin | kEnd)
|
* @param flags flag bitset. Default (kBegin | kEnd)
|
||||||
* @return encrypted data
|
* @return encrypted data
|
||||||
*/
|
*/
|
||||||
raw_data encrypt(
|
raw_data encrypt(const raw_data& plaintext,
|
||||||
const raw_data& plaintext, const std::bitset<Flags_num>& flags = Flags(kBegin | kEnd)) override;
|
const std::bitset<Flags_num>& flags =
|
||||||
|
Flags(kBegin | kEnd)) override;
|
||||||
|
|
||||||
/** @brief decrypt the content of a buffer using Cipher key
|
/** @brief decrypt the content of a buffer using Cipher key
|
||||||
* @details
|
* @details
|
||||||
@ -177,8 +182,9 @@ public:
|
|||||||
* @param flags flag bitset. Default (kBegin | kEnd)
|
* @param flags flag bitset. Default (kBegin | kEnd)
|
||||||
* @return decrypted data
|
* @return decrypted data
|
||||||
*/
|
*/
|
||||||
raw_data decrypt(
|
raw_data decrypt(const raw_data& ciphertext,
|
||||||
const raw_data& ciphertext, const std::bitset<Flags_num>& flags = Flags(kBegin | kEnd)) override;
|
const std::bitset<Flags_num>& flags =
|
||||||
|
Flags(kBegin | kEnd)) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/** @brief Initialize the Cipher context object needed for enc/dec operations
|
/** @brief Initialize the Cipher context object needed for enc/dec operations
|
||||||
|
@ -8,7 +8,8 @@ namespace horcrux {
|
|||||||
void Horcrux::process_chunks(size_t size) {
|
void Horcrux::process_chunks(size_t size) {
|
||||||
raw_data buf = input->read(size);
|
raw_data buf = input->read(size);
|
||||||
//std::cout << "read " << buf.size() << " " << size << std::endl;
|
//std::cout << "read " << buf.size() << " " << size << std::endl;
|
||||||
output->write_chunk(cipher->process(buf, Cipher::Flags(Cipher::flag::kBegin)));
|
output->write_chunk(cipher->process(buf,
|
||||||
|
Cipher::Flags(Cipher::flag::kBegin)));
|
||||||
while ((buf = input->read(size)).size() == size) {
|
while ((buf = input->read(size)).size() == size) {
|
||||||
//std::cout << "read " << buf.size() << std::endl;
|
//std::cout << "read " << buf.size() << std::endl;
|
||||||
output->write_chunk(cipher->process(buf, Cipher::Flags()));
|
output->write_chunk(cipher->process(buf, Cipher::Flags()));
|
||||||
@ -26,7 +27,8 @@ void Horcrux::init() {
|
|||||||
input = std::make_unique<FsPlainInput>(options.input[0]);
|
input = std::make_unique<FsPlainInput>(options.input[0]);
|
||||||
output = std::make_unique<FsCryptoOutput>(options.output, options.count,
|
output = std::make_unique<FsCryptoOutput>(options.output, options.count,
|
||||||
std::filesystem::path(options.input[0]).filename().string(),
|
std::filesystem::path(options.input[0]).filename().string(),
|
||||||
static_cast<FsPlainInput*>(input.get())->get_file_size() / options.count);
|
static_cast<FsPlainInput*>(input.get())->get_file_size() /
|
||||||
|
options.count);
|
||||||
options.base64_key = to_base64(cipher->get_encryption_key());
|
options.base64_key = to_base64(cipher->get_encryption_key());
|
||||||
break;
|
break;
|
||||||
case Mode::kDecrypt:
|
case Mode::kDecrypt:
|
||||||
|
@ -38,6 +38,11 @@ class Horcrux {
|
|||||||
* Used cipher, input and output are generic and the actual implementation
|
* Used cipher, input and output are generic and the actual implementation
|
||||||
* can be selected in this function */
|
* can be selected in this function */
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
|
/** @brief read from Input, process with Cipher and write to Output
|
||||||
|
* one chunk at a time
|
||||||
|
* @ size chunk buffer size.
|
||||||
|
*/
|
||||||
void process_chunks(size_t size);
|
void process_chunks(size_t size);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
35
src/io.cpp
35
src/io.cpp
@ -18,10 +18,6 @@ FsPlainInput::FsPlainInput(const std::string& path)
|
|||||||
|
|
||||||
raw_data FsPlainInput::read() {
|
raw_data FsPlainInput::read() {
|
||||||
return read(fs::file_size(file_path));
|
return read(fs::file_size(file_path));
|
||||||
// raw_data result(file_size);
|
|
||||||
// file_stream.open(file_path, std::ios::binary);
|
|
||||||
// file_stream.read(reinterpret_cast<char*>(result.data()), file_size);
|
|
||||||
// return result;
|
|
||||||
}
|
}
|
||||||
raw_data FsPlainInput::read(size_t size) {
|
raw_data FsPlainInput::read(size_t size) {
|
||||||
raw_data result(size);
|
raw_data result(size);
|
||||||
@ -29,7 +25,6 @@ raw_data FsPlainInput::read(size_t size) {
|
|||||||
file_stream.open(file_path, std::ios::binary);
|
file_stream.open(file_path, std::ios::binary);
|
||||||
file_stream.read(reinterpret_cast<char*>(result.data()), size);
|
file_stream.read(reinterpret_cast<char*>(result.data()), size);
|
||||||
result.resize(file_stream.gcount());
|
result.resize(file_stream.gcount());
|
||||||
//std::cout << size << " read " << file_stream.gcount() << std::endl;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,15 +50,8 @@ raw_data FsCryptoInput::read() {
|
|||||||
current_file = 0;
|
current_file = 0;
|
||||||
file_stream.close();
|
file_stream.close();
|
||||||
return read(total_size);
|
return read(total_size);
|
||||||
// raw_data result(total_size);
|
|
||||||
// size_t data_read{0};
|
|
||||||
// for (auto& f : file_paths) {
|
|
||||||
// std::ifstream ifstream(f.first, std::ios::binary);
|
|
||||||
// ifstream.read(reinterpret_cast<char*>(result.data()) + data_read, f.second);
|
|
||||||
// data_read += f.second;
|
|
||||||
// }
|
|
||||||
// return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
raw_data FsCryptoInput::read(size_t size) {
|
raw_data FsCryptoInput::read(size_t size) {
|
||||||
raw_data result(size);
|
raw_data result(size);
|
||||||
size_t data_read{0};
|
size_t data_read{0};
|
||||||
@ -80,7 +68,6 @@ raw_data FsCryptoInput::read(size_t size) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
result.resize(data_read);
|
result.resize(data_read);
|
||||||
//std::cout << "data_read: " << data_read << std::endl;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,12 +78,11 @@ FsPlainOutput::FsPlainOutput(const std::string& filename)
|
|||||||
throw std::invalid_argument("Output file is not a regular file");
|
throw std::invalid_argument("Output file is not a regular file");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t FsPlainOutput::write(const raw_data& to_write) {
|
size_t FsPlainOutput::write(const raw_data& to_write) {
|
||||||
//file_stream.open(file_path, std::ios::binary);
|
|
||||||
//file_stream.write(reinterpret_cast<const char*>(to_write.data()), to_write.size());
|
|
||||||
//return to_write.size();
|
|
||||||
return write_chunk(to_write);
|
return write_chunk(to_write);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t FsPlainOutput::write_chunk(const raw_data& to_write) {
|
size_t FsPlainOutput::write_chunk(const raw_data& to_write) {
|
||||||
if(!file_stream.is_open())
|
if(!file_stream.is_open())
|
||||||
file_stream.open(file_path, std::ios::binary);
|
file_stream.open(file_path, std::ios::binary);
|
||||||
@ -122,20 +108,6 @@ size_t FsCryptoOutput::write(const raw_data& to_write) {
|
|||||||
size = to_write.size() / num;
|
size = to_write.size() / num;
|
||||||
created_files.clear();
|
created_files.clear();
|
||||||
return write_chunk(to_write);
|
return write_chunk(to_write);
|
||||||
// std::ofstream f;
|
|
||||||
// created_files.clear();
|
|
||||||
// size_t data_written{0};
|
|
||||||
// for (int i = 0; i < num; ++i) {
|
|
||||||
// std::string name = base_name + '.' + std::to_string(i) + ".enc";
|
|
||||||
// created_files.emplace_back(folder_path / name);
|
|
||||||
// f = std::ofstream(created_files.back(),
|
|
||||||
// std::ios::binary | std::ios::trunc);
|
|
||||||
// auto chunk_size = i + 1 != num ? size : size + to_write.size() % size;
|
|
||||||
// f.write(reinterpret_cast<const char*>(to_write.data()) + data_written,
|
|
||||||
// chunk_size);
|
|
||||||
// data_written += chunk_size;
|
|
||||||
// }
|
|
||||||
// return data_written;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t FsCryptoOutput::write_chunk(const raw_data& to_write) {
|
size_t FsCryptoOutput::write_chunk(const raw_data& to_write) {
|
||||||
@ -143,7 +115,6 @@ size_t FsCryptoOutput::write_chunk(const raw_data& to_write) {
|
|||||||
if (size <= 0) {
|
if (size <= 0) {
|
||||||
throw std::invalid_argument("Invalid horcrux size");
|
throw std::invalid_argument("Invalid horcrux size");
|
||||||
}
|
}
|
||||||
//std::cout << "writing " << to_write.size() << std::endl;
|
|
||||||
size_t data_written{0};
|
size_t data_written{0};
|
||||||
while (data_written < to_write.size()){
|
while (data_written < to_write.size()){
|
||||||
if(!file_stream.is_open()){
|
if(!file_stream.is_open()){
|
||||||
|
46
src/io.h
46
src/io.h
@ -14,11 +14,14 @@ namespace horcrux {
|
|||||||
class Input {
|
class Input {
|
||||||
public:
|
public:
|
||||||
virtual ~Input() = default;
|
virtual ~Input() = default;
|
||||||
/** @brief Read from the input
|
/** @brief Read and consume the whole input
|
||||||
* @return new buffer containing all read data */
|
* @return new buffer containing all read data */
|
||||||
virtual raw_data read() = 0;
|
virtual raw_data read() = 0;
|
||||||
virtual raw_data read(size_t size) = 0;
|
|
||||||
|
|
||||||
|
/** @brief Read from the input
|
||||||
|
* @param size the amount of data to extract from the input
|
||||||
|
* @return a new buffer containing at most size bytes */
|
||||||
|
virtual raw_data read(size_t size) = 0;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -27,10 +30,15 @@ public:
|
|||||||
class Output {
|
class Output {
|
||||||
public:
|
public:
|
||||||
virtual ~Output() = default;
|
virtual ~Output() = default;
|
||||||
/** @brief Write to the output
|
|
||||||
* @param data to write
|
/** @brief Write everything to the output
|
||||||
|
* @param to_write the one and only data buffer to write
|
||||||
* @return written data */
|
* @return written data */
|
||||||
virtual size_t write(const raw_data& data) = 0;
|
virtual size_t write(const raw_data& data) = 0;
|
||||||
|
|
||||||
|
/** @brief Write single chunks to the output
|
||||||
|
* @param to_write the data chunk to write
|
||||||
|
* @return written data */
|
||||||
virtual size_t write_chunk(const raw_data& data) = 0;
|
virtual size_t write_chunk(const raw_data& data) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -52,11 +60,17 @@ public:
|
|||||||
* @param path input file path, must be a regular file */
|
* @param path input file path, must be a regular file */
|
||||||
explicit FsPlainInput(const std::string& path);
|
explicit FsPlainInput(const std::string& path);
|
||||||
|
|
||||||
|
/** @brief get the size of the underlying input file
|
||||||
|
* @return the file size */
|
||||||
const size_t& get_file_size() { return file_size; };
|
const size_t& get_file_size() { return file_size; };
|
||||||
|
|
||||||
/** @brief Read from the input
|
/** @brief Read and consume the whole input
|
||||||
* @return new buffer containing all read data */
|
* @return new buffer containing all read data */
|
||||||
raw_data read() override;
|
raw_data read() override;
|
||||||
|
|
||||||
|
/** @brief Read from the input
|
||||||
|
* @param size the amount of data to extract from the input
|
||||||
|
* @return a new buffer containing at most size bytes */
|
||||||
raw_data read(size_t size) override;
|
raw_data read(size_t size) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -80,10 +94,14 @@ public:
|
|||||||
* @param filenames paths of input files. They will be read in this order */
|
* @param filenames paths of input files. They will be read in this order */
|
||||||
explicit FsCryptoInput(const std::vector<std::string>& filenames);
|
explicit FsCryptoInput(const std::vector<std::string>& filenames);
|
||||||
|
|
||||||
/** @brief Read from the input
|
/** @brief Read and consume the whole input
|
||||||
* @details basically concatenate the input files.
|
* @details basically concatenate the input files.
|
||||||
* @return new buffer containing all read data */
|
* @return new buffer containing all read data */
|
||||||
raw_data read() override;
|
raw_data read() override;
|
||||||
|
|
||||||
|
/** @brief Read from the input
|
||||||
|
* @param size the amount of data to extract from the input
|
||||||
|
* @return a new buffer containing at most size bytes */
|
||||||
raw_data read(size_t size) override;
|
raw_data read(size_t size) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -103,10 +121,14 @@ public:
|
|||||||
* @param filename output file. It can be a new file, it must be writable */
|
* @param filename output file. It can be a new file, it must be writable */
|
||||||
explicit FsPlainOutput(const std::string& filename);
|
explicit FsPlainOutput(const std::string& filename);
|
||||||
|
|
||||||
/** @brief Write to the output
|
/** @brief Write everything to the output
|
||||||
* @param data to write
|
* @param to_write the one and only data buffer to write
|
||||||
* @return written data */
|
* @return written data */
|
||||||
size_t write(const raw_data& to_write) override;
|
size_t write(const raw_data& to_write) override;
|
||||||
|
|
||||||
|
/** @brief Write single chunks to the output
|
||||||
|
* @param to_write the data chunk to write
|
||||||
|
* @return written data */
|
||||||
size_t write_chunk(const raw_data& to_write) override;
|
size_t write_chunk(const raw_data& to_write) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -143,10 +165,14 @@ public:
|
|||||||
const std::string& filename = "horcrux",
|
const std::string& filename = "horcrux",
|
||||||
const size_t horcrux_size = 0);
|
const size_t horcrux_size = 0);
|
||||||
|
|
||||||
/** @brief Write to the output
|
/** @brief Write everything to the output
|
||||||
* @param data to write
|
* @param to_write the one and only data buffer to write
|
||||||
* @return written data */
|
* @return written data */
|
||||||
size_t write(const raw_data& to_write) override;
|
size_t write(const raw_data& to_write) override;
|
||||||
|
|
||||||
|
/** @brief Write single chunks to the output
|
||||||
|
* @param to_write the data chunk to write
|
||||||
|
* @return written data */
|
||||||
size_t write_chunk(const raw_data& to_write) override;
|
size_t write_chunk(const raw_data& to_write) override;
|
||||||
};
|
};
|
||||||
}; // namespace horcrux
|
}; // namespace horcrux
|
||||||
|
Loading…
Reference in New Issue
Block a user