aboutsummaryrefslogtreecommitdiff
path: root/src/LocalUserEncrypted.cpp
blob: a414c89d9ab791c8bdc3b248351442ccc64fbed6 (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
#include "../include/odhtdb/LocalUserEncrypted.hpp"
#include "../include/odhtdb/PasswordHash.hpp"
#include <cstring>

namespace odhtdb
{
    EncryptedPrivateKey::EncryptedPrivateKey()
    {
        memset(nonce, 0, ENCRYPTION_NONCE_BYTE_SIZE);
        memset(encryptedPrivateKey, 0, ENCRYPTION_CHECKSUM_BYTE_SIZE + PRIVATE_KEY_NUM_BYTES);
    }
    
    EncryptedPrivateKey::EncryptedPrivateKey(const EncryptedPrivateKey &other)
    {
        memcpy(nonce, other.nonce, ENCRYPTION_NONCE_BYTE_SIZE);
        memcpy(encryptedPrivateKey, other.encryptedPrivateKey, ENCRYPTION_CHECKSUM_BYTE_SIZE + PRIVATE_KEY_NUM_BYTES);
    }
    
    Signature::PrivateKey EncryptedPrivateKey::decrypt(const DataView &plainPassword, const DataView &salt) const
    {
        OwnedMemory hashedPassword = hashPassword(plainPassword, salt);
        Decryption decryptedPrivateKey(DataView((void*)encryptedPrivateKey, ENCRYPTION_CHECKSUM_BYTE_SIZE + PRIVATE_KEY_NUM_BYTES), 
                                       DataView((void*)nonce, ENCRYPTION_NONCE_BYTE_SIZE), 
                                       DataView(hashedPassword.data, hashedPassword.size));
        return { (const char*)decryptedPrivateKey.getDecryptedText().data, decryptedPrivateKey.getDecryptedText().size };
    }
}