From 33e823ddddddd4a13b1a05b90ae5b419b89bcb1d Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 17 Feb 2018 14:26:29 +0100 Subject: Add encryption functions (xchacha20) Changed license to GPL 3.0 because of incompatible license with opendht. Should odhtdb stay GPL 3.0 or should opendht be replaced with libdht so license can be changed back to MIT? --- src/Encryption.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/Encryption.cpp (limited to 'src/Encryption.cpp') diff --git a/src/Encryption.cpp b/src/Encryption.cpp new file mode 100644 index 0000000..8e87a8d --- /dev/null +++ b/src/Encryption.cpp @@ -0,0 +1,30 @@ +#include "../include/Encryption.hpp" +#include +#include +#include + +namespace odhtdb +{ + void generateEncryptionKey(EncryptionKey *output) + { + if(!output) return; + crypto_stream_xchacha20_keygen((unsigned char*)output); + } + + int encrypt(EncryptedData *output, const EncryptionKey *key, const void *data, size_t dataSize) + { + if(!output || !key) return -1; + if(dataSize == 0) return 0; + output->data.resize(dataSize); + randombytes_buf(output->nonce, NONCE_BYTE_SIZE); + return crypto_stream_xchacha20_xor((unsigned char*)&output->data[0], (const unsigned char*)data, dataSize, (const unsigned char*)output->nonce, (const unsigned char*)key); + } + + int decrypt(std::string *output, const EncryptionKey *key, const EncryptedData *encryptedData) + { + if(!encryptedData || !key || !output) return -1; + if(encryptedData->data.empty()) return 0; + output->resize(encryptedData->data.size()); + return crypto_stream_xchacha20_xor((unsigned char*)&(*output)[0], (const unsigned char*)&encryptedData->data[0], encryptedData->data.size(), (const unsigned char*)encryptedData->nonce, (const unsigned char*)key); + } +} -- cgit v1.2.3