Poprawka do generowania pliku nagłówkowego c++ z kluczem i nonce

This commit is contained in:
yanczi 2025-11-19 10:28:52 +01:00
parent b9a4eaa87c
commit 58c3870c53
2 changed files with 33 additions and 31 deletions

View file

@ -83,42 +83,44 @@ void EncryptionManager::saveCppHeadFile(const std::string& path)
std::vector<unsigned char> keyVec(key.begin(), key.end()); std::vector<unsigned char> keyVec(key.begin(), key.end());
std::vector<unsigned char> nonceVec(nonce.begin(), nonce.end()); std::vector<unsigned char> nonceVec(nonce.begin(), nonce.end());
const std::string headerText = const uint32_t keySize = crypto_stream_chacha20_ietf_KEYBYTES;
"// Plik wygenerowany przy wykorzystaniu exPAK\n\n" const uint32_t nonceSize = crypto_stream_chacha20_ietf_NONCEBYTES;
"// Klucz deszyfruj¹cy\n"
"const std::array<unsigned char, crypto_stream_chacha20_ietf_KEYBYTES> key{" + toHex(keyVec) + "};\n\n" std::ofstream file(path + ".hpp");
"// Ci¹g nonce\n"
"const std::array<unsigned char, crypto_stream_chacha20_ietf_NONCEBYTES> nonce{" + toHex(nonceVec) + "}; "; file << "// Plik wygenerowany przez " << PROGRAM_TITLE << " " << PROGRAM_VERSION << std::endl;
file << std::endl;
file << std::endl;
file << "#pragma once" << std::endl;
file << "#include <array>" << std::endl;
file << "#include <cstdint>" << std::endl;
file << std::endl;
file << "namespace enc" << std::endl;
file << "{" << std::endl;
file << " // Klucz deszyfruj¹cy" << std::endl;
file << " const std::array<uint8_t, " << keySize << "> 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<uint8_t, " << nonceSize << "> 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(); file.close();
} }
std::string EncryptionManager::toHex(const std::vector<unsigned char>& data) std::string EncryptionManager::toHex(const unsigned char* data, size_t len)
{ {
std::string bytes; std::ostringstream oss;
int sk = data.size(); oss << std::hex << std::setfill('0');
int skp = 1; for (size_t i = 0; i < len; ++i) {
oss << "0x" << std::setw(2) << static_cast<int>(data[i]);
for (const auto& b : data) if (i + 1 != len) oss << ", ";
{ if ((i + 1) % 12 == 0 && i + 1 != len) oss << "\n ";
std::stringstream ss;
ss << "0x" << std::hex << std::uppercase << std::setw(2) << std::setfill('0')
<< static_cast<int>(b);
bytes += "'";
bytes += ss.str();
bytes += "'";
if (skp < sk)
{
bytes += ", ";
skp++;
} }
} return oss.str();
return bytes;
} }
// Wczytaj klucz // Wczytaj klucz

View file

@ -32,7 +32,7 @@ private:
bool keyReady; bool keyReady;
void generateKeys(); void generateKeys();
std::string toHex(const std::vector<unsigned char>&); std::string toHex(const unsigned char*, size_t);
void saveCppHeadFile(const std::string&); void saveCppHeadFile(const std::string&);
template <size_t N> template <size_t N>