diff options
| author | Hannes Schulze <haschu0103@outlook.de> | 2025-02-12 23:57:32 +0100 |
|---|---|---|
| committer | Hannes Schulze <haschu0103@outlook.de> | 2025-02-12 23:57:32 +0100 |
| commit | dd22e1ab7ab7b5bac841dae2b62bfa200b36c4d8 (patch) | |
| tree | 900781940d2ed92df1d87cc7dd6209853492e07f /app/encrypt | |
| parent | 0de4957a22dd408793d0526351bcb7d61786fe86 (diff) | |
add encryption/decryption
Diffstat (limited to 'app/encrypt')
| -rw-r--r-- | app/encrypt/CMakeLists.txt | 13 | ||||
| -rw-r--r-- | app/encrypt/main.cc | 52 |
2 files changed, 65 insertions, 0 deletions
diff --git a/app/encrypt/CMakeLists.txt b/app/encrypt/CMakeLists.txt new file mode 100644 index 0000000..5da53af --- /dev/null +++ b/app/encrypt/CMakeLists.txt @@ -0,0 +1,13 @@ +add_executable(nes_app_test) + +target_include_directories(nes_app_test PRIVATE .) +target_link_libraries( + nes_app_test + PRIVATE + nes::options + nes::nes) + +target_sources( + nes_app_test + PRIVATE + main.cc)
\ No newline at end of file diff --git a/app/encrypt/main.cc b/app/encrypt/main.cc new file mode 100644 index 0000000..3b32159 --- /dev/null +++ b/app/encrypt/main.cc @@ -0,0 +1,52 @@ +#include "nes/app/crypto/aes256.hh" +#include "nes/app/crypto/sha256.hh" +#include "nes/common/utils.hh" +#include "nes/common/containers/string-view.hh" +#include <iostream> +#include <vector> +#include <unistd.h> + +auto main(int const argc, char** argv) -> int +{ + if (argc != 2) + { + std::cerr << "Usage:\n"; + std::cerr << " " << argv[0] << " <key> < input > output" << std::endl; + return EXIT_FAILURE; + } + + auto const key_str = nes::string_view{ argv[1] }; + auto const key = nes::span{ reinterpret_cast<nes::u8 const*>(key_str.get_data()), key_str.get_length() }; + auto const key_hash = nes::app::sha256::hash(key); + + auto data = std::vector<nes::u8>{}; + nes::u8 buffer[1024]{}; + while (true) + { + auto const bytes = read(STDIN_FILENO, buffer, sizeof(buffer)); + if (bytes < 0) + { + perror("read"); + return EXIT_FAILURE; + } + + if (bytes == 0) + { + break; + } + + auto offset = data.size(); + data.resize(offset + bytes); + nes::copy(buffer, &data[offset], bytes); + } + + nes::app::aes256::encrypt(nes::span{ data.data(), static_cast<nes::u32>(data.size()) }, key_hash.get_data()); + + if (write(STDOUT_FILENO, data.data(), data.size()) < 0) + { + perror("write"); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +}
\ No newline at end of file |
