diff --git a/EncryptionManager.cpp b/EncryptionManager.cpp index 546471e..763b711 100644 --- a/EncryptionManager.cpp +++ b/EncryptionManager.cpp @@ -83,42 +83,44 @@ void EncryptionManager::saveCppHeadFile(const std::string& path) std::vector keyVec(key.begin(), key.end()); std::vector nonceVec(nonce.begin(), nonce.end()); - const std::string headerText = - "// Plik wygenerowany przy wykorzystaniu exPAK\n\n" - "// Klucz deszyfrujący\n" - "const std::array key{" + toHex(keyVec) + "};\n\n" - "// Ciąg nonce\n" - "const std::array nonce{" + toHex(nonceVec) + "}; "; + const uint32_t keySize = crypto_stream_chacha20_ietf_KEYBYTES; + const uint32_t nonceSize = crypto_stream_chacha20_ietf_NONCEBYTES; + + std::ofstream file(path + ".hpp"); + + file << "// Plik wygenerowany przez " << PROGRAM_TITLE << " " << PROGRAM_VERSION << std::endl; + file << std::endl; + file << std::endl; + file << "#pragma once" << std::endl; + file << "#include " << std::endl; + file << "#include " << std::endl; + file << std::endl; + file << "namespace enc" << std::endl; + file << "{" << std::endl; + file << " // Klucz deszyfrujący" << std::endl; + file << " const std::array key{" << std::endl; + file << " " << toHex(key.data(), key.size()) << std::endl; + file << " };" << std::endl; + file << std::endl; + file << " // Ciąg nonce" << std::endl; + file << " const std::array nonce{" << std::endl; + file << " " << toHex(nonce.data(), nonce.size()) << std::endl; + file << " }; " << std::endl; + file << "} //namespace" << std::endl; - std::ofstream file(path + ".hh"); - file << headerText; file.close(); } -std::string EncryptionManager::toHex(const std::vector& data) +std::string EncryptionManager::toHex(const unsigned char* data, size_t len) { - std::string bytes; - int sk = data.size(); - int skp = 1; - - for (const auto& b : data) - { - std::stringstream ss; - ss << "0x" << std::hex << std::uppercase << std::setw(2) << std::setfill('0') - << static_cast(b); - - bytes += "'"; - bytes += ss.str(); - bytes += "'"; - - if (skp < sk) - { - bytes += ", "; - skp++; - } + std::ostringstream oss; + oss << std::hex << std::setfill('0'); + for (size_t i = 0; i < len; ++i) { + oss << "0x" << std::setw(2) << static_cast(data[i]); + if (i + 1 != len) oss << ", "; + if ((i + 1) % 12 == 0 && i + 1 != len) oss << "\n "; } - - return bytes; + return oss.str(); } // Wczytaj klucz diff --git a/EncryptionManager.h b/EncryptionManager.h index 4f06a06..601b8ac 100644 --- a/EncryptionManager.h +++ b/EncryptionManager.h @@ -32,7 +32,7 @@ private: bool keyReady; void generateKeys(); - std::string toHex(const std::vector&); + std::string toHex(const unsigned char*, size_t); void saveCppHeadFile(const std::string&); template