From 67957afb6ba01bcd85f1abd1a50ad2c1aa813c7c Mon Sep 17 00:00:00 2001 From: Aleksi Lindeman <0xdec05eba@gmail.com> Date: Wed, 14 Feb 2018 22:18:48 +0100 Subject: Sign messages/verify message signatures --- include/Signature.hpp | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) (limited to 'include/Signature.hpp') diff --git a/include/Signature.hpp b/include/Signature.hpp index ea776ea..aace383 100644 --- a/include/Signature.hpp +++ b/include/Signature.hpp @@ -1,11 +1,13 @@ #pragma once +#include "DataView.hpp" #include namespace odhtdb { const int PUBLIC_KEY_NUM_BYTES = 32; const int PRIVATE_KEY_NUM_BYTES = 64; + const int SIGNED_HASH_SIZE = 64; class InvalidSignatureKeySize : public std::runtime_error { @@ -25,6 +27,25 @@ namespace odhtdb DataSignException(const std::string &errMsg) : std::runtime_error(errMsg) {} }; + class UnsignException : public std::runtime_error + { + public: + UnsignException(const std::string &errMsg) : std::runtime_error(errMsg) {} + virtual ~UnsignException(){} + }; + + class UnsignInvalidSizeException : public UnsignException + { + public: + UnsignInvalidSizeException(const std::string &errMsg) : UnsignException(errMsg) {} + }; + + class UnsignWrongKeyException : public UnsignException + { + public: + UnsignWrongKeyException(const std::string &errMsg) : UnsignException(errMsg) {} + }; + namespace Signature { class PublicKey @@ -41,6 +62,11 @@ namespace odhtdb const char* getData() const { return data; } size_t getSize() const { return PUBLIC_KEY_NUM_BYTES; } + // Throws UnsignWrongKeyException if signed message was not signed using the matching private key of this public key. + // Throws UnsignInvalidSizeException if signed message is too small (< SIGNED_HASH_SIZE). + // Both exceptions are derived from UnsignException + std::string unsign(const DataView &signedMessage) const; + std::string toString() const; private: PublicKey(){} @@ -61,7 +87,7 @@ namespace odhtdb 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 sign(const DataView &dataToSign) const; std::string toString() const; private: PrivateKey(){} -- cgit v1.2.3