aboutsummaryrefslogtreecommitdiff
path: root/include/Signature.hpp
blob: 90d527854ef98749306cb2c17fed2ec82f7f1f30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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;
        };
    }
}