aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-02-17 14:26:29 +0100
committerdec05eba <dec05eba@protonmail.com>2020-08-18 23:25:41 +0200
commit33e823ddddddd4a13b1a05b90ae5b419b89bcb1d (patch)
tree7672ca6f2b3c3268decf0b44df1804b10f1a92e1 /src
parent40d94ad83f74753b71f33b58be8664bb21200219 (diff)
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?
Diffstat (limited to 'src')
-rw-r--r--src/DatabaseStorage.cpp2
-rw-r--r--src/Encryption.cpp30
2 files changed, 31 insertions, 1 deletions
diff --git a/src/DatabaseStorage.cpp b/src/DatabaseStorage.cpp
index 2028c63..62a2e77 100644
--- a/src/DatabaseStorage.cpp
+++ b/src/DatabaseStorage.cpp
@@ -27,7 +27,7 @@ namespace odhtdb
{
string errMsg = "Database storage with key ";
errMsg += key.hashedKey.toString();
- errMsg += " not found";
+ errMsg += " not found. Storage for a key needs to be created before data can be appended to it";
throw DatabaseStorageNotFound(errMsg);
}
it->second->objects.push_back({data, timestamp, creatorPublicKey});
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 <sodium/crypto_stream_xchacha20.h>
+#include <sodium/randombytes.h>
+#include <string>
+
+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);
+ }
+}