Poprawka do generowania pliku nagłówkowego c++ z kluczem i nonce
This commit is contained in:
parent
b9a4eaa87c
commit
58c3870c53
2 changed files with 33 additions and 31 deletions
|
|
@ -83,42 +83,44 @@ void EncryptionManager::saveCppHeadFile(const std::string& path)
|
|||
std::vector<unsigned char> keyVec(key.begin(), key.end());
|
||||
std::vector<unsigned char> nonceVec(nonce.begin(), nonce.end());
|
||||
|
||||
const std::string headerText =
|
||||
"// Plik wygenerowany przy wykorzystaniu exPAK\n\n"
|
||||
"// Klucz deszyfruj¹cy\n"
|
||||
"const std::array<unsigned char, crypto_stream_chacha20_ietf_KEYBYTES> key{" + toHex(keyVec) + "};\n\n"
|
||||
"// Ci¹g nonce\n"
|
||||
"const std::array<unsigned char, crypto_stream_chacha20_ietf_NONCEBYTES> 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 <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();
|
||||
}
|
||||
|
||||
std::string EncryptionManager::toHex(const std::vector<unsigned char>& 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<int>(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<int>(data[i]);
|
||||
if (i + 1 != len) oss << ", ";
|
||||
if ((i + 1) % 12 == 0 && i + 1 != len) oss << "\n ";
|
||||
}
|
||||
}
|
||||
|
||||
return bytes;
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
// Wczytaj klucz
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ private:
|
|||
bool keyReady;
|
||||
|
||||
void generateKeys();
|
||||
std::string toHex(const std::vector<unsigned char>&);
|
||||
std::string toHex(const unsigned char*, size_t);
|
||||
void saveCppHeadFile(const std::string&);
|
||||
|
||||
template <size_t N>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue