From a19e68b9b029d5374604e4b81dcff161d4b465ba Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 10 Feb 2018 03:38:47 +0100 Subject: Add private/public key for users --- include/Signature.hpp | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 include/Signature.hpp (limited to 'include/Signature.hpp') 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 + +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; + }; + } +} -- cgit v1.2.3