VoidArchive/EncryptionManager.cpp

28 lines
No EOL
742 B
C++

#include "EncryptionManager.h"
EncryptionManager::EncryptionManager()
{
if (sodium_init() < 0) {
throw std::runtime_error("libsodium init failed");
}
}
std::vector<char> EncryptionManager::encrypt(const std::vector<char>& raw)
{
randombytes_buf(key.data(), key.size());
randombytes_buf(nonce.data(), nonce.size());
std::vector<char> crypt(raw.size());
if (crypto_stream_chacha20_ietf_xor_ic(
reinterpret_cast<unsigned char*>(crypt.data()),
reinterpret_cast<const unsigned char*>(raw.data()),
static_cast<unsigned long long>(raw.size()),
nonce.data(),
0,
key.data()) != 0) {
throw std::runtime_error("crypto_stream_chacha20_ietf_xor_ic failed");
}
return crypt;
}