diff options
author | Aleksi Lindeman <aleksi_888@hotmail.com> | 2018-02-10 03:38:47 +0100 |
---|---|---|
committer | Aleksi Lindeman <aleksi_888@hotmail.com> | 2018-02-10 03:39:41 +0100 |
commit | be3c931f9b2db357c0b4306ad248c968d90254a3 (patch) | |
tree | 8b7943e750c27ed0be07c0354827eb3856423a71 /include/Signature.hpp | |
parent | 28efc0068f47ec787791a07a63d720710068c095 (diff) |
Add private/public key for users
Diffstat (limited to 'include/Signature.hpp')
-rw-r--r-- | include/Signature.hpp | 83 |
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; + }; + } +} |