aboutsummaryrefslogtreecommitdiff
path: root/include/Signature.hpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-02-10 03:38:47 +0100
committerdec05eba <dec05eba@protonmail.com>2020-08-18 23:25:12 +0200
commita19e68b9b029d5374604e4b81dcff161d4b465ba (patch)
treef1401a0278bd82deff117279376beec761e60a55 /include/Signature.hpp
parent5c1a20c4dacfe03db90b70c2665e66a76574196c (diff)
Add private/public key for users
Diffstat (limited to 'include/Signature.hpp')
-rw-r--r--include/Signature.hpp83
1 files changed, 83 insertions, 0 deletions
diff --git a/include/Signature.hpp b/include/Signature.hpp
new file mode 100644
index 0000000..90d5278
--- /dev/null
+++ b/include/Signature.hpp
@@ -0,0 +1,83 @@
+#pragma once
+
+#include <stdexcept>
+
+namespace odhtdb
+{
+ const int PUBLIC_KEY_NUM_BYTES = 32;
+ const int PRIVATE_KEY_NUM_BYTES = 64;
+
+ class InvalidSignatureKeySize : public std::runtime_error
+ {
+ public:
+ InvalidSignatureKeySize(const std::string &errMsg) : std::runtime_error(errMsg) {}
+ };
+
+ class SignatureGenerationException : public std::runtime_error
+ {
+ public:
+ SignatureGenerationException(const std::string &errMsg) : std::runtime_error(errMsg) {}
+ };
+
+ class DataSignException : public std::runtime_error
+ {
+ public:
+ DataSignException(const std::string &errMsg) : std::runtime_error(errMsg) {}
+ };
+
+ namespace Signature
+ {
+ class PublicKey
+ {
+ friend class KeyPair;
+ public:
+ // Throws InvalidSignatureKeySize if size is not PUBLIC_KEY_NUM_BYTES
+ PublicKey(char *data, size_t size);
+ PublicKey(const PublicKey &other);
+ PublicKey& operator=(const PublicKey &other);
+
+ const char* getData() const { return data; }
+ size_t getSize() const { return PUBLIC_KEY_NUM_BYTES; }
+
+ std::string toString() const;
+ private:
+ PublicKey(){}
+ private:
+ char data[PUBLIC_KEY_NUM_BYTES];
+ };
+
+ class PrivateKey
+ {
+ friend class KeyPair;
+ public:
+ // Throws InvalidSignatureKeySize if size is not PRIVATE_KEY_NUM_BYTES
+ PrivateKey(char *data, size_t size);
+ PrivateKey(const PrivateKey &other);
+ PrivateKey& operator=(const PrivateKey &other);
+
+ const char* getData() const { return data; }
+ size_t getSize() const { return PRIVATE_KEY_NUM_BYTES; }
+
+ // Throws DataSignException if signing data failed for whatever reason. This wont happen unless there is an issue with the private key
+ std::string sign(const std::string &dataToSign) const;
+ std::string toString() const;
+ private:
+ PrivateKey(){}
+ private:
+ char data[PRIVATE_KEY_NUM_BYTES];
+ };
+
+ class KeyPair
+ {
+ public:
+ // Throws SignatureGenerationException if generation of private/public key pair fails (should never happen)
+ KeyPair();
+
+ const PublicKey& getPublicKey() const { return publicKey; }
+ const PrivateKey& getPrivateKey() const { return privateKey; }
+ private:
+ PublicKey publicKey;
+ PrivateKey privateKey;
+ };
+ }
+}