aboutsummaryrefslogtreecommitdiff
path: root/include/odhtdb/LocalUserEncrypted.hpp
blob: c250d1385918f644f4d9bf5385531038a2e772d1 (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
#pragma once

#include "User.hpp"
#include "types.hpp"
#include "Encryption.hpp"

namespace odhtdb
{
    struct EncryptedPrivateKey
    {
        u8 nonce[ENCRYPTION_NONCE_BYTE_SIZE];
        u8 encryptedPrivateKey[16 + PRIVATE_KEY_NUM_BYTES];
        
        EncryptedPrivateKey();
        EncryptedPrivateKey(const EncryptedPrivateKey &other);
        
        // Throws DecryptionException if password (or salt) is wrong
        Signature::PrivateKey decrypt(const DataView &plainPassword, const DataView &salt) const;
    };
    
    // Local user with encrypted private key
    class LocalUserEncrypted : public User
    {
    public:
        static LocalUserEncrypted* create(const Signature::PublicKey &publicKey, const EncryptedPrivateKey &encryptedPrivateKey, const std::string &name, Group *group)
        {
            return new LocalUserEncrypted(publicKey, encryptedPrivateKey, name, group);
        }
        
        const Signature::PublicKey& getPublicKey() const override
        {
            return publicKey;
        }
        
        const EncryptedPrivateKey& getPrivateKey() const
        {
            return encryptedPrivateKey;
        }
    private:
        LocalUserEncrypted(const Signature::PublicKey &_publicKey, const EncryptedPrivateKey &_encryptedPrivateKey, const std::string &name, Group *group) : 
            User(User::Type::LOCAL_ENCRYPTED, name, group),
            publicKey(_publicKey),
            encryptedPrivateKey(_encryptedPrivateKey)
        {
            
        }
    private:
        Signature::PublicKey publicKey;
        EncryptedPrivateKey encryptedPrivateKey;
    };
}